This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
|
||||
from src.pwo import retry, async_retry, async_test
|
||||
from pwo import retry, async_retry, async_test, AsyncQueueIterator, aenumerate
|
||||
from asyncio import Queue
|
||||
|
||||
|
||||
class PrivateTest(unittest.TestCase):
|
||||
@@ -70,5 +71,25 @@ class PrivateTest(unittest.TestCase):
|
||||
await bar()
|
||||
self.assertEqual(max_attempts, attempt)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@async_test
|
||||
async def test_async_queue_iterator(self):
|
||||
queue = Queue()
|
||||
queue_size = 10
|
||||
objects = [object() for _ in range(queue_size)]
|
||||
|
||||
async def poll() -> int:
|
||||
completed = 0
|
||||
async for i, obj in aenumerate(AsyncQueueIterator(queue)):
|
||||
self.assertIs(objects[i], obj)
|
||||
completed += 1
|
||||
return completed
|
||||
|
||||
handle = poll()
|
||||
|
||||
for o in objects:
|
||||
queue.put_nowait(o)
|
||||
queue.put_nowait(None)
|
||||
processed = await handle
|
||||
self.assertEqual(queue_size, processed)
|
||||
|
||||
|
||||
|
42
tests/test_try.py
Normal file
42
tests/test_try.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import unittest
|
||||
from pwo import Try
|
||||
|
||||
|
||||
class TestException(Exception):
|
||||
|
||||
def __init__(self, msg: str):
|
||||
super().__init__(msg)
|
||||
|
||||
|
||||
class TryTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_try(self):
|
||||
|
||||
with self.subTest("Test failure"):
|
||||
def throw_test_exception():
|
||||
raise TestException("error")
|
||||
|
||||
t = Try.of(throw_test_exception)
|
||||
|
||||
with self.assertRaises(TestException):
|
||||
t.get()
|
||||
|
||||
t = Try.failure(TestException("error"))
|
||||
|
||||
with self.assertRaises(TestException):
|
||||
t.get()
|
||||
|
||||
with self.subTest("Test success"):
|
||||
def complete_successfully():
|
||||
return 42
|
||||
|
||||
t = Try.of(complete_successfully)
|
||||
|
||||
self.assertEqual(42, t.get())
|
||||
|
||||
t2 = t.handle(lambda value, err: value * 2)
|
||||
self.assertEqual(84, t2.get())
|
||||
|
Reference in New Issue
Block a user