11import json
22from threading import Thread
33from time import sleep
4+ from unittest import mock
45
56from scheduler .helpers .queues import get_queue
67from scheduler .tests .jobs import test_job , two_seconds_job
78from ..test_views .base import BaseTestCase
9+ from ...helpers .callback import Callback
810from ...redis_models import JobModel , JobStatus , WorkerModel
911from ...worker import create_worker
1012from ...worker .commands import send_command , StopJobCommand
1113from ...worker .commands .suspend_worker import SuspendWorkCommand
1214
1315
16+ def _callback_func ():
17+ pass
18+ 19+ 20+ def callback_func ():
21+ pass
22+ 23+ 1424class WorkerCommandsTest (BaseTestCase ):
1525 def test_stop_worker_command__green (self ):
1626 # Arrange
@@ -45,11 +55,12 @@ def test_stop_worker_command__bad_worker_name(self):
4555 job = JobModel .get (job .name , connection = queue .connection )
4656 self .assertFalse (job .is_queued )
4757
48- def test_stop_job_command__success (self ):
58+ @mock .patch ("scheduler.redis_models.job.JobModel.call_stopped_callback" )
59+ def test_stop_job_command__success (self , mock_stopped_callback ):
4960 # Arrange
5061 worker_name = "test"
5162 queue = get_queue ("default" )
52- job = queue .create_and_enqueue_job (two_seconds_job )
63+ job = queue .create_and_enqueue_job (two_seconds_job , on_stopped = Callback ( callback_func ) )
5364 self .assertTrue (job .is_queued )
5465 worker = create_worker ("default" , name = worker_name , burst = True , with_scheduler = False )
5566 worker .bootstrap ()
@@ -70,3 +81,4 @@ def test_stop_job_command__success(self):
7081 self .assertIsNone (worker .current_job_name )
7182 self .assertEqual (job .status , JobStatus .STOPPED )
7283 t .join ()
84+ mock_stopped_callback .assert_called ()
0 commit comments