Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasst committed Apr 24, 2024
1 parent b86d469 commit d8d0c72
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions tests/test_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tasktiger import Task, Worker
from tasktiger._internal import ACTIVE
from tasktiger.executor import SyncExecutor
from tasktiger.worker import LOCK_REDIS_KEY

from .config import DELAY
from .tasks import (
Expand Down Expand Up @@ -189,7 +190,8 @@ def test_handles_timeout(self, tiger, ensure_queues):

def test_heartbeat(self, tiger):
# Test both task heartbeat and lock renewal.
task = Task(tiger, sleep_task, lock=True)
# We set unique=True so the task ID matches the lock key.
task = Task(tiger, sleep_task, lock=True, unique=True)
task.delay()

# Start a worker and wait until it starts processing.
Expand All @@ -208,11 +210,31 @@ def test_heartbeat(self, tiger):

time.sleep(DELAY)

key = tiger._key(ACTIVE, "default")
queue_key = tiger._key(ACTIVE, "default")
queue_lock_key = tiger._key(LOCK_REDIS_KEY, "default")
task_lock_key = tiger._key("lockv2", task.id)

conn = tiger.connection
heartbeat_1 = conn.zscore(key, task.id)

heartbeat_1 = conn.zscore(queue_key, task.id)
queue_lock_1 = conn.zrange(queue_lock_key, 0, -1, withscores=True)[0][
1
]
task_lock_1 = conn.pttl(task_lock_key)

time.sleep(DELAY / 2)
heartbeat_2 = conn.zscore(key, task.id)

heartbeat_2 = conn.zscore(queue_key, task.id)
queue_lock_2 = conn.zrange(queue_lock_key, 0, -1, withscores=True)[0][
1
]
task_lock_2 = conn.pttl(task_lock_key)

assert heartbeat_2 > heartbeat_1 > 0
assert queue_lock_2 > queue_lock_1 > 0

# Active task update timeout is 2 * DELAY and we renew every DELAY / 2.
assert task_lock_1 > DELAY
assert task_lock_2 > DELAY

worker.kill()

0 comments on commit d8d0c72

Please sign in to comment.