1
1
import json
2
2
from threading import Thread
3
3
from time import sleep
4
+ from unittest import mock
4
5
5
6
from scheduler .helpers .queues import get_queue
6
7
from scheduler .tests .jobs import test_job , two_seconds_job
7
8
from ..test_views .base import BaseTestCase
9
+ from ...helpers .callback import Callback
8
10
from ...redis_models import JobModel , JobStatus , WorkerModel
9
11
from ...worker import create_worker
10
12
from ...worker .commands import send_command , StopJobCommand
11
13
from ...worker .commands .suspend_worker import SuspendWorkCommand
12
14
13
15
16
+ def _callback_func ():
17
+ pass
18
+
19
+
20
+ def callback_func ():
21
+ pass
22
+
23
+
14
24
class WorkerCommandsTest (BaseTestCase ):
15
25
def test_stop_worker_command__green (self ):
16
26
# Arrange
@@ -45,11 +55,12 @@ def test_stop_worker_command__bad_worker_name(self):
45
55
job = JobModel .get (job .name , connection = queue .connection )
46
56
self .assertFalse (job .is_queued )
47
57
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 ):
49
60
# Arrange
50
61
worker_name = "test"
51
62
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 ) )
53
64
self .assertTrue (job .is_queued )
54
65
worker = create_worker ("default" , name = worker_name , burst = True , with_scheduler = False )
55
66
worker .bootstrap ()
@@ -70,3 +81,4 @@ def test_stop_job_command__success(self):
70
81
self .assertIsNone (worker .current_job_name )
71
82
self .assertEqual (job .status , JobStatus .STOPPED )
72
83
t .join ()
84
+ mock_stopped_callback .assert_called ()
0 commit comments