2019-12-31 21:30:13 +01:00
|
|
|
from lbry.testcase import AsyncioTestCase
|
2020-05-01 15:34:34 +02:00
|
|
|
from lbry.event import EventController
|
|
|
|
from lbry.tasks import TaskGroup
|
2019-08-07 07:48:40 +02:00
|
|
|
|
2019-08-07 16:27:25 +02:00
|
|
|
|
|
|
|
class StreamControllerTestCase(AsyncioTestCase):
|
2020-05-01 15:34:34 +02:00
|
|
|
|
|
|
|
async def test_non_unique_events(self):
|
2019-08-07 07:48:40 +02:00
|
|
|
events = []
|
2020-05-01 15:34:34 +02:00
|
|
|
controller = EventController()
|
|
|
|
controller.stream.listen(events.append)
|
|
|
|
await controller.add("yo")
|
|
|
|
await controller.add("yo")
|
2019-10-05 23:12:01 +02:00
|
|
|
self.assertListEqual(events, ["yo", "yo"])
|
2019-08-07 07:48:40 +02:00
|
|
|
|
2020-05-01 15:34:34 +02:00
|
|
|
async def test_unique_events(self):
|
2019-08-07 07:48:40 +02:00
|
|
|
events = []
|
2020-05-01 15:34:34 +02:00
|
|
|
controller = EventController(merge_repeated_events=True)
|
|
|
|
controller.stream.listen(events.append)
|
|
|
|
await controller.add("yo")
|
|
|
|
await controller.add("yo")
|
2019-10-05 23:12:01 +02:00
|
|
|
self.assertListEqual(events, ["yo"])
|
2019-12-11 23:31:45 +01:00
|
|
|
|
2020-05-01 15:34:34 +02:00
|
|
|
async def test_sync_listener_errors(self):
|
|
|
|
def bad_listener(e):
|
|
|
|
raise ValueError('bad')
|
|
|
|
controller = EventController()
|
|
|
|
controller.stream.listen(bad_listener)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
await controller.add("yo")
|
|
|
|
|
|
|
|
async def test_async_listener_errors(self):
|
|
|
|
async def bad_listener(e):
|
|
|
|
raise ValueError('bad')
|
|
|
|
controller = EventController()
|
|
|
|
controller.stream.listen(bad_listener)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
await controller.add("yo")
|
|
|
|
|
2020-05-21 00:01:34 +02:00
|
|
|
async def test_first_event(self):
|
|
|
|
controller = EventController()
|
|
|
|
first = controller.stream.first
|
|
|
|
await controller.add("one")
|
|
|
|
second = controller.stream.first
|
|
|
|
await controller.add("two")
|
|
|
|
self.assertEqual("one", await first)
|
|
|
|
self.assertEqual("two", await second)
|
|
|
|
|
|
|
|
async def test_last_event(self):
|
|
|
|
controller = EventController()
|
|
|
|
last = controller.stream.last
|
|
|
|
await controller.add("one")
|
|
|
|
await controller.add("two")
|
|
|
|
await controller.close()
|
|
|
|
self.assertEqual("two", await last)
|
|
|
|
|
2019-12-11 23:31:45 +01:00
|
|
|
|
2019-12-12 04:10:39 +01:00
|
|
|
class TaskGroupTestCase(AsyncioTestCase):
|
|
|
|
|
|
|
|
async def test_cancel_sets_it_done(self):
|
2019-12-11 23:31:45 +01:00
|
|
|
group = TaskGroup()
|
|
|
|
group.cancel()
|
2019-12-12 03:33:34 +01:00
|
|
|
self.assertTrue(group.done.is_set())
|