コード例 #1
0
class TestPubNubAsyncHereNow(AsyncTestCase):
    def setUp(self):
        super(TestPubNubAsyncHereNow, self).setUp()
        self.pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop)

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/here_now/single.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pres'])
    @tornado.testing.gen_test(timeout=15)
    def test_here_now_single_channel(self):
        ch = 'test-here-now-channel'
        self.pubnub.config.uuid = 'test-here-now-uuid'
        yield connect_to_channel(self.pubnub, ch)
        yield tornado.gen.sleep(10)
        env = yield self.pubnub.here_now() \
            .channels(ch) \
            .include_uuids(True) \
            .future()

        assert env.result.total_channels == 1
        assert env.result.total_occupancy >= 1

        channels = env.result.channels

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

        yield disconnect_from_channel(self.pubnub, ch)
        self.pubnub.stop()
        self.stop()

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/here_now/multiple.yaml',
        filter_query_parameters=['uuid', 'tt', 'tr', 'pnsdk', 'l_pres'])
    @tornado.testing.gen_test(timeout=120)
    def test_here_now_multiple_channels(self):
        ch1 = 'test-here-now-channel1'
        ch2 = 'test-here-now-channel2'
        self.pubnub.config.uuid = 'test-here-now-uuid'
        # print("connecting to the first...")
        yield connect_to_channel(self.pubnub, ch1)
        # print("...connected to the first")
        yield gen.sleep(1)
        # print("connecting to the second...")
        self.pubnub.subscribe().channels(ch2).execute()
        # print("...connected to the second")
        yield gen.sleep(5)
        env = yield self.pubnub.here_now() \
            .channels([ch1, ch2]) \
            .future()

        assert env.result.total_channels == 2
        assert env.result.total_occupancy >= 1

        channels = env.result.channels

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

        yield disconnect_from_channel(self.pubnub, [ch1, ch2])
        self.pubnub.stop()
        self.stop()

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/here_now/global.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pres'])
    @tornado.testing.gen_test(timeout=15)
    def test_here_now_global(self):
        ch1 = 'test-here-now-channel1'
        ch2 = 'test-here-now-channel2'
        self.pubnub.config.uuid = 'test-here-now-uuid'

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channels(ch1).execute()
        yield callback_messages.wait_for_connect()

        self.pubnub.subscribe().channels(ch2).execute()
        yield gen.sleep(6)

        env = yield self.pubnub.here_now().future()

        assert env.result.total_channels >= 2
        assert env.result.total_occupancy >= 1

        yield disconnect_from_channel(self.pubnub, [ch1, ch2])

        self.pubnub.stop()
        self.stop()
コード例 #2
0
class TestChannelGroupSubscription(AsyncTestCase, SubscriptionTest):
    def setUp(self):
        super(TestChannelGroupSubscription, self).setUp()
        self.pubnub = PubNubTornado(pnconf_sub_copy(),
                                    custom_ioloop=self.io_loop)
        self.pubnub_listener = PubNubTornado(pnconf_sub_copy(),
                                             custom_ioloop=self.io_loop)

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_sub_unsub.yaml',
        filter_query_parameters=['uuid', 'seqn'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_subscribe_unsubscribe(self):
        ch = "subscribe-unsubscribe-channel"
        gr = "subscribe-unsubscribe-group"

        envelope = yield self.pubnub.add_channel_to_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channel_groups(gr).execute()
        yield callback_messages.wait_for_connect()

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

        envelope = yield self.pubnub.remove_channel_from_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_sub_pub_unsub.yaml',
        filter_query_parameters=['uuid', 'seqn'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_subscribe_publish_unsubscribe(self):
        ch = "subscribe-unsubscribe-channel"
        gr = "subscribe-unsubscribe-group"
        message = "hey"

        envelope = yield self.pubnub.add_channel_to_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channel_groups(gr).execute()
        yield callback_messages.wait_for_connect()

        sub_envelope, pub_envelope = yield [
            callback_messages.wait_for_message_on(ch),
            self.pubnub.publish().channel(ch).message(message).future()
        ]

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

        assert sub_envelope.channel == ch
        assert sub_envelope.subscription == gr
        assert sub_envelope.message == message

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

        envelope = yield self.pubnub.remove_channel_from_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_join_leave.yaml',
        filter_query_parameters=['uuid', 'seqn'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_join_leave(self):
        self.pubnub.config.uuid = "test-subscribe-messenger"
        self.pubnub_listener.config.uuid = "test-subscribe-listener"

        ch = "subscribe-test-channel"
        gr = "subscribe-test-group"

        envelope = yield self.pubnub.add_channel_to_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

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

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

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

        useless, prs_envelope = yield [
            callback_messages.wait_for_connect(),
            callback_presence.wait_for_presence_on(ch)
        ]

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

        self.pubnub.unsubscribe().channel_groups(gr).execute()

        useless, prs_envelope = yield [
            callback_messages.wait_for_disconnect(),
            callback_presence.wait_for_presence_on(ch)
        ]

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

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

        envelope = yield self.pubnub.remove_channel_from_channel_group(
        ).channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/subscribe_tep_by_step.yaml',
        filter_query_parameters=['uuid', 'seqn'])
    @tornado.testing.gen_test(timeout=30)
    def test_subscribe_step_by_step(self):
        ch1 = 'test-here-now-channel1'
        ch2 = 'test-here-now-channel2'
        ch3 = 'test-here-now-channel3'
        self.pubnub.config.uuid = 'test-here-now-uuid'
        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        print("connecting to the first...")
        self.pubnub.subscribe().channels(ch1).execute()
        yield callback_messages.wait_for_connect()
        print("...connected to the first")
        yield gen.sleep(1)
        print("connecting to the second...")
        self.pubnub.subscribe().channels(ch2).execute()
        self.pubnub.subscribe().channels(ch3).execute()
        self.pubnub.subscribe().channels(ch3).execute()
        self.pubnub.subscribe().channels(ch2).execute()
        print("...connected to the second")
        yield gen.sleep(5)
        env = yield self.pubnub.here_now() \
            .channels([ch1, ch2]) \
            .future()

        assert env.result.total_channels == 2
        assert env.result.total_occupancy >= 1

        channels = env.result.channels

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

        self.pubnub.unsubscribe().channels([ch1, ch2]).execute()
        yield callback_messages.wait_for_disconnect()

        self.pubnub.unsubscribe().channels(ch3).execute()

        self.pubnub.stop()
        self.stop()
コード例 #3
0
class TestChannelGroupSubscription(AsyncTestCase, SubscriptionTest):
    def setUp(self):
        super(TestChannelGroupSubscription, self).setUp()
        self.pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop)
        self.pubnub_listener = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop)

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_sub_unsub.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_subscribe_unsubscribe(self):
        ch = "subscribe-unsubscribe-channel"
        gr = "subscribe-unsubscribe-group"

        envelope = yield self.pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channel_groups(gr).execute()
        yield callback_messages.wait_for_connect()

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

        envelope = yield self.pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_sub_pub_unsub.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_subscribe_publish_unsubscribe(self):
        ch = "subscribe-unsubscribe-channel"
        gr = "subscribe-unsubscribe-group"
        message = "hey"

        envelope = yield self.pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        self.pubnub.subscribe().channel_groups(gr).execute()
        yield callback_messages.wait_for_connect()

        sub_envelope, pub_envelope = yield [
            callback_messages.wait_for_message_on(ch),
            self.pubnub.publish().channel(ch).message(message).future()]

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

        assert sub_envelope.channel == ch
        assert sub_envelope.subscription == gr
        assert sub_envelope.message == message

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

        envelope = yield self.pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/group_join_leave.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk'])
    @tornado.testing.gen_test(timeout=60)
    def test_group_join_leave(self):
        self.pubnub.config.uuid = "test-subscribe-messenger"
        self.pubnub_listener.config.uuid = "test-subscribe-listener"

        ch = "subscribe-test-channel"
        gr = "subscribe-test-group"

        envelope = yield self.pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

        yield gen.sleep(1)

        callback_messages = SubscribeListener()
        callback_presence = SubscribeListener()

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

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

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

        useless, prs_envelope = yield [
            callback_messages.wait_for_connect(),
            callback_presence.wait_for_presence_on(ch)
        ]

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

        self.pubnub.unsubscribe().channel_groups(gr).execute()

        useless, prs_envelope = yield [
            callback_messages.wait_for_disconnect(),
            callback_presence.wait_for_presence_on(ch)
        ]

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

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

        envelope = yield self.pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future()
        assert envelope.status.original_response['status'] == 200

    @use_cassette_and_stub_time_sleep(
        'tests/integrational/fixtures/tornado/subscribe/subscribe_tep_by_step.yaml',
        filter_query_parameters=['uuid', 'seqn', 'pnsdk'])
    @tornado.testing.gen_test(timeout=30)
    def test_subscribe_step_by_step(self):
        ch1 = 'test-here-now-channel1'
        ch2 = 'test-here-now-channel2'
        ch3 = 'test-here-now-channel3'
        self.pubnub.config.uuid = 'test-here-now-uuid'
        callback_messages = SubscribeListener()
        self.pubnub.add_listener(callback_messages)
        print("connecting to the first...")
        self.pubnub.subscribe().channels(ch1).execute()
        yield callback_messages.wait_for_connect()
        print("...connected to the first")
        yield gen.sleep(1)
        print("connecting to the second...")
        self.pubnub.subscribe().channels(ch2).execute()
        self.pubnub.subscribe().channels(ch3).execute()
        self.pubnub.subscribe().channels(ch3).execute()
        self.pubnub.subscribe().channels(ch2).execute()
        print("...connected to the second")
        yield gen.sleep(5)
        env = yield self.pubnub.here_now() \
            .channels([ch1, ch2]) \
            .future()

        assert env.result.total_channels == 2
        assert env.result.total_occupancy >= 1

        channels = env.result.channels

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

        self.pubnub.unsubscribe().channels([ch1, ch2]).execute()
        yield callback_messages.wait_for_disconnect()

        self.pubnub.unsubscribe().channels(ch3).execute()

        self.pubnub.stop()
        self.stop()