forked from Bogdanp/dramatiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_canteen.py
46 lines (32 loc) · 1.2 KB
/
test_canteen.py
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
import multiprocessing
import pytest
from dramatiq.canteen import Canteen, canteen_add, canteen_get, canteen_try_init
def test_canteen_add_adds_paths():
# Given that I have a Canteen
c = multiprocessing.Value(Canteen)
# When I append a couple of paths and mark it ready
with canteen_try_init(c):
canteen_add(c, "hello")
canteen_add(c, "there")
# Then those paths should be stored in the canteen
assert canteen_get(c) == ["hello", "there"]
def test_canteen_add_fails_when_adding_too_many_paths():
# Given that I have a Canteen
c = Canteen()
# When I append too many paths
# Then a RuntimeError should be raised
with pytest.raises(RuntimeError):
for _ in range(1024):
canteen_add(c, "0" * 1024)
def test_canteen_try_init_runs_at_most_once():
# Given that I have a Canteen
c = multiprocessing.Value(Canteen)
# When I run two canteen_try_init blocks
with canteen_try_init(c) as acquired:
if acquired:
canteen_add(c, "hello")
with canteen_try_init(c) as acquired:
if acquired:
canteen_add(c, "goodbye")
# Then only the first one should run
assert canteen_get(c) == ["hello"]