1
- # Copyright 2020 Google LLC All Rights Reserved.
1
+ # Copyright 2018 Google LLC All Rights Reserved.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
17
17
import sys
18
18
import uuid
19
19
20
- from gcp_devrel .testing import eventually_consistent
20
+ from googleapiclient .errors import HttpError
21
+ from retrying import retry
21
22
22
23
# Add datasets for bootstrapping datasets for testing
23
- sys .path .append (os .path .join (os .path .dirname (__file__ ), '..' , 'datasets' )) # noqa
24
+ sys .path .append (os .path .join (os .path .dirname (__file__ ), '..' , 'datasets' )) # noqa
24
25
import datasets
25
26
import hl7v2_stores
26
27
import hl7v2_messages
35
36
label_value = 'TRUE'
36
37
37
38
38
- @pytest .fixture (scope = 'module' )
39
- def test_dataset ():
40
- dataset = datasets .create_dataset (
41
- project_id ,
42
- cloud_region ,
43
- dataset_id )
44
-
45
- yield dataset
39
+ def retry_if_server_exception (exception ):
40
+ return isinstance (exception , (HttpError ))
46
41
47
- # Clean up
48
- datasets .delete_dataset (
49
- project_id ,
50
- cloud_region ,
51
- dataset_id )
52
42
43
+ @pytest .fixture (scope = "module" )
44
+ def test_dataset ():
45
+ @retry (
46
+ wait_exponential_multiplier = 1000 ,
47
+ wait_exponential_max = 10000 ,
48
+ stop_max_attempt_number = 10 ,
49
+ retry_on_exception = retry_if_server_exception )
50
+ def create ():
51
+ try :
52
+ datasets .create_dataset (project_id , cloud_region , dataset_id )
53
+ except HttpError as err :
54
+ # We ignore 409 conflict here, because we know it's most
55
+ # likely the first request failed on the client side, but
56
+ # the creation suceeded on the server side.
57
+ if err .resp .status == 409 :
58
+ print (
59
+ 'Got exception {} while creating dataset' .format (
60
+ err .resp .status ))
61
+ else :
62
+ raise
63
+ create ()
64
+
65
+ yield
53
66
54
- @pytest .fixture (scope = 'module' )
67
+ # Clean up
68
+ @retry (
69
+ wait_exponential_multiplier = 1000 ,
70
+ wait_exponential_max = 10000 ,
71
+ stop_max_attempt_number = 10 ,
72
+ retry_on_exception = retry_if_server_exception )
73
+ def clean_up ():
74
+ try :
75
+ datasets .delete_dataset (project_id , cloud_region , dataset_id )
76
+ except HttpError as err :
77
+ # The API returns 403 when the dataset doesn't exist.
78
+ if err .resp .status == 404 or err .resp .status == 403 :
79
+ print (
80
+ 'Got exception {} while deleting dataset' .format (
81
+ err .resp .status ))
82
+ else :
83
+ raise
84
+
85
+ clean_up ()
86
+
87
+
88
+ @pytest .fixture (scope = "module" )
55
89
def test_hl7v2_store ():
56
- hl7v2_store = hl7v2_stores .create_hl7v2_store (
57
- project_id ,
58
- cloud_region ,
59
- dataset_id ,
60
- hl7v2_store_id )
61
-
62
- yield hl7v2_store
90
+ @retry (
91
+ wait_exponential_multiplier = 1000 ,
92
+ wait_exponential_max = 10000 ,
93
+ stop_max_attempt_number = 10 ,
94
+ retry_on_exception = retry_if_server_exception )
95
+ def create ():
96
+ try :
97
+ hl7v2_stores .create_hl7v2_store (
98
+ project_id , cloud_region , dataset_id , hl7v2_store_id )
99
+ except HttpError as err :
100
+ # We ignore 409 conflict here, because we know it's most
101
+ # likely the first request failed on the client side, but
102
+ # the creation suceeded on the server side.
103
+ if err .resp .status == 409 :
104
+ print (
105
+ 'Got exception {} while creating HL7v2 store' .format (
106
+ err .resp .status ))
107
+ else :
108
+ raise
109
+ create ()
110
+
111
+ yield
63
112
64
- hl7v2_stores .delete_hl7v2_store (
65
- project_id ,
66
- cloud_region ,
67
- dataset_id ,
68
- hl7v2_store_id )
113
+ # Clean up
114
+ @retry (
115
+ wait_exponential_multiplier = 1000 ,
116
+ wait_exponential_max = 10000 ,
117
+ stop_max_attempt_number = 10 ,
118
+ retry_on_exception = retry_if_server_exception )
119
+ def clean_up ():
120
+ try :
121
+ hl7v2_stores .delete_hl7v2_store (
122
+ project_id , cloud_region , dataset_id , hl7v2_store_id )
123
+ except HttpError as err :
124
+ # The API returns 403 when the HL7v2 store doesn't exist.
125
+ if err .resp .status == 404 or err .resp .status == 403 :
126
+ print (
127
+ 'Got exception {} while deleting HL7v2 store' .format (
128
+ err .resp .status ))
129
+ else :
130
+ raise
131
+
132
+ clean_up ()
69
133
70
134
71
135
def test_CRUD_hl7v2_message (test_dataset , test_hl7v2_store , capsys ):
@@ -76,16 +140,15 @@ def test_CRUD_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
76
140
hl7v2_store_id ,
77
141
hl7v2_message_file )
78
142
79
- hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
80
- project_id ,
81
- cloud_region ,
82
- dataset_id ,
83
- hl7v2_store_id )
143
+ hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
144
+ project_id ,
145
+ cloud_region ,
146
+ dataset_id ,
147
+ hl7v2_store_id )
84
148
85
- assert len (hl7v2_messages_list ) > 0
86
- hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
87
- nonlocal hl7v2_message_id
88
- hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
149
+ assert len (hl7v2_messages_list ) > 0
150
+ hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
151
+ hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
89
152
90
153
hl7v2_messages .get_hl7v2_message (
91
154
project_id ,
@@ -117,16 +180,15 @@ def test_ingest_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
117
180
hl7v2_store_id ,
118
181
hl7v2_message_file )
119
182
120
- hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
121
- project_id ,
122
- cloud_region ,
123
- dataset_id ,
124
- hl7v2_store_id )
183
+ hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
184
+ project_id ,
185
+ cloud_region ,
186
+ dataset_id ,
187
+ hl7v2_store_id )
125
188
126
- assert len (hl7v2_messages_list ) > 0
127
- hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
128
- nonlocal hl7v2_message_id
129
- hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
189
+ assert len (hl7v2_messages_list ) > 0
190
+ hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
191
+ hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
130
192
131
193
hl7v2_messages .get_hl7v2_message (
132
194
project_id ,
@@ -158,16 +220,15 @@ def test_patch_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
158
220
hl7v2_store_id ,
159
221
hl7v2_message_file )
160
222
161
- hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
162
- project_id ,
163
- cloud_region ,
164
- dataset_id ,
165
- hl7v2_store_id )
223
+ hl7v2_messages_list = hl7v2_messages .list_hl7v2_messages (
224
+ project_id ,
225
+ cloud_region ,
226
+ dataset_id ,
227
+ hl7v2_store_id )
166
228
167
- assert len (hl7v2_messages_list ) > 0
168
- hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
169
- nonlocal hl7v2_message_id
170
- hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
229
+ assert len (hl7v2_messages_list ) > 0
230
+ hl7v2_message_name = hl7v2_messages_list [0 ].get ('name' )
231
+ hl7v2_message_id = hl7v2_message_name .split ('/' , 9 )[9 ]
171
232
172
233
hl7v2_messages .patch_hl7v2_message (
173
234
project_id ,
0 commit comments