Пример #1
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, [])
Пример #2
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)
Пример #3
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')
Пример #4
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')