2019-12-31 15:30:13 -05:00
|
|
|
from lbry.testcase import AsyncioTestCase
|
2020-05-01 09:34:34 -04:00
|
|
|
from lbry.event import EventController
|
|
|
|
from lbry.tasks import TaskGroup
|
2019-08-07 02:48:40 -03:00
|
|
|
|
2019-08-07 11:27:25 -03:00
|
|
|
|
|
|
|
class StreamControllerTestCase(AsyncioTestCase):
|
2020-05-01 09:34:34 -04:00
|
|
|
|
|
|
|
async def test_non_unique_events(self):
|
2019-08-07 02:48:40 -03:00
|
|
|
events = []
|
2020-05-01 09:34:34 -04:00
|
|
|
controller = EventController()
|
|
|
|
controller.stream.listen(events.append)
|
|
|
|
await controller.add("yo")
|
|
|
|
await controller.add("yo")
|
2019-10-06 00:12:01 +03:00
|
|
|
self.assertListEqual(events, ["yo", "yo"])
|
2019-08-07 02:48:40 -03:00
|
|
|
|
2020-05-01 09:34:34 -04:00
|
|
|
async def test_unique_events(self):
|
2019-08-07 02:48:40 -03:00
|
|
|
events = []
|
2020-05-01 09:34:34 -04:00
|
|
|
controller = EventController(merge_repeated_events=True)
|
|
|
|
controller.stream.listen(events.append)
|
|
|
|
await controller.add("yo")
|
|
|
|
await controller.add("yo")
|
2019-10-06 00:12:01 +03:00
|
|
|
self.assertListEqual(events, ["yo"])
|
2019-12-11 19:31:45 -03:00
|
|
|
|
2020-05-01 09:34:34 -04: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")
|
|
|
|
|
2019-12-11 19:31:45 -03:00
|
|
|
|
2019-12-11 22:10:39 -05:00
|
|
|
class TaskGroupTestCase(AsyncioTestCase):
|
|
|
|
|
|
|
|
async def test_cancel_sets_it_done(self):
|
2019-12-11 19:31:45 -03:00
|
|
|
group = TaskGroup()
|
|
|
|
group.cancel()
|
2019-12-11 23:33:34 -03:00
|
|
|
self.assertTrue(group.done.is_set())
|