예제 #1
0
    def test_cancel_within_coro(self):
        async def nuke_loop():
            return loop.cancel()

        loop = Loop()
        loop.gather(nuke_loop())

        with loop as lo:
            result = lo.run_until_complete()

        self.assertIsNone(result)
예제 #2
0
    def test_raise_exception_instead_of_result(self):
        async def raise_exc():
            raise Exception

        with self.assertRaises(Exception):
            with Loop(raise_exc()) as loop:
                loop.run_until_complete()
예제 #3
0
    def test_run_loop_with_futures(self):
        with Loop(wait_for_it(0.005), wait_for_it(0.002)) as loop:
            result = loop.run_until_complete()

        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 'success with wait 0.005')
        self.assertEqual(result[1], 'success with wait 0.002')
예제 #4
0
    def test_publish_to_empty_event(self):
        event = Event('I am empty inside :(')

        self.assertEqual(len(event.pub_sub), 0)

        with Loop(event.publish('enter the void')) as loop:
            result = loop.run_until_complete()

        self.assertListEqual(result, [])
예제 #5
0
    def test_return_exception_as_result(self):
        exc = Exception('fail')

        async def raise_exc():
            raise exc

        with Loop(raise_exc(), return_exceptions=True) as loop:
            result = loop.run_until_complete()

        self.assertEqual(result, exc)
예제 #6
0
    def test_call_function_handler(self):
        async def test_func(message, publisher, event):
            return [message, publisher, event]

        sub = Subscriber(test_func)
        with Loop(sub('some message', None, 'test')) as loop:
            result = loop.run_until_complete()

        self.assertEqual(result[0], 'some message')
        self.assertIsNone(result[1])
        self.assertEqual(result[2], 'test')
예제 #7
0
    def test_call_class_handler(self):
        class TestCls:
            async def __call__(self, message, publisher, event):
                return [message, publisher, event]

        sub = Subscriber(TestCls())
        with Loop(sub('some message', None, 'test')) as loop:
            result = loop.run_until_complete()

        self.assertEqual(result[0], 'some message')
        self.assertIsNone(result[1])
        self.assertEqual(result[2], 'test')
예제 #8
0
    def test_gather_single_future(self):
        loop = Loop()
        self.assertIsNone(loop.futures)

        loop.gather(wait_for_it(0.001))
        self.assertIsInstance(loop.futures, asyncio.Future)
        loop.run_until_complete()
예제 #9
0
    def test_publish_on_disabled_event(self):
        event = Event('disabled')
        event.disable()

        # noinspection PyShadowingNames
        @event.subscribe()
        async def funny_handler(message, publisher, event):
            return [message, publisher, event]

        with Loop(event.publish('sad message')) as loop:
            result = loop.run_until_complete()

        self.assertIsNone(result)
예제 #10
0
    def test_publish_to_all(self):
        event = Event('publish to all')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def first_all(message, publisher, event):
            return [message, publisher, event]

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def second_all(message, publisher, event):
            return [message, publisher, event]

        with Loop(event.publish('test message')) as loop:
            result = loop.run_until_complete()

        self.assertEqual(len(result), 2)
        for r in result:
            self.assertEqual(r[0], 'test message')
            self.assertIsNone(r[1])
            self.assertEqual(r[2], 'publish to all')
예제 #11
0
    def test_publish_to_all_but_specific(self):
        event = Event('publish to all but omit')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe(publisher=Publisher('omit'))
        async def first_sp(message, publisher, event):
            return ['omitted', publisher, event]

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def second_sp(message, publisher, event):
            return ['received', publisher, event]

        with Loop(event.publish('secret message',
                                Publisher('broadcast'))) as loop:
            result = loop.run_until_complete()

        self.assertEqual(len(result), 1)
        result = result.pop()
        self.assertEqual(result[0], 'received')
        self.assertEqual(result[1], Publisher('broadcast'))
        self.assertEqual(result[2], 'publish to all but omit')
예제 #12
0
    def test_run_loop_with_single_future(self):
        with Loop(wait_for_it(0.003)) as loop:
            result = loop.run_until_complete()

        self.assertEqual(result, 'success with wait 0.003')
예제 #13
0
 def test_auto_gather_single_through_constructor(self):
     loop = Loop(wait_for_it(0.001), wait_for_it(0.001))
     self.assertIsInstance(loop.futures, asyncio.Future)
     loop.run_until_complete()
예제 #14
0
 def test_get_existing_event_loop(self):
     existing_loop = asyncio.new_event_loop()
     loop = Loop._get_event_loop(existing_loop)
     self.assertIs(loop, existing_loop)