forked from aio-libs/janus
/
test_mixed.py
187 lines (139 loc) · 5.15 KB
/
test_mixed.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
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
import asyncio
import concurrent.futures
import unittest
from unittest import mock
import janus
class TestMixedMode(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
self.loop.set_default_executor(self.executor)
asyncio.set_event_loop(None)
def tearDown(self):
self.executor.shutdown()
self.loop.close()
def test_ctor_loop(self):
loop = mock.Mock()
q = janus.Queue(loop=loop)
self.assertIs(q._loop, loop)
q = janus.Queue(loop=self.loop)
self.assertIs(q._loop, self.loop)
def test_ctor_noloop(self):
asyncio.set_event_loop(self.loop)
q = janus.Queue()
self.assertIs(q._loop, self.loop)
def test_maxsize(self):
q = janus.Queue(5, loop=self.loop)
self.assertIs(5, q.maxsize)
def test_maxsize_named_param(self):
q = janus.Queue(maxsize=7, loop=self.loop)
self.assertIs(7, q.maxsize)
def test_maxsize_default(self):
q = janus.Queue(loop=self.loop)
self.assertIs(0, q.maxsize)
def test_unfinished(self):
q = janus.Queue(loop=self.loop)
self.assertEqual(q.sync_q.unfinished_tasks, 0)
self.assertEqual(q.async_q.unfinished_tasks, 0)
q.sync_q.put(1)
self.assertEqual(q.sync_q.unfinished_tasks, 1)
self.assertEqual(q.async_q.unfinished_tasks, 1)
q.sync_q.get()
self.assertEqual(q.sync_q.unfinished_tasks, 1)
self.assertEqual(q.async_q.unfinished_tasks, 1)
q.sync_q.task_done()
self.assertEqual(q.sync_q.unfinished_tasks, 0)
self.assertEqual(q.async_q.unfinished_tasks, 0)
def test_sync_put_async_get(self):
q = janus.Queue(loop=self.loop)
def threaded():
for i in range(5):
q.sync_q.put(i)
@asyncio.coroutine
def go():
f = self.loop.run_in_executor(None, threaded)
for i in range(5):
val = yield from q.async_q.get()
self.assertEqual(val, i)
self.assertTrue(q.async_q.empty())
yield from f
for i in range(3):
self.loop.run_until_complete(go())
def test_async_put_sync_get(self):
q = janus.Queue(loop=self.loop)
def threaded():
for i in range(5):
val = q.sync_q.get()
self.assertEqual(val, i)
@asyncio.coroutine
def go():
f = self.loop.run_in_executor(None, threaded)
for i in range(5):
yield from q.async_q.put(i)
yield from f
self.assertTrue(q.async_q.empty())
for i in range(3):
self.loop.run_until_complete(go())
def test_sync_join_async_done(self):
q = janus.Queue(loop=self.loop)
def threaded():
for i in range(5):
q.sync_q.put(i)
q.sync_q.join()
@asyncio.coroutine
def go():
f = self.loop.run_in_executor(None, threaded)
for i in range(5):
val = yield from q.async_q.get()
self.assertEqual(val, i)
q.async_q.task_done()
self.assertTrue(q.async_q.empty())
yield from f
for i in range(3):
self.loop.run_until_complete(go())
def test_async_join_async_done(self):
q = janus.Queue(loop=self.loop)
def threaded():
for i in range(5):
val = q.sync_q.get()
self.assertEqual(val, i)
q.sync_q.task_done()
@asyncio.coroutine
def go():
f = self.loop.run_in_executor(None, threaded)
for i in range(5):
yield from q.async_q.put(i)
yield from q.async_q.join()
yield from f
self.assertTrue(q.async_q.empty())
for i in range(3):
self.loop.run_until_complete(go())
def test_wait_without_closing(self):
q = janus.Queue(loop=self.loop)
with self.assertRaises(RuntimeError):
self.loop.run_until_complete(q.wait_closed())
q.close()
self.loop.run_until_complete(q.wait_closed())
def test_modifying_forbidden_after_closing(self):
q = janus.Queue(loop=self.loop)
q.close()
with self.assertRaises(RuntimeError):
q.sync_q.put(5)
with self.assertRaises(RuntimeError):
q.sync_q.get()
with self.assertRaises(RuntimeError):
q.sync_q.task_done()
with self.assertRaises(RuntimeError):
self.loop.run_until_complete(q.async_q.put(5))
with self.assertRaises(RuntimeError):
q.async_q.put_nowait(5)
with self.assertRaises(RuntimeError):
q.async_q.get_nowait()
with self.assertRaises(RuntimeError):
self.loop.run_until_complete(q.sync_q.task_done())
self.loop.run_until_complete(q.wait_closed())
def test_double_closing(self):
q = janus.Queue(loop=self.loop)
q.close()
q.close()
self.loop.run_until_complete(q.wait_closed())