forked from Bogdanp/dramatiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): use a context manager for workers to avoid leaking them
- Loading branch information
Showing
4 changed files
with
68 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from contextlib import contextmanager | ||
from dramatiq import Worker | ||
|
||
|
||
@contextmanager | ||
def worker(*args, **kwargs): | ||
try: | ||
worker = Worker(*args, **kwargs) | ||
worker.start() | ||
yield worker | ||
finally: | ||
worker.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
from dramatiq import Worker | ||
from .common import worker | ||
|
||
|
||
def test_workers_dont_register_queues_that_arent_whitelisted(stub_broker): | ||
# Given that I have a worker object with a restricted set of queues | ||
worker = Worker(stub_broker, queues={"a", "b"}) | ||
worker.start() | ||
|
||
try: | ||
with worker(stub_broker, queues={"a", "b"}) as stub_worker: | ||
# When I try to register a consumer for a queue that hasn't been whitelisted | ||
stub_broker.declare_queue("c") | ||
stub_broker.declare_queue("c.DQ") | ||
|
||
# Then a consumer should not get spun up for that queue | ||
assert "c" not in worker.consumers | ||
assert "c.DQ" not in worker.consumers | ||
finally: | ||
worker.stop() | ||
assert "c" not in stub_worker.consumers | ||
assert "c.DQ" not in stub_worker.consumers |