File: test_registry.py

package info (click to toggle)
python-rq 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,580 kB
  • sloc: python: 13,878; makefile: 22; sh: 19
file content (724 lines) | stat: -rw-r--r-- 30,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import math
from datetime import timedelta
from unittest import mock
from unittest.mock import ANY

import pytest

from rq.defaults import DEFAULT_FAILURE_TTL
from rq.exceptions import AbandonedJobError, InvalidJobOperation
from rq.executions import Execution
from rq.job import Dependency, Job, JobStatus, requeue_job
from rq.queue import Queue
from rq.registry import (
    BaseRegistry,
    CanceledJobRegistry,
    DeferredJobRegistry,
    FailedJobRegistry,
    FinishedJobRegistry,
    StartedJobRegistry,
    clean_registries,
)
from rq.serializers import JSONSerializer
from rq.utils import as_text, current_timestamp, now
from rq.worker import Worker
from tests import RQTestCase
from tests.fixtures import div_by_zero, say_hello


class CustomJob(Job):
    """A custom job class just to test it"""


class TestRegistry(RQTestCase):
    """Test all the BaseRegistry functionality"""

    def setUp(self):
        super().setUp()
        self.registry = BaseRegistry(connection=self.connection)

    def test_init(self):
        """Registry can be instantiated with queue or name/Redis connection"""
        queue = Queue('foo', connection=self.connection)
        registry = BaseRegistry(queue=queue)
        self.assertEqual(registry.name, queue.name)
        self.assertEqual(registry.connection, queue.connection)
        self.assertEqual(registry.serializer, queue.serializer)

        registry = BaseRegistry('bar', self.connection, serializer=JSONSerializer)
        self.assertEqual(registry.name, 'bar')
        self.assertEqual(registry.connection, self.connection)
        self.assertEqual(registry.serializer, JSONSerializer)

    def test_key(self):
        self.assertEqual(self.registry.key, 'rq:registry:default')

    def test_custom_job_class(self):
        registry = BaseRegistry(job_class=CustomJob)
        self.assertIsNot(registry.job_class, self.registry.job_class)

    def test_contains(self):
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        self.assertNotIn(job, self.registry)
        self.assertNotIn(job.id, self.registry)

        self.registry.add(job, 5)

        self.assertIn(job, self.registry)
        self.assertIn(job.id, self.registry)

    def test_get_expiration_time(self):
        """registry.get_expiration_time() returns correct datetime objects"""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        self.registry.add(job, 5)
        time = self.registry.get_expiration_time(job)
        expected_time = (now() + timedelta(seconds=5)).replace(microsecond=0)
        self.assertGreaterEqual(time, expected_time - timedelta(seconds=2))
        self.assertLessEqual(time, expected_time + timedelta(seconds=2))

    def test_add_and_remove(self):
        """Adding and removing job from BaseRegistry."""
        timestamp = current_timestamp()

        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        # Test that job is added with the right score
        self.registry.add(job, 1000)
        self.assertLess(self.connection.zscore(self.registry.key, job.id), timestamp + 1002)

        # Ensure that a timeout of -1 results in a score of inf
        self.registry.add(job, -1)
        self.assertEqual(self.connection.zscore(self.registry.key, job.id), float('inf'))

        # Ensure that job is removed from sorted set, but job key is not deleted
        self.registry.remove(job)
        self.assertIsNone(self.connection.zscore(self.registry.key, job.id))
        self.assertTrue(self.connection.exists(job.key))

        self.registry.add(job, -1)

        # registry.remove() also accepts job.id
        self.registry.remove(job.id)
        self.assertIsNone(self.connection.zscore(self.registry.key, job.id))

        self.registry.add(job, -1)

        # delete_job = True deletes job key
        self.registry.remove(job, delete_job=True)
        self.assertIsNone(self.connection.zscore(self.registry.key, job.id))
        self.assertFalse(self.connection.exists(job.key))

        job = queue.enqueue(say_hello)

        self.registry.add(job, -1)

        # delete_job = True also works with job.id
        self.registry.remove(job.id, delete_job=True)
        self.assertIsNone(self.connection.zscore(self.registry.key, job.id))
        self.assertFalse(self.connection.exists(job.key))

    def test_add_and_remove_with_serializer(self):
        """Adding and removing job from BaseRegistry (with serializer)."""
        # delete_job = True also works with job.id and custom serializer
        queue = Queue(connection=self.connection, serializer=JSONSerializer)
        registry = BaseRegistry(connection=self.connection, serializer=JSONSerializer)
        job = queue.enqueue(say_hello)
        registry.add(job, -1)
        registry.remove(job.id, delete_job=True)
        self.assertIsNone(self.connection.zscore(registry.key, job.id))
        self.assertFalse(self.connection.exists(job.key))

    def test_get_job_ids(self):
        """Getting job ids from BaseRegistry."""
        timestamp = current_timestamp()
        self.connection.zadd(self.registry.key, {'will-be-cleaned-up': 1})
        self.connection.zadd(self.registry.key, {'foo': timestamp + 10})
        self.connection.zadd(self.registry.key, {'bar': timestamp + 20})
        self.assertEqual(self.registry.get_job_ids(), ['will-be-cleaned-up', 'foo', 'bar'])

    def test_get_expired_job_ids(self):
        """Getting expired job ids form BaseRegistry."""
        timestamp = current_timestamp()

        self.connection.zadd(self.registry.key, {'foo': 1})
        self.connection.zadd(self.registry.key, {'bar': timestamp + 10})
        self.connection.zadd(self.registry.key, {'baz': timestamp + 30})

        self.assertEqual(self.registry.get_expired_job_ids(), ['foo'])
        self.assertEqual(self.registry.get_expired_job_ids(timestamp + 20), ['foo', 'bar'])

        # CanceledJobRegistry does not implement get_expired_job_ids()
        registry = CanceledJobRegistry(connection=self.connection)
        self.assertRaises(NotImplementedError, registry.get_expired_job_ids)

    def test_count(self):
        """BaseRegistry returns the right number of job count."""
        timestamp = current_timestamp() + 10
        self.connection.zadd(self.registry.key, {'will-be-cleaned-up': 1})
        self.connection.zadd(self.registry.key, {'foo': timestamp})
        self.connection.zadd(self.registry.key, {'bar': timestamp})
        self.assertEqual(self.registry.count, 3)
        self.assertEqual(len(self.registry), 3)

    def test_get_job_count(self):
        """Ensure cleanup is not called and does not affect the reported number of jobs.

        Note, the original motivation to stop calling cleanup was to make the count operation O(1) to allow usage of
        monitoring tools and avoid side effects of failure callbacks that cleanup triggers.
        """
        timestamp = current_timestamp() + 10
        self.connection.zadd(self.registry.key, {'will-be-counted-despite-outdated': 1})
        self.connection.zadd(self.registry.key, {'foo': timestamp})
        self.connection.zadd(self.registry.key, {'bar': timestamp})
        with mock.patch.object(self.registry, 'cleanup') as mock_cleanup:
            self.assertEqual(self.registry.get_job_count(cleanup=False), 3)
        mock_cleanup.assert_not_called()

    def test_clean_registries(self):
        """clean_registries() cleans Started and Finished job registries."""

        queue = Queue(connection=self.connection)

        finished_job_registry = FinishedJobRegistry(connection=self.connection)
        self.connection.zadd(finished_job_registry.key, {'foo': 1})

        started_job_registry = StartedJobRegistry(connection=self.connection)
        self.connection.zadd(started_job_registry.key, {'foo:execution_id': 1})

        failed_job_registry = FailedJobRegistry(connection=self.connection)
        self.connection.zadd(failed_job_registry.key, {'foo': 1})

        clean_registries(queue)
        self.assertEqual(self.connection.zcard(finished_job_registry.key), 0)
        self.assertEqual(self.connection.zcard(started_job_registry.key), 0)
        self.assertEqual(self.connection.zcard(failed_job_registry.key), 0)

    def test_clean_registries_with_serializer(self):
        """clean_registries() cleans Started and Finished job registries (with serializer)."""

        queue = Queue(connection=self.connection, serializer=JSONSerializer)

        finished_job_registry = FinishedJobRegistry(connection=self.connection, serializer=JSONSerializer)
        self.connection.zadd(finished_job_registry.key, {'foo': 1})

        started_job_registry = StartedJobRegistry(connection=self.connection, serializer=JSONSerializer)
        self.connection.zadd(started_job_registry.key, {'foo:execution_id': 1})

        failed_job_registry = FailedJobRegistry(connection=self.connection, serializer=JSONSerializer)
        self.connection.zadd(failed_job_registry.key, {'foo': 1})

        clean_registries(queue)
        self.assertEqual(self.connection.zcard(finished_job_registry.key), 0)
        self.assertEqual(self.connection.zcard(started_job_registry.key), 0)
        self.assertEqual(self.connection.zcard(failed_job_registry.key), 0)

    def test_get_queue(self):
        """registry.get_queue() returns the right Queue object."""
        registry = BaseRegistry(connection=self.connection)
        self.assertEqual(registry.get_queue(), Queue(connection=self.connection))

        registry = BaseRegistry('foo', connection=self.connection, serializer=JSONSerializer)
        self.assertEqual(registry.get_queue(), Queue('foo', connection=self.connection, serializer=JSONSerializer))


class TestFinishedJobRegistry(RQTestCase):
    def setUp(self):
        super().setUp()
        self.registry = FinishedJobRegistry(connection=self.connection)

    def test_key(self):
        self.assertEqual(self.registry.key, 'rq:finished:default')

    def test_cleanup(self):
        """Finished job registry removes expired jobs."""
        timestamp = current_timestamp()
        self.connection.zadd(self.registry.key, {'foo': 1})
        self.connection.zadd(self.registry.key, {'bar': timestamp + 10})
        self.connection.zadd(self.registry.key, {'baz': timestamp + 30})

        self.registry.cleanup()
        self.assertEqual(self.registry.get_job_ids(), ['bar', 'baz'])

        self.registry.cleanup(timestamp + 20)
        self.assertEqual(self.registry.get_job_ids(), ['baz'])

        # CanceledJobRegistry now implements noop cleanup, should not raise exception
        registry = CanceledJobRegistry(connection=self.connection)
        registry.cleanup()

    def test_jobs_are_put_in_registry(self):
        """Completed jobs are added to FinishedJobRegistry."""
        self.assertEqual(self.registry.get_job_ids(), [])
        queue = Queue(connection=self.connection)
        worker = Worker([queue], connection=self.connection)

        # Completed jobs are put in FinishedJobRegistry
        job = queue.enqueue(say_hello)
        worker.perform_job(job, queue)
        self.assertEqual(self.registry.get_job_ids(), [job.id])

        # When job is deleted, it should be removed from FinishedJobRegistry
        self.assertEqual(job.get_status(), JobStatus.FINISHED)
        job.delete()
        self.assertEqual(self.registry.get_job_ids(), [])

        # Failed jobs are not put in FinishedJobRegistry
        failed_job = queue.enqueue(div_by_zero)
        worker.perform_job(failed_job, queue)
        self.assertEqual(self.registry.get_job_ids(), [])


class TestDeferredRegistry(RQTestCase):
    def setUp(self):
        super().setUp()
        self.registry = DeferredJobRegistry(connection=self.connection)

    def test_key(self):
        self.assertEqual(self.registry.key, 'rq:deferred:default')

    def test_add(self):
        """Adding a job to DeferredJobsRegistry."""
        job = Job(connection=self.connection)
        self.registry.add(job)
        job_ids = [as_text(job_id) for job_id in self.connection.zrange(self.registry.key, 0, -1)]
        self.assertEqual(job_ids, [job.id])

    def test_add_with_deferred_ttl(self):
        """Job TTL defaults to +inf"""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        key = self.registry.key

        self.registry.add(job)
        score = self.connection.zscore(key, job.id)
        self.assertEqual(score, float('inf'))

        timestamp = current_timestamp()
        ttl = 5
        self.registry.add(job, ttl=ttl)
        score = self.connection.zscore(key, job.id)
        self.assertLess(score, timestamp + ttl + 2)
        self.assertGreater(score, timestamp + ttl - 2)

    def test_register_dependency(self):
        """Ensure job creation and deletion works with DeferredJobRegistry."""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)
        job2 = queue.enqueue(say_hello, depends_on=job)

        registry = DeferredJobRegistry(connection=self.connection)
        self.assertEqual(registry.get_job_ids(), [job2.id])

        # When deleted, job removes itself from DeferredJobRegistry
        job2.delete()
        self.assertEqual(registry.get_job_ids(), [])

    def test_cleanup_supports_deleted_jobs(self):
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)
        self.registry.add(job, ttl=10)

        self.assertEqual(self.registry.count, 1)
        job.delete(remove_from_queue=False)
        self.assertEqual(self.registry.count, 1)

        self.registry.cleanup(current_timestamp() + 100)
        self.assertEqual(self.registry.count, 0)

    def test_cleanup_moves_jobs_to_failed_job_registry(self):
        """Moving expired jobs to FailedJobRegistry."""
        queue = Queue(connection=self.connection)
        failed_job_registry = FailedJobRegistry(connection=self.connection)
        job = queue.enqueue(say_hello)

        self.connection.zadd(self.registry.key, {job.id: 2})

        # Job has not been moved to FailedJobRegistry
        self.registry.cleanup(1)
        self.assertNotIn(job, failed_job_registry)
        self.assertIn(job, self.registry)

        self.registry.cleanup()
        self.assertIn(job.id, failed_job_registry)
        self.assertNotIn(job, self.registry)
        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.FAILED)
        self.assertTrue(job.exc_info)  # explanation is written to exc_info


class TestFailedJobRegistry(RQTestCase):
    def test_default_failure_ttl(self):
        """Job TTL defaults to DEFAULT_FAILURE_TTL"""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        registry = FailedJobRegistry(connection=self.connection)
        key = registry.key

        timestamp = current_timestamp()
        registry.add(job)
        score = self.connection.zscore(key, job.id)
        self.assertLess(score, timestamp + DEFAULT_FAILURE_TTL + 2)
        self.assertGreater(score, timestamp + DEFAULT_FAILURE_TTL - 2)

        # Job key will also expire
        job_ttl = self.connection.ttl(job.key)
        self.assertLess(job_ttl, DEFAULT_FAILURE_TTL + 2)
        self.assertGreater(job_ttl, DEFAULT_FAILURE_TTL - 2)

        timestamp = current_timestamp()
        ttl = 5
        registry.add(job, ttl=ttl)
        score = self.connection.zscore(key, job.id)
        self.assertLess(score, timestamp + ttl + 2)
        self.assertGreater(score, timestamp + ttl - 2)

        job_ttl = self.connection.ttl(job.key)
        self.assertLess(job_ttl, ttl + 2)
        self.assertGreater(job_ttl, ttl - 2)

    def test_requeue(self):
        """FailedJobRegistry.requeue works properly"""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(div_by_zero, failure_ttl=5)

        worker = Worker([queue], connection=self.connection)
        worker.work(burst=True)

        registry = FailedJobRegistry(connection=worker.connection)
        self.assertIn(job, registry)

        registry.requeue(job.id)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)
        self.assertEqual(job.started_at, None)
        self.assertEqual(job.ended_at, None)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # Should also work with job instance
        registry.requeue(job)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # requeue_job should work the same way
        requeue_job(job.id, connection=self.connection)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # And so does job.requeue()
        job.requeue()
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

    def test_requeue_with_serializer(self):
        """FailedJobRegistry.requeue works properly (with serializer)"""
        queue = Queue(connection=self.connection, serializer=JSONSerializer)
        job = queue.enqueue(div_by_zero, failure_ttl=5)

        worker = Worker([queue], serializer=JSONSerializer, connection=self.connection)
        worker.work(burst=True)

        registry = FailedJobRegistry(connection=worker.connection, serializer=JSONSerializer)
        self.assertIn(job, registry)

        registry.requeue(job.id)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)
        self.assertEqual(job.started_at, None)
        self.assertEqual(job.ended_at, None)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # Should also work with job instance
        registry.requeue(job)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # requeue_job should work the same way
        requeue_job(job.id, connection=self.connection, serializer=JSONSerializer)
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

        worker.work(burst=True)
        self.assertIn(job, registry)

        # And so does job.requeue()
        job.requeue()
        self.assertNotIn(job, registry)
        self.assertIn(job.id, queue.get_job_ids())

        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.QUEUED)

    def test_invalid_job(self):
        """Requeuing a job that's not in FailedJobRegistry raises an error."""
        queue = Queue(connection=self.connection)
        job = queue.enqueue(say_hello)

        registry = FailedJobRegistry(connection=self.connection)
        with self.assertRaises(InvalidJobOperation):
            registry.requeue(job)

    def test_worker_handle_job_failure(self):
        """Failed jobs are added to FailedJobRegistry"""
        q = Queue(connection=self.connection)

        w = Worker([q], connection=self.connection)
        registry = FailedJobRegistry(connection=w.connection)

        timestamp = current_timestamp()

        job = q.enqueue(div_by_zero, failure_ttl=5)
        w.handle_job_failure(job, q)
        # job is added to FailedJobRegistry with default failure ttl
        self.assertIn(job.id, registry.get_job_ids())
        self.assertLess(self.connection.zscore(registry.key, job.id), timestamp + DEFAULT_FAILURE_TTL + 5)

        # job is added to FailedJobRegistry with specified ttl
        job = q.enqueue(div_by_zero, failure_ttl=5)
        w.handle_job_failure(job, q)
        self.assertLess(self.connection.zscore(registry.key, job.id), timestamp + 7)


class TestStartedJobRegistry(RQTestCase):
    def setUp(self):
        super().setUp()
        self.registry = StartedJobRegistry(connection=self.connection)
        self.queue = Queue(connection=self.connection)

    def test_job_deletion(self):
        """Ensure job is removed from StartedJobRegistry when deleted."""
        worker = Worker([self.queue], connection=self.connection)

        job = self.queue.enqueue(say_hello)
        self.assertTrue(job.is_queued)

        execution = worker.prepare_execution(job)
        worker.prepare_job_execution(job)
        self.assertIn(execution.job_id, self.registry.get_job_ids())

        pipeline = self.connection.pipeline()
        job.delete(pipeline=pipeline)
        pipeline.execute()
        self.assertNotIn(execution.job_id, self.registry.get_job_ids())

    def test_contains(self):
        """Test the StartedJobRegistry __contains__ method. It is slightly different
        because the entries in the registry are {job_id}:{execution_id} format."""
        job = self.queue.enqueue(say_hello)

        self.assertNotIn(job, self.registry)
        self.assertNotIn(job.id, self.registry)

        with self.connection.pipeline() as pipe:
            self.registry.add_execution(
                Execution(id='execution', job_id=job.id, connection=self.connection), pipeline=pipe, ttl=5
            )
            pipe.execute()
        self.assertIn(job, self.registry)
        self.assertIn(job.id, self.registry)

    def test_infinite_score(self):
        """Test the StartedJobRegistry __contains__ method. It is slightly different
        because the entries in the registry are {job_id}:{execution_id} format."""
        job = self.queue.enqueue(say_hello)

        self.assertNotIn(job, self.registry)
        self.assertNotIn(job.id, self.registry)

        with self.connection.pipeline() as pipe:
            execution = Execution(id='execution', job_id=job.id, connection=self.connection)
            self.registry.add_execution(execution=execution, pipeline=pipe, ttl=-1)
            pipe.execute()
        self.assertIn(job, self.registry)
        self.assertIn(job.id, self.registry)
        self.assertEqual(self.connection.zscore(self.registry.key, execution.composite_key), math.inf)

    def test_remove_executions(self):
        """Ensure all executions for a job are removed from registry."""
        worker = Worker([self.queue], connection=self.connection)
        job = self.queue.enqueue(say_hello)

        execution_1 = worker.prepare_execution(job)
        execution_2 = worker.prepare_execution(job)
        self.assertIn((job.id, execution_1.id), self.registry.get_job_and_execution_ids())
        self.assertIn((job.id, execution_2.id), self.registry.get_job_and_execution_ids())

        self.registry.remove_executions(job)

        self.assertNotIn((job.id, execution_1.id), self.registry.get_job_and_execution_ids())
        self.assertNotIn((job.id, execution_2.id), self.registry.get_job_and_execution_ids())

        job.delete()

    def test_job_execution(self):
        """Job is removed from StartedJobRegistry after execution."""
        worker = Worker([self.queue], connection=self.connection)

        job = self.queue.enqueue(say_hello)
        self.assertTrue(job.is_queued)
        execution = worker.prepare_execution(job)
        worker.prepare_job_execution(job)
        self.assertIn(job.id, self.registry.get_job_ids())
        self.assertIn((job.id, execution.id), self.registry.get_job_and_execution_ids())
        self.assertTrue(job.is_started)

        worker.perform_job(job, self.queue)
        self.assertNotIn(job.id, self.registry.get_job_ids())
        self.assertNotIn((job.id, execution.id), self.registry.get_job_and_execution_ids())
        self.assertTrue(job.is_finished)

        # Job that fails
        job = self.queue.enqueue(div_by_zero)
        execution = worker.prepare_execution(job)
        worker.prepare_job_execution(job)
        self.assertIn(job.id, self.registry.get_job_ids())
        self.assertIn((job.id, execution.id), self.registry.get_job_and_execution_ids())

        worker.perform_job(job, self.queue)
        self.assertNotIn(job.id, self.registry.get_job_ids())
        self.assertNotIn((job.id, execution.id), self.registry.get_job_and_execution_ids())

    def test_get_job_ids(self):
        """Getting job ids with cleanup."""
        timestamp = current_timestamp()
        self.connection.zadd(self.registry.key, {'will-be-cleaned-up:execution_id1': 1})
        self.connection.zadd(self.registry.key, {'foo:execution_id2': timestamp + 10})
        self.connection.zadd(self.registry.key, {'bar:execution_id3': timestamp + 20})
        self.assertEqual(self.registry.get_job_ids(), ['foo', 'bar'])

    def test_get_job_ids_does_not_cleanup(self):
        """Getting job ids without a cleanup."""
        timestamp = current_timestamp()
        self.connection.zadd(self.registry.key, {'will-be-returned-despite-outdated:execution_id1': 1})
        self.connection.zadd(self.registry.key, {'foo:execution_id2': timestamp + 10})
        self.connection.zadd(self.registry.key, {'bar:execution_id3': timestamp + 20})
        self.assertEqual(self.registry.get_job_ids(cleanup=False), ['will-be-returned-despite-outdated', 'foo', 'bar'])

    def test_count(self):
        """Return the right number of job count (cleanup should be performed)"""
        timestamp = current_timestamp() + 10
        self.connection.zadd(self.registry.key, {'will-be-cleaned-up': 1})
        self.connection.zadd(self.registry.key, {'foo': timestamp})
        self.connection.zadd(self.registry.key, {'bar': timestamp})
        self.assertEqual(self.registry.count, 2)
        self.assertEqual(len(self.registry), 2)

    def test_cleanup_moves_jobs_to_failed_job_registry(self):
        """Moving expired jobs to FailedJobRegistry."""

        failed_job_registry = FailedJobRegistry(connection=self.connection)
        job = self.queue.enqueue(say_hello)

        self.connection.zadd(self.registry.key, {f'{job.id}:execution_id': 100})

        # Job has not been moved to FailedJobRegistry
        self.registry.cleanup(1)
        self.assertNotIn(job, failed_job_registry)
        self.assertIn(job, self.registry)

        with mock.patch.object(Job, 'execute_failure_callback') as mocked:
            mock_handler = mock.MagicMock()
            mock_handler.return_value = False
            mock_handler_no_return = mock.MagicMock()
            mock_handler_no_return.return_value = None
            self.registry.cleanup(exception_handlers=[mock_handler_no_return, mock_handler])
            mocked.assert_called_once_with(self.queue.death_penalty_class, AbandonedJobError, ANY, ANY)
            mock_handler.assert_called_once_with(job, AbandonedJobError, ANY, ANY)
            mock_handler_no_return.assert_called_once_with(job, AbandonedJobError, ANY, ANY)
        self.assertIn(job.id, failed_job_registry)
        self.assertNotIn(job, self.registry)
        job.refresh()
        self.assertEqual(job.get_status(), JobStatus.FAILED)
        latest_result = job.latest_result()
        self.assertIsNotNone(latest_result)
        self.assertTrue(latest_result.exc_string)  # explanation is written to exc_info

    def test_enqueue_dependents_when_parent_job_is_abandoned(self):
        """Enqueuing parent job's dependencies after moving it to FailedJobRegistry due to AbandonedJobError."""
        queue = Queue(connection=self.connection)
        worker = Worker([queue])
        failed_job_registry = FailedJobRegistry(connection=self.connection)
        finished_job_registry = FinishedJobRegistry(connection=self.connection)
        deferred_job_registry = DeferredJobRegistry(connection=self.connection)

        parent_job = queue.enqueue(say_hello)
        job_to_be_executed = queue.enqueue_call(say_hello, depends_on=Dependency(jobs=[parent_job], allow_failure=True))
        job_not_to_be_executed = queue.enqueue_call(
            say_hello, depends_on=Dependency(jobs=[parent_job], allow_failure=False)
        )
        self.assertIn(job_to_be_executed, deferred_job_registry)
        self.assertIn(job_not_to_be_executed, deferred_job_registry)

        self.connection.zadd(self.registry.key, {f'{parent_job.id}:execution': 2})
        queue.remove(parent_job.id)

        with mock.patch.object(Job, 'execute_failure_callback') as mocked:
            self.registry.cleanup()
            mocked.assert_called_once_with(queue.death_penalty_class, AbandonedJobError, ANY, ANY)

        # check that parent job was moved to FailedJobRegistry and has correct status
        self.assertIn(parent_job, failed_job_registry)
        self.assertNotIn(parent_job, self.registry)
        self.assertTrue(parent_job.is_failed)

        # check that only job_to_be_executed has been queued and executed
        self.assertEqual(len(queue.get_job_ids()), 1)
        self.assertTrue(job_to_be_executed.is_queued)
        self.assertFalse(job_not_to_be_executed.is_queued)

        worker.work(burst=True)
        self.assertTrue(job_to_be_executed.is_finished)
        self.assertNotIn(job_to_be_executed, deferred_job_registry)
        self.assertIn(job_to_be_executed, finished_job_registry)

        self.assertFalse(job_not_to_be_executed.is_finished)
        self.assertNotIn(job_not_to_be_executed, finished_job_registry)

    def test_warnings_on_add_remove_and_exception(self):
        """Test backwards compatibility of the .add and .remove methods for
        the StartedJobRegistry."""
        with pytest.raises(NotImplementedError):
            self.registry.add('job_id')

        with pytest.raises(NotImplementedError):
            self.registry.remove('job_id')