Exemplo n.º 1
0
    def test_cg_subscribe_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")

        pubnub = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        cg_operation = NonSubscribeListener()

        pubnub.add_channel_to_channel_group()\
            .channel_group(gr)\
            .channels(ch)\
            .async(cg_operation.callback)
        result = cg_operation.await_result()
        assert isinstance(result, PNChannelGroupsAddChannelResult)
        cg_operation.reset()

        time.sleep(1)

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()
        callback_messages.wait_for_connect()

        pubnub.unsubscribe().channel_groups(gr).execute()
        callback_messages.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group()\
            .channel_group(gr)\
            .channels(ch)\
            .async(cg_operation.callback)
        result = cg_operation.await_result()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
Exemplo n.º 2
0
async def test_timeout_event_on_broken_heartbeat(event_loop):
    ch = helper.gen_channel("heartbeat-test")

    pubnub = PubNubAsyncio(messenger_config, custom_event_loop=event_loop)
    pubnub_listener = PubNubAsyncio(listener_config,
                                    custom_event_loop=event_loop)

    pubnub.config.uuid = helper.gen_channel("messenger")
    pubnub_listener.config.uuid = helper.gen_channel("listener")

    # - connect to :ch-pnpres
    callback_presence = SubscribeListener()
    pubnub_listener.add_listener(callback_presence)
    pubnub_listener.subscribe().channels(ch).with_presence().execute()
    await callback_presence.wait_for_connect()

    envelope = await callback_presence.wait_for_presence_on(ch)
    assert ch == envelope.channel
    assert 'join' == envelope.event
    assert pubnub_listener.uuid == envelope.uuid

    # - connect to :ch
    callback_messages = SubscribeListener()
    pubnub.add_listener(callback_messages)
    pubnub.subscribe().channels(ch).execute()

    useless_connect_future = callback_messages.wait_for_connect()
    presence_future = asyncio.ensure_future(
        callback_presence.wait_for_presence_on(ch))

    # - assert join event
    await asyncio.wait([useless_connect_future, presence_future])

    prs_envelope = presence_future.result()

    assert ch == prs_envelope.channel
    assert 'join' == prs_envelope.event
    assert pubnub.uuid == prs_envelope.uuid

    # wait for one heartbeat call
    await asyncio.sleep(8)

    # - break messenger heartbeat loop
    pubnub._subscription_manager._stop_heartbeat_timer()

    # - assert for timeout
    envelope = await callback_presence.wait_for_presence_on(ch)
    assert ch == envelope.channel
    assert 'timeout' == envelope.event
    assert pubnub.uuid == envelope.uuid

    pubnub.unsubscribe().channels(ch).execute()
    await callback_messages.wait_for_disconnect()

    # - disconnect from :ch-pnpres
    pubnub_listener.unsubscribe().channels(ch).execute()
    await callback_presence.wait_for_disconnect()

    await pubnub.stop()
    await pubnub_listener.stop()
Exemplo n.º 3
0
    def test_cg_subscribe_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")

        pubnub = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        cg_operation = NonSubscribeListener()

        pubnub.add_channel_to_channel_group()\
            .channel_group(gr)\
            .channels(ch)\
            .pn_async(cg_operation.callback)
        result = cg_operation.await_result()
        assert isinstance(result, PNChannelGroupsAddChannelResult)
        cg_operation.reset()

        time.sleep(1)

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()
        callback_messages.wait_for_connect()

        pubnub.unsubscribe().channel_groups(gr).execute()
        callback_messages.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group()\
            .channel_group(gr)\
            .channels(ch)\
            .pn_async(cg_operation.callback)
        result = cg_operation.await_result()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
Exemplo n.º 4
0
    def test_subscribe_cg_join_leave(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")

        pubnub = PubNub(pnconf_sub_copy())
        pubnub_listener = PubNub(pnconf_sub_copy())
        non_subscribe_listener = NonSubscribeListener()

        pubnub.add_channel_to_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsAddChannelResult)

        time.sleep(1)

        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

        pubnub_listener.add_listener(callback_presence)
        pubnub_listener.subscribe().channel_groups(gr).with_presence().execute()
        callback_presence.wait_for_connect()

        prs_envelope = callback_presence.wait_for_presence_on(ch)
        assert prs_envelope.event == 'join'
        assert prs_envelope.uuid == pubnub_listener.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()

        prs_envelope = callback_presence.wait_for_presence_on(ch)

        assert prs_envelope.event == 'join'
        assert prs_envelope.uuid == pubnub.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub.unsubscribe().channel_groups(gr).execute()
        prs_envelope = callback_presence.wait_for_presence_on(ch)

        assert prs_envelope.event == 'leave'
        assert prs_envelope.uuid == pubnub.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub_listener.unsubscribe().channel_groups(gr).execute()
        callback_presence.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
        pubnub_listener.stop()
Exemplo n.º 5
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("wherenow-asyncio-channel")
        uuid = helper.gen_channel("wherenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        where_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.where_now() \
            .uuid(uuid) \
            .pn_async(where_now_listener.callback)

        if where_now_listener.pn_await() is False:
            self.fail("WhereNow operation timeout")

        result = where_now_listener.result
        channels = result.channels

        assert len(channels) == 1
        assert channels[0] == ch

        pubnub.unsubscribe().channels(ch).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
Exemplo n.º 6
0
    def test_multiple_channels(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch1 = helper.gen_channel("here-now-native-sync-ch1")
        ch2 = helper.gen_channel("here-now-native-sync-ch2")
        pubnub.config.uuid = "here-now-native-sync-uuid"

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(5)

        pubnub.here_now() \
            .channels([ch1, ch2]) \
            .pn_async(here_now_listener.callback)

        if here_now_listener.pn_await() is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 2
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid
        assert channels[1].occupancy == 1
        assert channels[1].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels([ch1, ch2]).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
Exemplo n.º 7
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("herenow-asyncio-channel")
        uuid = helper.gen_channel("herenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.here_now() \
            .channels(ch) \
            .include_uuids(True) \
            .pn_async(here_now_listener.callback)

        if here_now_listener.pn_await() is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 1
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels(ch).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
Exemplo n.º 8
0
def test_timeout_event_on_broken_heartbeat(event_loop):
    ch = helper.gen_channel("heartbeat-test")

    pubnub = PubNubAsyncio(messenger_config, custom_event_loop=event_loop)
    pubnub_listener = PubNubAsyncio(listener_config, custom_event_loop=event_loop)

    pubnub.config.uuid = helper.gen_channel("messenger")
    pubnub_listener.config.uuid = helper.gen_channel("listener")

    # - connect to :ch-pnpres
    callback_presence = SubscribeListener()
    pubnub_listener.add_listener(callback_presence)
    pubnub_listener.subscribe().channels(ch).with_presence().execute()
    yield from callback_presence.wait_for_connect()

    envelope = yield from callback_presence.wait_for_presence_on(ch)
    assert ch == envelope.channel
    assert 'join' == envelope.event
    assert pubnub_listener.uuid == envelope.uuid

    # - connect to :ch
    callback_messages = SubscribeListener()
    pubnub.add_listener(callback_messages)
    pubnub.subscribe().channels(ch).execute()

    useless_connect_future = callback_messages.wait_for_connect()
    presence_future = asyncio.ensure_future(callback_presence.wait_for_presence_on(ch))

    # - assert join event
    yield from asyncio.wait([useless_connect_future, presence_future])

    prs_envelope = presence_future.result()

    assert ch == prs_envelope.channel
    assert 'join' == prs_envelope.event
    assert pubnub.uuid == prs_envelope.uuid

    # wait for one heartbeat call
    yield from asyncio.sleep(8)

    # - break messenger heartbeat loop
    pubnub._subscription_manager._stop_heartbeat_timer()

    # - assert for timeout
    envelope = yield from callback_presence.wait_for_presence_on(ch)
    assert ch == envelope.channel
    assert 'timeout' == envelope.event
    assert pubnub.uuid == envelope.uuid

    pubnub.unsubscribe().channels(ch).execute()
    yield from callback_messages.wait_for_disconnect()

    # - disconnect from :ch-pnpres
    pubnub_listener.unsubscribe().channels(ch).execute()
    yield from callback_presence.wait_for_disconnect()

    pubnub.stop()
    pubnub_listener.stop()
Exemplo n.º 9
0
    def test_timeout_event_on_broken_heartbeat(self):
        ch = helper.gen_channel("heartbeat-test")

        pubnub = PubNub(messenger_config)
        pubnub_listener = PubNub(listener_config)

        pubnub.config.uuid = helper.gen_channel("messenger")
        pubnub_listener.config.uuid = helper.gen_channel("listener")

        callback_presence = SubscribeListener()
        callback_messages = SubscribeListener()

        # - connect to :ch-pnpres
        pubnub_listener.add_listener(callback_presence)
        pubnub_listener.subscribe().channels(ch).with_presence().execute()
        callback_presence.wait_for_connect()

        presence_message = callback_presence.wait_for_presence_on(ch)
        assert ch == presence_message.channel
        assert 'join' == presence_message.event
        assert pubnub_listener.uuid == presence_message.uuid

        # - connect to :ch
        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channels(ch).execute()
        callback_messages.wait_for_connect()

        prs_envelope = callback_presence.wait_for_presence_on(ch)
        assert ch == prs_envelope.channel
        assert 'join' == prs_envelope.event
        assert pubnub.uuid == prs_envelope.uuid

        # wait for one heartbeat call
        time.sleep(6)

        # - break messenger heartbeat loop
        pubnub._subscription_manager._stop_heartbeat_timer()

        # - assert for timeout
        presence_message = callback_presence.wait_for_presence_on(ch)
        assert ch == presence_message.channel
        assert 'timeout' == presence_message.event
        assert pubnub.uuid == presence_message.uuid

        pubnub.unsubscribe().channels(ch).execute()
        callback_messages.wait_for_disconnect()

        # - disconnect from :ch-pnpres
        pubnub_listener.unsubscribe().channels(ch).execute()
        callback_presence.wait_for_disconnect()

        pubnub.stop()
        pubnub_listener.stop()
Exemplo n.º 10
0
    def test_subscribe_pub_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-sub-pub-unsub")
        pubnub = PubNub(pnconf_sub_copy())
        subscribe_listener = SubscribeListener()
        publish_operation = NonSubscribeListener()
        message = "hey"

        try:
            pubnub.add_listener(subscribe_listener)

            pubnub.subscribe().channels(ch).execute()
            subscribe_listener.wait_for_connect()

            pubnub.publish().channel(ch).message(message).pn_async(
                publish_operation.callback)

            if publish_operation. await () is False:
                self.fail("Publish operation timeout")

            publish_result = publish_operation.result
            assert isinstance(publish_result, PNPublishResult)
            assert publish_result.timetoken > 0

            result = subscribe_listener.wait_for_message_on(ch)
            assert isinstance(result, PNMessageResult)
            assert result.channel == ch
            assert result.subscription is None
            assert result.timetoken > 0
            assert result.message == message

            pubnub.unsubscribe().channels(ch).execute()
            subscribe_listener.wait_for_disconnect()
Exemplo n.º 11
0
    def test_subscribe_unsubscribe(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("test-subscribe-sub-unsub")

        try:
            listener = SubscribeListener()
            pubnub.add_listener(listener)

            pubnub.subscribe().channels(ch).execute()
            assert ch in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 1

            listener.wait_for_connect()
            assert ch in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 1

            pubnub.unsubscribe().channels(ch).execute()
            assert ch not in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 0

            listener.wait_for_disconnect()
            assert ch not in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 0

        except PubNubException as e:
            self.fail(e)
        finally:
            pubnub.stop()
Exemplo n.º 12
0
    def test_subscribe_unsubscribe(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("test-subscribe-sub-unsub")

        try:
            listener = SubscribeListener()
            pubnub.add_listener(listener)

            pubnub.subscribe().channels(ch).execute()
            assert ch in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 1

            listener.wait_for_connect()
            assert ch in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 1

            pubnub.unsubscribe().channels(ch).execute()
            assert ch not in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 0

            listener.wait_for_disconnect()
            assert ch not in pubnub.get_subscribed_channels()
            assert len(pubnub.get_subscribed_channels()) == 0

        except PubNubException as e:
            self.fail(e)
        finally:
            pubnub.stop()
Exemplo n.º 13
0
    def test_subscribe_pub_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-sub-pub-unsub")
        pubnub = PubNub(pnconf_sub_copy())
        subscribe_listener = SubscribeListener()
        publish_operation = NonSubscribeListener()
        message = "hey"

        try:
            pubnub.add_listener(subscribe_listener)

            pubnub.subscribe().channels(ch).execute()
            subscribe_listener.wait_for_connect()

            pubnub.publish().channel(ch).message(message).async(publish_operation.callback)

            if publish_operation.await() is False:
                self.fail("Publish operation timeout")

            publish_result = publish_operation.result
            assert isinstance(publish_result, PNPublishResult)
            assert publish_result.timetoken > 0

            result = subscribe_listener.wait_for_message_on(ch)
            assert isinstance(result, PNMessageResult)
            assert result.channel == ch
            assert result.subscription is None
            assert result.timetoken > 0
            assert result.message == message

            pubnub.unsubscribe().channels(ch).execute()
            subscribe_listener.wait_for_disconnect()
Exemplo n.º 14
0
    def test_join_leave(self):
        ch = helper.gen_channel("test-subscribe-join-leave")

        pubnub = PubNub(pnconf_sub_copy())
        pubnub_listener = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

        pubnub.config.uuid = helper.gen_channel("messenger")
        pubnub_listener.config.uuid = helper.gen_channel("listener")

        try:
            pubnub.add_listener(callback_messages)
            pubnub_listener.add_listener(callback_presence)

            pubnub_listener.subscribe().channels(ch).with_presence().execute()
            callback_presence.wait_for_connect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'join'
            assert envelope.uuid == pubnub_listener.uuid

            pubnub.subscribe().channels(ch).execute()
            callback_messages.wait_for_connect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'join'
            assert envelope.uuid == pubnub.uuid

            pubnub.unsubscribe().channels(ch).execute()
            callback_messages.wait_for_disconnect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'leave'
            assert envelope.uuid == pubnub.uuid

            pubnub_listener.unsubscribe().channels(ch).execute()
            callback_presence.wait_for_disconnect()
        except PubNubException as e:
            self.fail(e)
        finally:
            pubnub.stop()
            pubnub_listener.stop()
Exemplo n.º 15
0
    def test_join_leave(self):
        ch = helper.gen_channel("test-subscribe-join-leave")

        pubnub = PubNub(pnconf_sub_copy())
        pubnub_listener = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

        pubnub.config.uuid = helper.gen_channel("messenger")
        pubnub_listener.config.uuid = helper.gen_channel("listener")

        try:
            pubnub.add_listener(callback_messages)
            pubnub_listener.add_listener(callback_presence)

            pubnub_listener.subscribe().channels(ch).with_presence().execute()
            callback_presence.wait_for_connect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'join'
            assert envelope.uuid == pubnub_listener.uuid

            pubnub.subscribe().channels(ch).execute()
            callback_messages.wait_for_connect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'join'
            assert envelope.uuid == pubnub.uuid

            pubnub.unsubscribe().channels(ch).execute()
            callback_messages.wait_for_disconnect()

            envelope = callback_presence.wait_for_presence_on(ch)
            assert envelope.channel == ch
            assert envelope.event == 'leave'
            assert envelope.uuid == pubnub.uuid

            pubnub_listener.unsubscribe().channels(ch).execute()
            callback_presence.wait_for_disconnect()
        except PubNubException as e:
            self.fail(e)
        finally:
            pubnub.stop()
            pubnub_listener.stop()
Exemplo n.º 16
0
    def test_multiple_channels(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch1 = helper.gen_channel("here-now-native-sync-ch1")
        ch2 = helper.gen_channel("here-now-native-sync-ch2")
        pubnub.config.uuid = "here-now-native-sync-uuid"

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(5)

        pubnub.here_now().channels([ch1, ch2]).async(here_now_listener.callback)

        if here_now_listener.await() is False:
            self.fail("HereNow operation timeout")
Exemplo n.º 17
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("herenow-asyncio-channel")
        uuid = helper.gen_channel("herenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.here_now().channels(ch).include_uuids(True).async(here_now_listener.callback)

        if here_now_listener.await() is False:
            self.fail("HereNow operation timeout")
Exemplo n.º 18
0
    def test_multiple_channels(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch1 = helper.gen_channel("here-now-native-sync-ch1")
        ch2 = helper.gen_channel("here-now-native-sync-ch2")
        pubnub.config.uuid = "here-now-native-sync-uuid"

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(5)

        pubnub.here_now() \
            .channels([ch1, ch2]) \
            .async(here_now_listener.callback)

        if here_now_listener. await () is False:
            self.fail("HereNow operation timeout")
Exemplo n.º 19
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("herenow-asyncio-channel")
        uuid = helper.gen_channel("herenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.here_now() \
            .channels(ch) \
            .include_uuids(True) \
            .async(here_now_listener.callback)

        if here_now_listener. await () is False:
            self.fail("HereNow operation timeout")
Exemplo n.º 20
0
    def test_subscribe_cg_publish_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")
        message = "hey"

        pubnub = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        non_subscribe_listener = NonSubscribeListener()

        pubnub.add_channel_to_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .pn_async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsAddChannelResult)

        time.sleep(1)

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()
        callback_messages.wait_for_connect()

        pubnub.publish().message(message).channel(ch).pn_async(
            non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNPublishResult)
        assert result.timetoken > 0

        pubnub.unsubscribe().channel_groups(gr).execute()
        callback_messages.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .pn_async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
Exemplo n.º 21
0
    def test_subscribe_cg_publish_unsubscribe(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")
        message = "hey"

        pubnub = PubNub(pnconf_sub_copy())
        callback_messages = SubscribeListener()
        non_subscribe_listener = NonSubscribeListener()

        pubnub.add_channel_to_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsAddChannelResult)

        time.sleep(1)

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()
        callback_messages.wait_for_connect()

        pubnub.publish().message(message).channel(ch).async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNPublishResult)
        assert result.timetoken > 0

        pubnub.unsubscribe().channel_groups(gr).execute()
        callback_messages.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
    async def test_subscribe_publish_unsubscribe(self):
        ch = helper.gen_channel("subscribe-test")
        message = "hey"

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channels(ch).execute()
        await callback_messages.wait_for_connect()

        sub_env, pub_env = await tornado.gen.multi([
            callback_messages.wait_for_message_on(ch),
            self.pubnub.publish().channel(ch).message(message).future()])

        assert pub_env.status.original_response[0] == 1
        assert pub_env.status.original_response[1] == 'Sent'

        assert sub_env.channel == ch
        assert sub_env.subscription is None
        assert sub_env.message == message

        self.pubnub.unsubscribe().channels(ch).execute()
        await callback_messages.wait_for_disconnect()
    async def test_subscribe_publish_unsubscribe(self):
        ch = helper.gen_channel("subscribe-test")
        message = "hey"

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channels(ch).execute()
        await callback_messages.wait_for_connect()

        sub_env, pub_env = await tornado.gen.multi([
            callback_messages.wait_for_message_on(ch),
            self.pubnub.publish().channel(ch).message(message).future()
        ])

        assert pub_env.status.original_response[0] == 1
        assert pub_env.status.original_response[1] == 'Sent'

        assert sub_env.channel == ch
        assert sub_env.subscription is None
        assert sub_env.message == message

        self.pubnub.unsubscribe().channels(ch).execute()
        await callback_messages.wait_for_disconnect()
async def test_subscribe_publish_unsubscribe(event_loop):
    pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)

    callback = SubscribeListener()
    channel = helper.gen_channel("test-sub-pub-unsub")
    message = "hey"
    pubnub.add_listener(callback)
    pubnub.subscribe().channels(channel).execute()

    await callback.wait_for_connect()

    publish_future = asyncio.ensure_future(pubnub.publish().channel(channel).message(message).future())
    subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel))

    await asyncio.wait([
        publish_future,
        subscribe_message_future
    ])

    publish_envelope = publish_future.result()
    subscribe_envelope = subscribe_message_future.result()

    assert isinstance(subscribe_envelope, PNMessageResult)
    assert subscribe_envelope.channel == channel
    assert subscribe_envelope.subscription is None
    assert subscribe_envelope.message == message
    assert subscribe_envelope.timetoken > 0

    assert isinstance(publish_envelope, AsyncioEnvelope)
    assert publish_envelope.result.timetoken > 0
    assert publish_envelope.status.original_response[0] == 1

    pubnub.unsubscribe().channels(channel).execute()
    await callback.wait_for_disconnect()

    pubnub.stop()
Exemplo n.º 25
0
import logging
import asyncio
import pytest

import pubnub as pn
from pubnub.pubnub_asyncio import PubNubAsyncio, SubscribeListener
from tests import helper
from tests.helper import pnconf_sub_copy

pn.set_stream_logger('pubnub', logging.DEBUG)

messenger_config = pnconf_sub_copy()
messenger_config.set_presence_timeout(8)
messenger_config.uuid = helper.gen_channel("messenger")

listener_config = pnconf_sub_copy()
listener_config.uuid = helper.gen_channel("listener")


@pytest.mark.asyncio
async def test_timeout_event_on_broken_heartbeat(event_loop):
    ch = helper.gen_channel("heartbeat-test")

    pubnub = PubNubAsyncio(messenger_config, custom_event_loop=event_loop)
    pubnub_listener = PubNubAsyncio(listener_config,
                                    custom_event_loop=event_loop)

    pubnub.config.uuid = helper.gen_channel("messenger")
    pubnub_listener.config.uuid = helper.gen_channel("listener")

    # - connect to :ch-pnpres
Exemplo n.º 26
0
import logging
import asyncio
import pytest

import pubnub as pn
from pubnub.pubnub_asyncio import PubNubAsyncio, SubscribeListener
from tests import helper
from tests.helper import pnconf_sub_copy

pn.set_stream_logger('pubnub', logging.DEBUG)


messenger_config = pnconf_sub_copy()
messenger_config.set_presence_timeout(8)
messenger_config.uuid = helper.gen_channel("messenger")

listener_config = pnconf_sub_copy()
listener_config.uuid = helper.gen_channel("listener")


@pytest.mark.asyncio
def test_timeout_event_on_broken_heartbeat(event_loop):
    ch = helper.gen_channel("heartbeat-test")

    pubnub = PubNubAsyncio(messenger_config, custom_event_loop=event_loop)
    pubnub_listener = PubNubAsyncio(listener_config, custom_event_loop=event_loop)

    pubnub.config.uuid = helper.gen_channel("messenger")
    pubnub_listener.config.uuid = helper.gen_channel("listener")

    # - connect to :ch-pnpres
Exemplo n.º 27
0
    def test_subscribe_cg_join_leave(self):
        ch = helper.gen_channel("test-subscribe-unsubscribe-channel")
        gr = helper.gen_channel("test-subscribe-unsubscribe-group")

        pubnub = PubNub(pnconf_sub_copy())
        pubnub_listener = PubNub(pnconf_sub_copy())
        non_subscribe_listener = NonSubscribeListener()

        pubnub.add_channel_to_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .pn_async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsAddChannelResult)

        time.sleep(1)

        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

        pubnub_listener.add_listener(callback_presence)
        pubnub_listener.subscribe().channel_groups(
            gr).with_presence().execute()
        callback_presence.wait_for_connect()

        prs_envelope = callback_presence.wait_for_presence_on(ch)
        assert prs_envelope.event == 'join'
        assert prs_envelope.uuid == pubnub_listener.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub.add_listener(callback_messages)
        pubnub.subscribe().channel_groups(gr).execute()

        prs_envelope = callback_presence.wait_for_presence_on(ch)

        assert prs_envelope.event == 'join'
        assert prs_envelope.uuid == pubnub.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub.unsubscribe().channel_groups(gr).execute()
        prs_envelope = callback_presence.wait_for_presence_on(ch)

        assert prs_envelope.event == 'leave'
        assert prs_envelope.uuid == pubnub.uuid
        assert prs_envelope.channel == ch
        assert prs_envelope.subscription == gr

        pubnub_listener.unsubscribe().channel_groups(gr).execute()
        callback_presence.wait_for_disconnect()

        pubnub.remove_channel_from_channel_group() \
            .channel_group(gr) \
            .channels(ch) \
            .pn_async(non_subscribe_listener.callback)
        result = non_subscribe_listener.await_result_and_reset()
        assert isinstance(result, PNChannelGroupsRemoveChannelResult)

        pubnub.stop()
        pubnub_listener.stop()