@@ -23,6 +23,11 @@ def _(job):
23
23
return _
24
24
25
25
26
+ def test_worker_additional_context (app ):
27
+ worker_obj = worker .Worker (app = app , additional_context = {"foo" : "bar" })
28
+ assert worker_obj .base_context .additional_context == {"foo" : "bar" }
29
+
30
+
26
31
async def test_run (test_worker , mocker , caplog ):
27
32
caplog .set_level ("INFO" )
28
33
@@ -315,17 +320,15 @@ def task_func(test_context, a):
315
320
task_name = "job" ,
316
321
queue = "yay" ,
317
322
)
318
- test_worker = worker .Worker (app , queues = ["yay" ], name = "my_worker" )
319
- context = job_context .JobContext (
320
- worker_name = "my_worker" ,
321
- worker_id = 3 ,
322
- worker_queues = ["yay" ],
323
- job = job ,
324
- task = task_func ,
323
+ test_worker = worker .Worker (
324
+ app , queues = ["yay" ], name = "my_worker" , additional_context = {"foo" : "bar" }
325
325
)
326
- test_worker .current_contexts [3 ] = context
326
+ context = test_worker .context_for_worker (worker_id = 3 )
327
+
327
328
await test_worker .run_job (job= job , worker_id = 3 )
328
329
330
+ context = context .evolve (task = task_func )
331
+
329
332
assert result == [
330
333
context ,
331
334
1 ,
@@ -519,3 +522,36 @@ def test_context_for_worker_reset(app):
519
522
context = test_worker .context_for_worker (worker_id = 3 , reset = True )
520
523
521
524
assert context == expected
525
+
526
+
527
+ def test_worker_copy_additional_context (app ):
528
+ additional_context = {"foo" : "bar" }
529
+ test_worker = worker .Worker (
530
+ app = app ,
531
+ name = "worker" ,
532
+ additional_context = additional_context ,
533
+ )
534
+
535
+ # mutate the additional_context object and test that we have the original
536
+ # value in the worker
537
+ additional_context ["foo" ] = "baz"
538
+ assert test_worker .base_context .additional_context == {"foo" : "bar" }
539
+
540
+
541
+ def test_context_for_worker_with_additional_context (app ):
542
+ additional_context = {"foo" : "bar" }
543
+ test_worker = worker .Worker (
544
+ app = app ,
545
+ name = "worker" ,
546
+ additional_context = additional_context ,
547
+ )
548
+
549
+ context1 = test_worker .context_for_worker (worker_id = 3 )
550
+
551
+ # mutate the additional_context object for one worker and test that it
552
+ # hasn't changed for other workers
553
+ context1 .additional_context ["foo" ] = "baz"
554
+
555
+ context2 = test_worker .context_for_worker (worker_id = 4 )
556
+
557
+ assert context2 .additional_context == {"foo" : "bar" }
0 commit comments