def test_multiple_channels(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) ch1 = 'test-where-now-asyncio-ch1' ch2 = 'test-where-now-asyncio-ch2' uuid = 'test-where-now-asyncio-uuid' pubnub.config.uuid = uuid callback = SubscribeListener() pubnub.add_listener(callback) pubnub.subscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_connect() yield from sleeper(7) env = yield from pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 2 assert ch1 in channels assert ch2 in channels pubnub.unsubscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_disconnect() pubnub.stop()
def test_subscribe_unsubscribe(event_loop): channel = "test-subscribe-asyncio-ch" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) callback = SubscribeListener() pubnub.add_listener(callback) pubnub.subscribe().channels(channel).execute() assert channel in pubnub.get_subscribed_channels() assert len(pubnub.get_subscribed_channels()) == 1 yield from callback.wait_for_connect() assert channel in pubnub.get_subscribed_channels() assert len(pubnub.get_subscribed_channels()) == 1 pubnub.unsubscribe().channels(channel).execute() assert channel not in pubnub.get_subscribed_channels() assert len(pubnub.get_subscribed_channels()) == 0 yield from callback.wait_for_disconnect() assert channel not in pubnub.get_subscribed_channels() assert len(pubnub.get_subscribed_channels()) == 0 pubnub.stop()
def test_cg_subscribe_unsubscribe(event_loop, sleeper=asyncio.sleep): ch = "test-subscribe-asyncio-channel" gr = "test-subscribe-asyncio-group" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) envelope = yield from pubnub.add_channel_to_channel_group().channel_group( gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 yield from sleeper(3) callback_messages = SubscribeListener() pubnub.add_listener(callback_messages) pubnub.subscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_connect() pubnub.unsubscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_disconnect() envelope = yield from pubnub.remove_channel_from_channel_group( ).channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop()
def test_single_channel_with_subscription(event_loop, sleeper=asyncio.sleep): pnconf = pnconf_sub_copy() pnconf.set_presence_timeout(12) pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch = 'test-state-asyncio-ch' pubnub.config.uuid = 'test-state-asyncio-uuid' state = {"name": "Alex", "count": 5} callback = SubscribeListener() pubnub.add_listener(callback) pubnub.subscribe().channels(ch).execute() yield from callback.wait_for_connect() yield from sleeper(20) env = yield from pubnub.set_state() \ .channels(ch) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = yield from pubnub.get_state() \ .channels(ch) \ .future() assert env.result.channels[ch]['name'] == "Alex" assert env.result.channels[ch]['count'] == 5 pubnub.unsubscribe().channels(ch).execute() yield from callback.wait_for_disconnect() pubnub.stop()
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()
async def message_pump(self): pnconfig = PNConfiguration() pnconfig.subscribe_key = config['cardsubkey'] # Why aren't these the default settings? pnconfig.ssl = True pnconfig.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL pubnub = PubNubAsyncio(pnconfig) listener = SubscribeListener() pubnub.add_listener(listener) pubnub.subscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_connect() log.info("Connected to PubNub") message_future = asyncio.ensure_future( listener.wait_for_message_on(config['cardviewerchannel'])) while True: await asyncio.wait([self.stop_future, message_future], return_when=asyncio.FIRST_COMPLETED) if message_future.done(): message = message_future.result().message log.info("Message from PubNub: %r", message) card_id = self._extract(message) if card_id is not None: await self._card(card_id) message_future = asyncio.ensure_future( listener.wait_for_message_on(config['cardviewerchannel'])) if self.stop_future.done(): break if not message_future.done(): message_future.cancel() pubnub.unsubscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_disconnect() pubnub.stop() log.info("Disconnected from PubNub")
def test_unsubscribe_all(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-subscribe-asyncio-messenger" ch = "test-subscribe-asyncio-unsubscribe-all-ch" ch1 = "test-subscribe-asyncio-unsubscribe-all-ch1" ch2 = "test-subscribe-asyncio-unsubscribe-all-ch2" ch3 = "test-subscribe-asyncio-unsubscribe-all-ch3" gr1 = "test-subscribe-asyncio-unsubscribe-all-gr1" gr2 = "test-subscribe-asyncio-unsubscribe-all-gr2" envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr1).channels(ch).future() assert envelope.status.original_response['status'] == 200 envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr2).channels(ch).future() assert envelope.status.original_response['status'] == 200 yield from sleeper(1) callback_messages = SubscribeListener() pubnub.add_listener(callback_messages) pubnub.subscribe().channels([ch1, ch2, ch3]).channel_groups([gr1, gr2]).execute() yield from callback_messages.wait_for_connect() assert len(pubnub.get_subscribed_channels()) == 3 assert len(pubnub.get_subscribed_channel_groups()) == 2 pubnub.unsubscribe_all() yield from callback_messages.wait_for_disconnect() assert len(pubnub.get_subscribed_channels()) == 0 assert len(pubnub.get_subscribed_channel_groups()) == 0 envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr1).channels(ch).future() assert envelope.status.original_response['status'] == 200 envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr2).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop()
def test_subscribe_publish_unsubscribe(event_loop): pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub' pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub' callback = SubscribeListener() channel = "test-subscribe-asyncio-ch" message = "hey" pubnub_sub.add_listener(callback) pubnub_sub.subscribe().channels(channel).execute() yield from callback.wait_for_connect() publish_future = asyncio.ensure_future(pubnub_pub.publish().channel(channel).message(message).future()) subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel)) yield from 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_sub.unsubscribe().channels(channel).execute() yield from callback.wait_for_disconnect() pubnub_pub.stop() pubnub_sub.stop()
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()
async def test_send(pubnub_bot, caplog): """ Test PubNub send """ caplog.set_level(logging.DEBUG, logger="sprinkler") listener = SubscribeListener() pubnub_bot.add_listener(listener) pubnub_bot.subscribe().channels('sprinkler').execute() listener.wait_for_connect() # Empty message queue while not listener.message_queue.empty(): await listener.message_queue.get() msg_con = messages.Messages(os.environ['SUBKEY'], os.environ['PUBKEY'], os.environ['PUBNUBID']) await msg_con.send("Test send message") msg = await listener.message_queue.get() await msg_con.stop() assert msg.message['content'] == "Test send message"
async def message_pump(self): pnconfig = PNConfiguration() pnconfig.subscribe_key = config['cardsubkey'] # Why aren't these the default settings? pnconfig.ssl = True pnconfig.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL pubnub = PubNubAsyncio(pnconfig) listener = SubscribeListener() pubnub.add_listener(listener) pubnub.subscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_connect() log.info("Connected to PubNub") message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel'])) while True: await asyncio.wait([self.stop_future, message_future], return_when=asyncio.FIRST_COMPLETED) if message_future.done(): message = message_future.result().message log.info("Message from PubNub: %r", message) card_id = self._extract(message) if card_id is not None: await self._card(card_id) message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel'])) if self.stop_future.done(): break if not message_future.done(): message_future.cancel() pubnub.unsubscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_disconnect() pubnub.stop() log.info("Disconnected from PubNub")
def test_cg_subscribe_publish_unsubscribe(event_loop, sleeper=asyncio.sleep): ch = "test-subscribe-asyncio-channel" gr = "test-subscribe-asyncio-group" message = "hey" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 yield from sleeper(1) callback_messages = SubscribeListener() pubnub.add_listener(callback_messages) pubnub.subscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_connect() subscribe_future = asyncio.ensure_future(callback_messages.wait_for_message_on(ch)) publish_future = asyncio.ensure_future(pubnub.publish().channel(ch).message(message).future()) yield from asyncio.wait([subscribe_future, publish_future]) sub_envelope = subscribe_future.result() pub_envelope = publish_future.result() 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 pubnub.unsubscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_disconnect() envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop()
def test_cg_subscribe_unsubscribe(event_loop, sleeper=asyncio.sleep): ch = "test-subscribe-asyncio-channel" gr = "test-subscribe-asyncio-group" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 yield from sleeper(3) callback_messages = SubscribeListener() pubnub.add_listener(callback_messages) pubnub.subscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_connect() pubnub.unsubscribe().channel_groups(gr).execute() yield from callback_messages.wait_for_disconnect() envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop()
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()
async def main(): my_listener = SubscribeListener() pubnub.add_listener(my_listener) pubnub.subscribe().channels("awesomeChannel").execute() await my_listener.wait_for_connect() print("connected") asyncio.ensure_future(pubnub.publish().channel("awesomeChannel").message({ 'fieldA': 'awesome', 'fieldB': 10 }).future()) result = await my_listener.wait_for_message_on("awesomeChannel") print(result.message) pubnub.unsubscribe().channels("awesomeChannel").execute() await my_listener.wait_for_disconnect() print("unsubscribed")
def test_join_leave(event_loop): channel = "test-subscribe-asyncio-join-leave-ch" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub_listener = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-subscribe-asyncio-messenger" pubnub_listener.config.uuid = "test-subscribe-asyncio-listener" callback_presence = SubscribeListener() callback_messages = SubscribeListener() pubnub_listener.add_listener(callback_presence) pubnub_listener.subscribe().channels(channel).with_presence().execute() yield from callback_presence.wait_for_connect() envelope = yield from callback_presence.wait_for_presence_on(channel) assert envelope.channel == channel assert envelope.event == 'join' assert envelope.uuid == pubnub_listener.uuid pubnub.add_listener(callback_messages) pubnub.subscribe().channels(channel).execute() yield from callback_messages.wait_for_connect() envelope = yield from callback_presence.wait_for_presence_on(channel) assert envelope.channel == channel assert envelope.event == 'join' assert envelope.uuid == pubnub.uuid pubnub.unsubscribe().channels(channel).execute() yield from callback_messages.wait_for_disconnect() envelope = yield from callback_presence.wait_for_presence_on(channel) assert envelope.channel == channel assert envelope.event == 'leave' assert envelope.uuid == pubnub.uuid pubnub_listener.unsubscribe().channels(channel).execute() yield from callback_presence.wait_for_disconnect() pubnub.stop() pubnub_listener.stop()
def test_cg_join_leave(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub_listener = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-subscribe-asyncio-messenger" pubnub_listener.config.uuid = "test-subscribe-asyncio-listener" ch = "test-subscribe-asyncio-join-leave-cg-channel" gr = "test-subscribe-asyncio-join-leave-cg-group" envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 yield from sleeper(1) callback_messages = SubscribeListener() callback_presence = SubscribeListener() pubnub_listener.add_listener(callback_presence) pubnub_listener.subscribe().channel_groups(gr).with_presence().execute() yield from callback_presence.wait_for_connect() prs_envelope = yield from 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() callback_messages_future = asyncio.ensure_future(callback_messages.wait_for_connect()) presence_messages_future = asyncio.ensure_future(callback_presence.wait_for_presence_on(ch)) yield from asyncio.wait([callback_messages_future, presence_messages_future]) prs_envelope = presence_messages_future.result() 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() callback_messages_future = asyncio.ensure_future(callback_messages.wait_for_disconnect()) presence_messages_future = asyncio.ensure_future(callback_presence.wait_for_presence_on(ch)) yield from asyncio.wait([callback_messages_future, presence_messages_future]) prs_envelope = presence_messages_future.result() 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() yield from callback_presence.wait_for_disconnect() envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop() pubnub_listener.stop()