@@ -71,6 +71,11 @@ def pytest_configure(config):
71
71
my_vcr .before_record_request = lambda request : None
72
72
73
73
74
+ def is_using_real_services (request ):
75
+ return (request .config .option .update_cassettes or
76
+ request .config .option .ignore_cassettes )
77
+
78
+
74
79
@pytest .fixture (scope = 'session' )
75
80
def hsclient ():
76
81
return HubstorageClient (auth = TEST_AUTH , endpoint = TEST_ENDPOINT )
@@ -87,23 +92,20 @@ def hsspiderid(hsproject):
87
92
return str (hsproject .ids .spider (TEST_SPIDER_NAME , create = 1 ))
88
93
89
94
90
- @my_vcr .use_cassette ()
91
95
@pytest .fixture (scope = 'session' )
92
- def hscollection (hsproject ):
96
+ def hscollection (hsproject , request ):
93
97
collection = get_test_collection (hsproject )
94
- clean_collection (collection )
98
+ if is_using_real_services (request ):
99
+ clean_collection (collection )
95
100
yield collection
96
- clean_collection (collection )
97
101
98
102
99
- @my_vcr .use_cassette ()
100
103
@pytest .fixture (autouse = True , scope = 'session' )
101
- def setup_session (hsclient , hsproject , hscollection ):
102
- set_testbotgroup (hsproject )
103
- remove_all_jobs (hsproject )
104
+ def setup_session (hsclient , hsproject , hscollection , request ):
105
+ if is_using_real_services (request ):
106
+ set_testbotgroup (hsproject )
107
+ remove_all_jobs (hsproject )
104
108
yield
105
- remove_all_jobs (hsproject )
106
- unset_testbotgroup (hsproject )
107
109
hsclient .close ()
108
110
109
111
@@ -116,10 +118,7 @@ def setup_vcrpy(request, hsproject):
116
118
request .function .__module__ .split ('.' )[- 1 ],
117
119
request .function .__name__
118
120
)
119
- # we should clean jobs only when working with real services
120
- # doesn't make sense to clean jobs when working with cassettes
121
- if (request .config .option .update_cassettes or
122
- request .config .option .ignore_cassettes ):
121
+ if is_using_real_services (request ):
123
122
remove_all_jobs (hsproject )
124
123
with my_vcr .use_cassette (cassette_name ):
125
124
yield
@@ -145,14 +144,11 @@ def remove_all_jobs(hsproject):
145
144
del hsproject .settings [k ]
146
145
hsproject .settings .save ()
147
146
148
- # Cleanup JobQ
149
- jobq = hsproject .jobq
150
- for queuename in ('pending' , 'running' , 'finished' ):
151
- info = {'summary' : [None ]} # poor-guy do...while
152
- while info ['summary' ]:
153
- info = jobq .summary (queuename )
154
- for summary in info ['summary' ]:
155
- _remove_job (hsproject , summary ['key' ])
147
+ # Cleanup JobQ: run 2 times to ensure we covered all jobs
148
+ for queuename in ('pending' , 'running' , 'finished' )* 2 :
149
+ info = hsproject .jobq .summary (queuename )
150
+ for summary in info ['summary' ]:
151
+ _remove_job (hsproject , summary ['key' ])
156
152
157
153
158
154
def _remove_job (hsproject , jobkey ):
0 commit comments