File size: 6,533 Bytes
3f7cfab |
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import unittest
import asyncio
from iterators import AsyncTimeoutIterator
async def iter_simple():
yield 1
yield 2
async def iter_with_sleep():
yield 1
await asyncio.sleep(0.6)
yield 2
await asyncio.sleep(0.4)
yield 3
async def iter_with_exception():
yield 1
yield 2
raise Exception
yield 3
class TestTimeoutIterator(unittest.TestCase):
def test_normal_iteration(self):
async def _(self):
i = iter_simple()
it = AsyncTimeoutIterator(i)
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), 2)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_normal_iteration_for_loop(self):
async def _(self):
i = iter_simple()
it = AsyncTimeoutIterator(i)
iterResults = []
async for x in it:
iterResults.append(x)
self.assertEqual(iterResults, [1,2])
asyncio.get_event_loop().run_until_complete(_(self))
def test_timeout_block(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i)
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_timeout_block_for_loop(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i)
iterResults = []
async for x in it:
iterResults.append(x)
self.assertEqual(iterResults, [1,2,3])
asyncio.get_event_loop().run_until_complete(_(self))
def test_fixed_timeout(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5)
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), it.get_sentinel())
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_fixed_timeout(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5)
iterResults = []
async for x in it:
iterResults.append(x)
self.assertEqual(iterResults, [1,it.get_sentinel(),2,3])
asyncio.get_event_loop().run_until_complete(_(self))
def test_timeout_update(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5)
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), it.get_sentinel())
it.set_timeout(0.3)
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), it.get_sentinel())
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_custom_sentinel(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5, sentinel="END")
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), "END")
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_feature_timeout_reset(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5, reset_on_next=True)
self.assertEqual(await it.__anext__(), 1) # timeout gets reset after first iteration
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_function_set_reset_on_next(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.35, reset_on_next=False)
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), it.get_sentinel())
it.set_reset_on_next(True)
self.assertEqual(await it.__anext__(), 2)
self.assertEqual(await it.__anext__(), 3)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_iterator_raises_exception(self):
async def _(self):
i = iter_with_exception()
it = AsyncTimeoutIterator(i, timeout=0.5, sentinel="END")
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), 2)
with self.assertRaises(Exception):
await it.__anext__()
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
def test_interrupt_thread(self):
async def _(self):
i = iter_with_sleep()
it = AsyncTimeoutIterator(i, timeout=0.5, sentinel="END")
self.assertEqual(await it.__anext__(), 1)
self.assertEqual(await it.__anext__(), it.get_sentinel())
it.interrupt()
self.assertEqual(await it.__anext__(), 2)
with self.assertRaises(StopAsyncIteration):
await it.__anext__()
asyncio.get_event_loop().run_until_complete(_(self))
|