def test_super_call(self): ch1 = "state-tornado-ch1" ch2 = "state-tornado-ch2" pnconf = pnconf_pam_copy() pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) pubnub.config.uuid = 'test-state-tornado-uuid-|.*$' state = {"name": "Alex", "count": 5} env = yield pubnub.set_state() \ .channels([ch1, ch2]) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = yield pubnub.get_state() \ .channels([ch1, ch2]) \ .future() assert env.result.channels[ch1]['name'] == "Alex" assert env.result.channels[ch2]['name'] == "Alex" assert env.result.channels[ch1]['count'] == 5 assert env.result.channels[ch2]['count'] == 5 pubnub.stop() self.stop()
class TestPubNubAsyncWhereNow(AsyncTestCase): def setUp(self): super(TestPubNubAsyncWhereNow, self).setUp() self.pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/where_now/single_channel.yaml', filter_query_parameters=['uuid', 'pnsdk']) @tornado.testing.gen_test(timeout=15) def test_where_now_single_channel(self): ch = "where-now-tornado-ch" uuid = "where-now-tornado-uuid" self.pubnub.config.uuid = uuid yield connect_to_channel(self.pubnub, ch) yield gen.sleep(10) env = yield self.pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 1 assert channels[0] == ch yield disconnect_from_channel(self.pubnub, ch) self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/where_now/multiple_channels.yaml', filter_query_parameters=['uuid', 'pnsdk']) @tornado.testing.gen_test(timeout=15) def test_multiple_channels(self): ch1 = "where-now-tornado-ch1" ch2 = "where-now-tornado-ch2" uuid = "where-now-tornado-uuid" self.pubnub.config.uuid = 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(5) env = yield self.pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 2 assert ch1 in channels assert ch2 in channels yield disconnect_from_channel(self.pubnub, [ch1, ch2]) self.pubnub.stop() self.stop()
class RealtimeData(Settings): last_price = 0 def __init__(self): super().__init__() self.c = PNConfiguration() self.c.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' self.c.reconnect_policy = PNReconnectionPolicy.LINEAR self.pubnub = PubNubTornado(self.c) channels = [ self.realtime_product, ] self.main(channels) self.pubnub.start() @gen.coroutine def main(self, channels): class Callback(SubscribeCallback): def message(self, pubnub, message): RealtimeData.last_price = message.message['ltp'] pubnub.stop() # 必要なデータを取得したら一度停止する s = Callback() self.pubnub.add_listener(s) self.pubnub.subscribe().channels(channels).execute() def stop(self): self.pubnub.stop() def get_current_data(self): self.pubnub.start()
def test_publish_futurex(self): pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) envelope = yield pubnub.publish().message('hey').channel('blah').future() assert isinstance(envelope, TornadoEnvelope) assert not envelope.is_error() pubnub.stop()
def test_publish_futurex(self): pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) envelope = yield pubnub.publish().message('hey').channel( 'blah').future() assert isinstance(envelope, TornadoEnvelope) assert not envelope.is_error() pubnub.stop()
def test_publish_future_raises(self): pubnub = PubNubTornado(corrupted_keys, custom_ioloop=self.io_loop) e = yield pubnub.publish().message('hey').channel('blah').future() assert isinstance(e, PubNubTornadoException) assert e.is_error() assert 400 == e.value()._status_code pubnub.stop()
def test_publish_result_raises_pubnub_error(self): pubnub = PubNubTornado(corrupted_keys, custom_ioloop=self.io_loop) with pytest.raises(PubNubException) as exinfo: yield pubnub.publish().message('hey').channel('blah').result() assert 'Invalid Subscribe Key' in str(exinfo.value) assert 400 == exinfo.value._status_code pubnub.stop()
def xtest_publish_future_raises_lower_level_error(self): pubnub = PubNubTornado(corrupted_keys, custom_ioloop=self.io_loop) pubnub.http.close() e = yield pubnub.publish().message('hey').channel('blah').future() assert isinstance(e, PubNubTornadoException) assert str(e) == "fetch() called on closed AsyncHTTPClient" pubnub.stop()
def xtest_publish_result_raises_lower_level_error(self): pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) # TODO: find a better way ot emulate broken connection pubnub.http.close() with self.assertRaises(Exception) as context: yield pubnub.publish().message('hey').channel('blah').result() assert 'fetch() called on closed AsyncHTTPClient' in str(context.exception.message) pubnub.stop()
def xtest_publish_result_raises_lower_level_error(self): pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) # TODO: find a better way ot emulate broken connection pubnub.http.close() with self.assertRaises(Exception) as context: yield pubnub.publish().message('hey').channel('blah').result() assert 'fetch() called on closed AsyncHTTPClient' in str( context.exception.message) pubnub.stop()
class TestMessageCount(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_mc_copy() config.enable_subscribe = False self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/message_count/single.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_cg', 'l_pub']) @tornado.testing.gen_test def test_single_channel(self): chan = 'unique_tornado' envelope = yield self.pn.publish().channel(chan).message( 'bla').future() time = envelope.result.timetoken - 10 envelope = yield self.pn.message_counts().channel( chan).channel_timetokens([time]).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert envelope.result.channels[chan] == 1 assert isinstance(envelope.result, PNMessageCountResult) assert isinstance(envelope.status, PNStatus) self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/message_count/multi.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_cg', 'l_pub']) @tornado.testing.gen_test def test_multiple_channels(self): chan_1 = 'unique_asyncio_1' chan_2 = 'unique_asyncio_2' chans = ','.join([chan_1, chan_2]) envelope = yield self.pn.publish().channel(chan_1).message( 'something').future() time = envelope.result.timetoken - 10 envelope = yield self.pn.message_counts().channel( chans).channel_timetokens([time, time]).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert envelope.result.channels[chan_1] == 1 assert envelope.result.channels[chan_2] == 0 assert isinstance(envelope.result, PNMessageCountResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
def test_publish_ssl(self): print(sys.version_info) pubnub = PubNubTornado(pnconf_ssl_copy(), custom_ioloop=self.io_loop) msg = "hey" pub = pubnub.publish().channel(ch).message(msg) envelope = yield pub.future() assert isinstance(envelope, TornadoEnvelope) assert isinstance(envelope.result, PNPublishResult) assert isinstance(envelope.status, PNStatus) assert envelope.result.timetoken > 0 assert len(envelope.status.original_response) > 0 pubnub.stop()
class TestMessageCount(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_mc_copy() config.enable_subscribe = False self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette('tests/integrational/fixtures/tornado/message_count/single.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_cg', 'l_pub']) @tornado.testing.gen_test def test_single_channel(self): chan = 'unique_tornado' envelope = yield self.pn.publish().channel(chan).message('bla').future() time = envelope.result.timetoken - 10 envelope = yield self.pn.message_counts().channel(chan).channel_timetokens([time]).future() assert(isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert envelope.result.channels[chan] == 1 assert isinstance(envelope.result, PNMessageCountResult) assert isinstance(envelope.status, PNStatus) self.pn.stop() @pn_vcr.use_cassette('tests/integrational/fixtures/tornado/message_count/multi.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_cg', 'l_pub']) @tornado.testing.gen_test def test_multiple_channels(self): chan_1 = 'unique_asyncio_1' chan_2 = 'unique_asyncio_2' chans = ','.join([chan_1, chan_2]) envelope = yield self.pn.publish().channel(chan_1).message('something').future() time = envelope.result.timetoken - 10 envelope = yield self.pn.message_counts().channel(chans).channel_timetokens([time, time]).future() assert(isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert envelope.result.channels[chan_1] == 1 assert envelope.result.channels[chan_2] == 0 assert isinstance(envelope.result, PNMessageCountResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
class TestMessageCount(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_copy() self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/fire_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_single_channel(self): chan = 'unique_sync' envelope = yield self.pn.fire().channel(chan).message('bla').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNFireResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
class TestSignal(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) pnconf = PNConfiguration() pnconf.publish_key = 'demo' pnconf.subscribe_key = 'demo' pnconf.enable_subscribe = False self.pn = PubNubTornado(pnconf, custom_ioloop=self.io_loop) @pn_vcr.use_cassette('tests/integrational/fixtures/tornado/signal/single.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_single_channel(self): chan = 'unique_sync' envelope = yield self.pn.signal().channel(chan).message('test').future() assert(isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert envelope.result.timetoken == '15640051976283377' assert isinstance(envelope.result, PNSignalResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
class TestPubNubState(AsyncTestCase): def setUp(self): super(TestPubNubState, self).setUp() self.pubnub = PubNubTornado(pnconf_copy(), custom_ioloop=self.io_loop) @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/state/single_channel.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk'], match_on=['method', 'host', 'path', 'state_object_in_query']) @tornado.testing.gen_test def test_state_single_channel(self): ch = "state-tornado-ch" self.pubnub.config.uuid = 'state-tornado-uuid' state = {"name": "Alex", "count": 5} env = yield self.pubnub.set_state() \ .channels(ch) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = yield self.pubnub.get_state() \ .channels(ch) \ .future() assert env.result.channels[ch]['name'] == "Alex" assert env.result.channels[ch]['count'] == 5 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/state/multiple_channel.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk'], match_on=['method', 'host', 'path', 'state_object_in_query']) @tornado.testing.gen_test def test_multiple_channels(self): ch1 = "state-tornado-ch1" ch2 = "state-tornado-ch2" self.pubnub.config.uuid = 'state-tornado-uuid' state = {"name": "Alex", "count": 5} env = yield self.pubnub.set_state() \ .channels([ch1, ch2]) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = yield self.pubnub.get_state() \ .channels([ch1, ch2]) \ .future() assert env.result.channels[ch1]['name'] == "Alex" assert env.result.channels[ch2]['name'] == "Alex" assert env.result.channels[ch1]['count'] == 5 assert env.result.channels[ch2]['count'] == 5 self.pubnub.stop() self.stop()
def test_publish_resultx(self): pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) result = yield pubnub.publish().message('hey').channel('blah').result() assert isinstance(result, PNPublishResult) pubnub.stop()
class TestUser(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_obj_copy() self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/user/users_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_users(self): envelope = yield self.pn.get_users().include('custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetUsersResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert len(data) == 100 assert set([ 'name', 'id', 'externalId', 'profileUrl', 'email', 'custom', 'created', 'updated', 'eTag' ]) == set(data[0]) assert set([ 'name', 'id', 'externalId', 'profileUrl', 'email', 'custom', 'created', 'updated', 'eTag' ]) == set(data[1]) self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/user/create_user.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_create_user(self): data = {'id': 'mg', 'name': 'MAGNUM', 'custom': {'XXX': 'YYYY'}} envelope = yield self.pn.create_user().data(data).include( 'custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNCreateUserResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert data['id'] == 'mg' assert data['name'] == 'MAGNUM' assert data['externalId'] is None assert data['profileUrl'] is None assert data['email'] is None assert data['custom'] == {'XXX': 'YYYY'} self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/user/fetch_user.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_user(self): envelope = yield self.pn.get_user().user_id('mg').include( 'custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetUserResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert set([ 'name', 'id', 'externalId', 'profileUrl', 'email', 'created', 'updated', 'eTag', 'custom' ]) == set(data) assert data['id'] == 'mg' assert data['name'] == 'MAGNUM' assert data['externalId'] is None assert data['profileUrl'] is None assert data['email'] is None assert data['custom'] == {'XXX': 'YYYY'} self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/user/update_user.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_update_user(self): envelope = yield self.pn.update_user().user_id('mg').data({ 'name': 'number 3' }).include('custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNUpdateUserResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert set([ 'name', 'id', 'externalId', 'profileUrl', 'email', 'created', 'updated', 'eTag', 'custom' ]) == set(data) assert data['id'] == 'mg' assert data['name'] == 'number 3' assert data['externalId'] is None assert data['profileUrl'] is None assert data['email'] is None assert data['custom'] == {'XXX': 'YYYY'} self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/user/delete_user.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_delete_user(self): envelope = yield self.pn.delete_user().user_id('mg').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNDeleteUserResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
class TestChannelSubscription(AsyncTestCase, SubscriptionTest): def setUp(self): super(TestChannelSubscription, 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/sub_unsub.yaml', filter_query_parameters=['uuid', 'seqn']) @tornado.testing.gen_test(timeout=300) def test_subscribe_unsubscribe(self): ch = "subscribe-tornado-ch" callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() assert ch in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 1 yield callback_messages.wait_for_connect() assert ch in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 1 self.pubnub.unsubscribe().channels(ch).execute() assert ch not in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 0 yield callback_messages.wait_for_disconnect() assert ch not in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/subscribe/sub_pub_unsub.yaml', filter_query_parameters=['uuid', 'seqn']) @tornado.testing.gen_test(timeout=30) def test_subscribe_publish_unsubscribe(self): ch = "subscribe-tornado-ch" message = "hey" callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() yield callback_messages.wait_for_connect() sub_env, pub_env = yield [ 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() yield callback_messages.wait_for_disconnect() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/subscribe/join_leave.yaml', filter_query_parameters=['uuid', 'seqn']) @tornado.testing.gen_test(timeout=15) def test_join_leave(self): ch = "subscribe-tornado-ch" self.pubnub.config.uuid = "subscribe-tornado-messenger" self.pubnub_listener.config.uuid = "subscribe-tornado-listener" callback_presence = SubscribeListener() self.pubnub_listener.add_listener(callback_presence) self.pubnub_listener.subscribe().channels(ch).with_presence().execute() yield callback_presence.wait_for_connect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'join' assert envelope.uuid == self.pubnub_listener.uuid callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() yield callback_messages.wait_for_connect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'join' assert envelope.uuid == self.pubnub.uuid self.pubnub.unsubscribe().channels(ch).execute() yield callback_messages.wait_for_disconnect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'leave' assert envelope.uuid == self.pubnub.uuid self.pubnub_listener.unsubscribe().channels(ch).execute() yield callback_presence.wait_for_disconnect() self.pubnub.stop() self.stop()
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()
class TestSpace(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_obj_copy() self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/space/get_spaces.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_spaces(self): envelope = yield self.pn.get_spaces().include('custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetSpacesResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert len(data) == 100 assert set([ 'name', 'id', 'description', 'custom', 'created', 'updated', 'eTag' ]) == set(data[0]) assert set([ 'name', 'id', 'description', 'custom', 'created', 'updated', 'eTag' ]) == set(data[1]) self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/space/create_space.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_create_space(self): envelope = yield self.pn.create_space().data({ 'id': 'in_space', 'name': 'some_name', 'custom': { 'a': 3 } }).include('custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNCreateSpaceResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert data['id'] == 'in_space' assert data['name'] == 'some_name' assert data['custom'] == {'a': 3} assert data['description'] is None self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/space/get_space.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_space(self): envelope = yield self.pn.get_space().space_id('in_space').include( 'custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetSpaceResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert set([ 'name', 'id', 'description', 'created', 'updated', 'eTag', 'custom' ]) == set(data) assert data['id'] == 'in_space' assert data['name'] == 'some_name' assert data['custom'] == {'a': 3} assert data['description'] is None self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/space/update_space.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_update_space(self): data = {'description': 'desc'} envelope = yield self.pn.update_space().space_id('in_space').data( data).include('custom').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNUpdateSpaceResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert set([ 'name', 'id', 'description', 'created', 'updated', 'eTag', 'custom' ]) == set(data) assert data['id'] == 'in_space' assert data['name'] == 'some_name' assert data['custom'] == {'a': 3} assert data['description'] == 'desc' self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/space/delete_space.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_delete_space(self): envelope = yield self.pn.delete_space().space_id('in_space').future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNDeleteSpaceResult) assert isinstance(envelope.status, PNStatus) self.pn.stop()
class TestUser(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) config = pnconf_obj_copy() self.pn = PubNubTornado(config, custom_ioloop=self.io_loop) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/members/get_members.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_members(self): envelope = yield self.pn.get_members().space_id('value1').include(['custom', 'user', 'user.custom'])\ .count(True).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetMembersResult) assert isinstance(envelope.status, PNStatus) assert envelope.result.total_count == 1 data = envelope.result.data assert len(data) == 1 assert set(['user', 'custom', 'id', 'created', 'updated', 'eTag']) == set(data[0]) assert data[0]['user']['id'] == 'mg3' assert data[0]['user']['name'] == 'MAGNUM3' assert data[0]['user']['custom'] == {'ZZZ': 'IIII'} self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/members/get_space_memberships.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_get_space_memberships(self): envelope = yield self.pn.get_space_memberships().user_id('mg3').include(['custom', 'space', 'space.custom'])\ .count(True).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNGetSpaceMembershipsResult) assert isinstance(envelope.status, PNStatus) assert envelope.result.total_count == 1 data = envelope.result.data assert len(data) == 1 assert set(['id', 'space', 'created', 'updated', 'eTag', 'custom']) == set(data[0]) assert data[0]['space']['id'] == 'value1' assert data[0]['space']['name'] == 'value2' assert data[0]['space']['description'] == 'abcd' assert data[0]['space']['custom'] is None self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/members/update_space_memberships.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_manage_memberships(self): envelope = yield self.pn.manage_memberships().user_id('mg').data({ 'add': [{ 'id': 'value1' }] }).include(['custom', 'space', 'space.custom']).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNManageMembershipsResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert len(data) == 1 assert set(['id', 'space', 'created', 'updated', 'eTag', 'custom']) == set(data[0]) assert data[0]['space']['id'] == 'value1' assert data[0]['space']['name'] == 'value2' assert data[0]['space']['description'] == 'abcd' assert data[0]['space']['custom'] is None self.pn.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/members/update_members.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_manage_members(self): envelope = yield self.pn.manage_members().space_id('value1').data({ 'add': [{ 'id': 'mg3' }] }).include(['custom', 'user', 'user.custom']).future() assert (isinstance(envelope, TornadoEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNManageMembersResult) assert isinstance(envelope.status, PNStatus) data = envelope.result.data assert len(data) == 2 assert set(['user', 'custom', 'id', 'created', 'updated', 'eTag']) == set(data[0]) assert set(['user', 'custom', 'id', 'created', 'updated', 'eTag']) == set(data[1]) if data[0]['user']['id'] == 'mg': user = data[0]['user'] else: user = data[1]['user'] assert user['id'] == 'mg' assert user['name'] == 'number 3' assert user['custom'] == {'XXX': 'YYYY'} self.pn.stop()
class TestChannelGroups(AsyncTestCase): def setUp(self): super(TestChannelGroups, self).setUp() self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_remove_single_channel.yaml', filter_query_parameters=['uuid', 'pnsdk']) @tornado.testing.gen_test def test_add_remove_single_channel(self): ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove env = yield self.pubnub.remove_channel_from_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_remove_multiple_channel.yaml', filter_query_parameters=['uuid', 'pnsdk']) @tornado.testing.gen_test def test_add_remove_multiple_channels(self): ch1 = "channel-groups-tornado-ch1" ch2 = "channel-groups-tornado-ch2" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels([ch1, ch2]).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 2 assert ch1 in env.result.channels assert ch2 in env.result.channels # remove env = yield self.pubnub.remove_channel_from_channel_group() \ .channels([ch1, ch2]).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_channel_remove_group.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test def test_add_channel_remove_group(self): ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove group env = yield self.pubnub.remove_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop()
class FastTrader(Settings): last_price = 0 hd = ConditionChecker() hd.market_reader() # マーケットの流れを確認 hd.board_status_checker() # サーバー状態を確認 hd.sfd_status_checker() # SFDを確認 rec = Recorder() count = 0 count2 = 0 last_balance = 0 order = OrderMaker() def __init__(self): super().__init__() self.c = PNConfiguration() self.c.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' self.c.reconnect_policy = PNReconnectionPolicy.LINEAR self.pubnub = PubNubTornado(self.c) channels = [ self.realtime_product, ] self.main(channels) self.pubnub.start() @gen.coroutine def main(self, channels): class Callback(SubscribeCallback): def message(self, pubnub, message): current_price = message.message['ltp'] FastTrader.hd.child_order_checker() FastTrader.hd.position_checker() if FastTrader.count == 0: FastTrader.hd.market_reader() print(current_price) if FastTrader.hd.positioning and not FastTrader.hd.ordering: FastTrader.hd.position_checker_for_market_ordering( current_price) print('aiming to place execution order') FastTrader.count += 1 if FastTrader.hd.signal: print('aiming to place a new order') FastTrader.hd.order_information_checker("MARKET") if FastTrader.count == 40: FastTrader.count2 += 1 FastTrader.hd.sfd_status_checker() if FastTrader.count2 == 10: FastTrader.rec.balance_recorder( FastTrader.hd.balance, current_price) FastTrader.count2 = 0 FastTrader.count = 0 s = Callback() self.pubnub.add_listener(s) self.pubnub.subscribe().channels(channels).execute() def stop(self): self.pubnub.stop() def get_current_data(self): self.pubnub.start()
class TestChannelGroups(AsyncTestCase): def setUp(self): super(TestChannelGroups, self).setUp() self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_remove_single_channel.yaml', filter_query_parameters=['uuid', 'pnsdk', 'l_cg']) @tornado.testing.gen_test def test_add_remove_single_channel(self): ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove env = yield self.pubnub.remove_channel_from_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_remove_multiple_channel.yaml', filter_query_parameters=['uuid', 'pnsdk', 'l_cg']) @tornado.testing.gen_test def test_add_remove_multiple_channels(self): ch1 = "channel-groups-tornado-ch1" ch2 = "channel-groups-tornado-ch2" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels([ch1, ch2]).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 2 assert ch1 in env.result.channels assert ch2 in env.result.channels # remove env = yield self.pubnub.remove_channel_from_channel_group() \ .channels([ch1, ch2]).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/groups/add_channel_remove_group.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_cg']) @tornado.testing.gen_test def test_add_channel_remove_group(self): ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = yield self.pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove group env = yield self.pubnub.remove_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) yield gen.sleep(1) # list env = yield self.pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 self.pubnub.stop() self.stop()
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()
class TestPubNubAsyncPublish(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) self.env = None def callback(self, tornado_res): self.env = tornado_res.result() self.pubnub.stop() self.stop() def assert_success(self, pub): pub.future().add_done_callback(self.callback) self.pubnub.start() self.wait() assert isinstance(self.env, TornadoEnvelope) assert isinstance(self.env.result, PNPublishResult) assert isinstance(self.env.status, PNStatus) assert self.env.result.timetoken > 0 assert len(self.env.status.original_response) > 0 @tornado.testing.gen_test def assert_success_yield(self, pub): envelope = yield pub.future() assert isinstance(envelope, TornadoEnvelope) assert isinstance(envelope.result, PNPublishResult) assert isinstance(envelope.status, PNStatus) assert envelope.result.timetoken > 0 assert len(envelope.status.original_response) > 0 def assert_success_publish_get(self, msg): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg)) def assert_success_publish_post(self, msg): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg).use_post(True)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg).use_post(True)) def assert_success_publish_get_encrypted(self, msg): self.pubnub = PubNubTornado(pnconf_enc, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg)) def assert_success_publish_post_encrypted(self, msg): self.pubnub = PubNubTornado(pnconf_enc, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg).use_post(True)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg).use_post(True)) def assert_client_side_error(self, pub, expected_err_msg): try: yield pub.future() self.pubnub.start() self.wait() except PubNubException as e: assert expected_err_msg in str(e) self.pubnub.stop() self.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_mixed_via_get(self): self.assert_success_publish_get("hi") self.assert_success_publish_get(5) self.assert_success_publish_get(True) self.assert_success_publish_get(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'scheme', 'host', 'port', 'object_in_path', 'query']) def test_publish_object_via_get(self): self.assert_success_publish_get({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_post.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'scheme', 'host', 'port', 'path', 'query']) def test_publish_mixed_via_post(self): self.assert_success_publish_post("hi") self.assert_success_publish_post(5) self.assert_success_publish_post(True) self.assert_success_publish_post(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_post.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'path', 'query', 'object_in_body']) def test_publish_object_via_post(self): self.assert_success_publish_post({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_get_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_mixed_via_get_encrypted(self): self.assert_success_publish_get_encrypted("hi") self.assert_success_publish_get_encrypted(5) self.assert_success_publish_get_encrypted(True) self.assert_success_publish_get_encrypted(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_get_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'query', 'object_in_path'], match_on_kwargs={'object_in_path': { 'decrypter': gen_decrypt_func('testKey')}}) def test_publish_object_via_get_encrypted(self): self.assert_success_publish_get_encrypted({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_post_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'path', 'query', 'body']) def test_publish_mixed_via_post_encrypted(self): self.assert_success_publish_post_encrypted("hi") self.assert_success_publish_post_encrypted(5) self.assert_success_publish_post_encrypted(True) self.assert_success_publish_post_encrypted(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_post_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'path', 'query', 'object_in_body'], match_on_kwargs={'object_in_body': { 'decrypter': gen_decrypt_func('testKey')}}) def test_publish_object_via_post_encrypted(self): self.assert_success_publish_post_encrypted({"name": "Alex", "online": True}) def test_error_missing_message(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_client_side_error(self.pubnub.publish().channel(ch).message(None), "Message missing") def test_error_missing_channel(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_client_side_error(self.pubnub.publish().channel("").message("hey"), "Channel missing") def test_error_non_serializable(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) def method(): pass self.assert_client_side_error(self.pubnub.publish().channel(ch).message(method), "not JSON serializable") def sserr_cb(self, env): assert isinstance(env, Future) exception = env.exception() self.pubnub.stop() # this kind of assertion will not fail the test if'll be moved below `self.stop()` call # but also not raises correct exception, timeout exception will be raised on fail instead assert self.expected_err_msg in str(exception) self.stop() def assert_server_side_error(self, pub, expected_err_msg): self.expected_err_msg = expected_err_msg pub.result().add_done_callback(self.sserr_cb) self.pubnub.start() self.wait() @tornado.testing.gen_test def assert_server_side_error_yield(self, pub, expected_err_msg): try: yield pub.result() self.pubnub.start() self.wait() except PubNubException as e: assert expected_err_msg in str(e) self.pubnub.stop() self.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/invalid_key.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_error_invalid_key(self): conf = PNConfiguration() conf.publish_key = "fake" conf.subscribe_key = "demo" self.pubnub = PubNubTornado(conf, custom_ioloop=self.io_loop) self.assert_server_side_error(self.pubnub.publish().channel(ch).message("hey"), "Invalid Key") self.assert_server_side_error_yield(self.pubnub.publish().channel(ch).message("hey"), "Invalid Key") @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/not_permitted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_error_not_permitted_403(self): my_pnconf = pnconf_pam_copy() my_pnconf.secret_key = None self.pubnub = PubNubTornado(my_pnconf, custom_ioloop=self.io_loop) self.assert_server_side_error( self.pubnub.publish().channel("not_permitted_channel").message("hey"), "HTTP Client Error (403)") self.assert_server_side_error_yield( self.pubnub.publish().channel("not_permitted_channel").message("hey"), "HTTP Client Error (403)") @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/meta_object.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'path', 'meta_object_in_query']) def test_publish_with_meta(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success( self.pubnub.publish().channel(ch).message("hey").meta({'a': 2, 'b': 'qwer'})) self.assert_success_yield( self.pubnub.publish().channel(ch).message("hey").meta({'a': 2, 'b': 'qwer'})) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/do_not_store.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_do_not_store(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success( self.pubnub.publish().channel(ch).message("hey").should_store(False)) self.assert_success_yield( self.pubnub.publish().channel(ch).message("hey").should_store(False))
class TestChannelSubscription(AsyncTestCase, SubscriptionTest): def setUp(self): super(TestChannelSubscription, 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/sub_unsub.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test(timeout=300) def test_subscribe_unsubscribe(self): ch = "subscribe-tornado-ch" callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() assert ch in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 1 yield callback_messages.wait_for_connect() assert ch in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 1 self.pubnub.unsubscribe().channels(ch).execute() assert ch not in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 0 yield callback_messages.wait_for_disconnect() assert ch not in self.pubnub.get_subscribed_channels() assert len(self.pubnub.get_subscribed_channels()) == 0 self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/subscribe/sub_pub_unsub.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test(timeout=30) def test_subscribe_publish_unsubscribe(self): ch = "subscribe-tornado-ch" message = "hey" callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() yield callback_messages.wait_for_connect() sub_env, pub_env = yield [ 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() yield callback_messages.wait_for_disconnect() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/subscribe/join_leave.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk']) @tornado.testing.gen_test(timeout=30) def test_join_leave(self): ch = "subscribe-tornado-ch" # HINT: use random generated uuids to test without VCR # rnd = gen_string(4) # self.pubnub.config.uuid = "subscribe-tornado-messenger-%s" % rnd # self.pubnub_listener.config.uuid = "subscribe-tornado-listener-%s" % rnd self.pubnub.config.uuid = "subscribe-tornado-messenger-3" self.pubnub_listener.config.uuid = "subscribe-tornado-listener-3" callback_presence = SubscribeListener() self.pubnub_listener.add_listener(callback_presence) self.pubnub_listener.subscribe().channels(ch).with_presence().execute() yield callback_presence.wait_for_connect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'join' assert envelope.uuid == self.pubnub_listener.uuid callback_messages = SubscribeListener() self.pubnub.add_listener(callback_messages) self.pubnub.subscribe().channels(ch).execute() yield callback_messages.wait_for_connect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'join' assert envelope.uuid == self.pubnub.uuid self.pubnub.unsubscribe().channels(ch).execute() yield callback_messages.wait_for_disconnect() envelope = yield callback_presence.wait_for_presence_on(ch) assert envelope.channel == ch assert envelope.event == 'leave' assert envelope.uuid == self.pubnub.uuid self.pubnub_listener.unsubscribe().channels(ch).execute() yield callback_presence.wait_for_disconnect() self.pubnub.stop() self.stop()
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()
class TestPubNubAsyncWhereNow(AsyncTestCase): def setUp(self): super(TestPubNubAsyncWhereNow, self).setUp() self.pubnub = PubNubTornado(pnconf_sub_copy(), custom_ioloop=self.io_loop) @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/where_now/single_channel.yaml', filter_query_parameters=['uuid']) @tornado.testing.gen_test(timeout=15) def test_where_now_single_channel(self): ch = "where-now-tornado-ch" uuid = "where-now-tornado-uuid" self.pubnub.config.uuid = uuid yield connect_to_channel(self.pubnub, ch) yield gen.sleep(10) env = yield self.pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 1 assert channels[0] == ch yield disconnect_from_channel(self.pubnub, ch) self.pubnub.stop() self.stop() @use_cassette_and_stub_time_sleep( 'tests/integrational/fixtures/tornado/where_now/multiple_channels.yaml', filter_query_parameters=['uuid']) @tornado.testing.gen_test(timeout=15) def test_multiple_channels(self): ch1 = "where-now-tornado-ch1" ch2 = "where-now-tornado-ch2" uuid = "where-now-tornado-uuid" self.pubnub.config.uuid = 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(5) env = yield self.pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 2 assert ch1 in channels assert ch2 in channels yield disconnect_from_channel(self.pubnub, [ch1, ch2]) self.pubnub.stop() self.stop()