Debian Codegraph @

t/unit/utils/test_threads.py

raw
1 from unittest.mock import patch
2
3 import pytest
4
5 from celery.utils.threads import Local, LocalManager, _FastLocalStack, _LocalStack, bgThread
6 from t.unit import conftest
7
8
9 class test_bgThread:
10
11 def test_crash(self):
12
13 class T(bgThread):
14
15 def body(self):
16 raise KeyError()
17
18 with patch('os._exit') as _exit:
19 with conftest.stdouts():
20 _exit.side_effect = ValueError()
21 t = T()
22 with pytest.raises(ValueError):
23 t.run()
24 _exit.assert_called_with(1)
25
26 def test_interface(self):
27 x = bgThread()
28 with pytest.raises(NotImplementedError):
29 x.body()
30
31
32 class test_Local:
33
34 def test_iter(self):
35 x = Local()
36 x.foo = 'bar'
37 ident = x.__ident_func__()
38 assert (ident, {'foo': 'bar'}) in list(iter(x))
39
40 delattr(x, 'foo')
41 assert (ident, {'foo': 'bar'}) not in list(iter(x))
42 with pytest.raises(AttributeError):
43 delattr(x, 'foo')
44
45 assert x(lambda: 'foo') is not None
46
47
48 class test_LocalStack:
49
50 def test_stack(self):
51 x = _LocalStack()
52 assert x.pop() is None
53 x.__release_local__()
54 ident = x.__ident_func__
55 x.__ident_func__ = ident
56
57 with pytest.raises(RuntimeError):
58 x()[0]
59
60 x.push(['foo'])
61 assert x()[0] == 'foo'
62 x.pop()
63 with pytest.raises(RuntimeError):
64 x()[0]
65
66
67 class test_FastLocalStack:
68
69 def test_stack(self):
70 x = _FastLocalStack()
71 x.push(['foo'])
72 x.push(['bar'])
73 assert x.top == ['bar']
74 assert len(x) == 2
75 x.pop()
76 assert x.top == ['foo']
77 x.pop()
78 assert x.top is None
79
80
81 class test_LocalManager:
82
83 def test_init(self):
84 x = LocalManager()
85 assert x.locals == []
86 assert x.ident_func
87
88 def ident():
89 return 1
90 loc = Local()
91 x = LocalManager([loc], ident_func=ident)
92 assert x.locals == [loc]
93 x = LocalManager(loc, ident_func=ident)
94 assert x.locals == [loc]
95 assert x.ident_func is ident
96 assert x.locals[0].__ident_func__ is ident
97 assert x.get_ident() == 1
98
99 with patch('celery.utils.threads.release_local') as release:
100 x.cleanup()
101 release.assert_called_with(loc)
102
103 assert repr(x)