1- import pytest
1+ import logging
22import time
33from collections import defaultdict
4- import logging
4+
5+ import pytest
6+
57from openeo .extra .job_management .thread_worker import _JobManagerWorkerThreadPool
68
9+
710# Fixture for creating and cleaning up the worker thread pool
811@pytest .fixture
912def worker_pool ():
1013 worker_pool = _JobManagerWorkerThreadPool ()
1114 yield worker_pool
1215 worker_pool .shutdown ()
1316
17+
1418# Fixture for the shared stats dictionary
1519@pytest .fixture
1620def stats ():
1721 return defaultdict (int )
1822
19- class TestJobManagerWorkerThreadPool :
2023
24+ class TestJobManagerWorkerThreadPool :
2125 def test_worker_thread_lifecycle (self , worker_pool , caplog ):
2226 with caplog .at_level (logging .INFO ):
2327 assert not worker_pool ._executor ._shutdown
@@ -44,17 +48,12 @@ def test_start_job_success(self, worker_pool, stats, requests_mock):
4448 f"{ backend_url } /jobs" ,
4549 json = {"job_id" : job_id , "status" : "created" },
4650 status_code = 201 ,
47- headers = {"openeo-identifier" : job_id }
51+ headers = {"openeo-identifier" : job_id },
4852 )
4953 requests_mock .post (
50- f"{ backend_url } /jobs/{ job_id } /results" ,
51- json = {"job_id" : job_id , "status" : "finished" },
52- status_code = 202
53- )
54- requests_mock .get (
55- f"{ backend_url } /jobs/{ job_id } " ,
56- json = {"id" : job_id , "status" : "finished" }
54+ f"{ backend_url } /jobs/{ job_id } /results" , json = {"job_id" : job_id , "status" : "finished" }, status_code = 202
5755 )
56+ requests_mock .get (f"{ backend_url } /jobs/{ job_id } " , json = {"id" : job_id , "status" : "finished" })
5857 # Submit several valid jobs
5958 for _ in range (3 ):
6059 worker_pool .submit_work (worker_pool .WORK_TYPE_START_JOB , (backend_url , "token" , job_id ))
@@ -82,11 +81,10 @@ def test_process_futures_mixed_success_and_failure(self, worker_pool, stats, req
8281 requests_mock .post (
8382 f"{ backend_url_success } /jobs/{ job_id_success } /results" ,
8483 json = {"job_id" : job_id_success , "status" : "finished" },
85- status_code = 202
84+ status_code = 202 ,
8685 )
8786 requests_mock .get (
88- f"{ backend_url_success } /jobs/{ job_id_success } " ,
89- json = {"id" : job_id_success , "status" : "finished" }
87+ f"{ backend_url_success } /jobs/{ job_id_success } " , json = {"id" : job_id_success , "status" : "finished" }
9088 )
9189 # Failed job
9290 backend_url_failure = "https://failure.test"
@@ -105,29 +103,27 @@ def test_invalid_work_type(self, worker_pool, stats, requests_mock, caplog):
105103 backend_url = "https://foo.test"
106104 job_id = "job-123"
107105 requests_mock .get (backend_url , json = {"api_version" : "1.1.0" })
108-
106+
109107 # Test that invalid work type raises ValueError
110108 with pytest .raises (ValueError ) as exc_info :
111109 worker_pool .submit_work ("invalid_work_type" , (backend_url , "token" , job_id ))
112-
110+
113111 assert "Unsupported work type: invalid_work_type" in str (exc_info .value )
114112 assert len (worker_pool ._futures ) == 0
115113
116-
117114 @pytest .mark .parametrize (
118- "work_args,expected_log" ,
119- [
120- (("https://foo.test" , "token" ), "Expected 3 arguments for work type start_job, got 2" ),
121- ((None , "token" , "job-123" ), "root_url must be a string" ),
122- ]
115+ "work_args,expected_log" ,
116+ [
117+ (("https://foo.test" , "token" ), "Expected 3 arguments for work type start_job, got 2" ),
118+ ((None , "token" , "job-123" ), "root_url must be a string" ),
119+ ],
123120 )
124121 def test_invalid_work_args (self , worker_pool , stats , caplog , work_args , expected_log ):
125- with caplog .at_level (logging .ERROR ):
122+ with caplog .at_level (logging .ERROR ):
126123 with pytest .raises (Exception ): # Expect an exception to be raised
127124 worker_pool .submit_work (worker_pool .WORK_TYPE_START_JOB , work_args )
128-
125+
129126 # Verify the expected log message
130127 assert expected_log in caplog .text
131128 assert len (worker_pool ._futures ) == 0
132129 assert stats ["job start" ] == 0
133-
0 commit comments