|
5 | 5 |
|
6 | 6 | from unittest.mock import patch
|
7 | 7 | from pubnub.models.consumer.pubsub import PNMessageResult
|
| 8 | +from pubnub.models.consumer.message_type import PNMessageType |
8 | 9 | from pubnub.pubnub_asyncio import PubNubAsyncio, AsyncioEnvelope, SubscribeListener
|
9 | 10 | from tests.helper import pnconf_sub_copy, pnconf_enc_sub_copy
|
10 | 11 | from tests.integrational.vcr_asyncio_sleeper import get_sleeper, VCR599Listener, VCR599ReconnectionManager
|
@@ -382,3 +383,119 @@ async def test_unsubscribe_all(event_loop, sleeper=asyncio.sleep):
|
382 | 383 | assert envelope.status.original_response['status'] == 200
|
383 | 384 |
|
384 | 385 | await pubnub.stop()
|
| 386 | + |
| 387 | + |
| 388 | +@get_sleeper('tests/integrational/fixtures/asyncio/subscription/publish_space_id.yaml') |
| 389 | +@pn_vcr.use_cassette( |
| 390 | + 'tests/integrational/fixtures/asyncio/subscription/publish_space_id.yaml', |
| 391 | + filter_query_parameters=['pnsdk', 'l_cg', 'l_pres'], |
| 392 | + match_on=['method', 'scheme', 'host', 'port', 'string_list_in_path', 'string_list_in_query'], |
| 393 | +) |
| 394 | +@pytest.mark.asyncio |
| 395 | +async def test_subscribe_publish_space_id(event_loop, sleeper=asyncio.sleep): |
| 396 | + pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) |
| 397 | + pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) |
| 398 | + |
| 399 | + patch_pubnub(pubnub_sub) |
| 400 | + patch_pubnub(pubnub_pub) |
| 401 | + |
| 402 | + pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub' |
| 403 | + pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub' |
| 404 | + |
| 405 | + callback = VCR599Listener(1) |
| 406 | + channel = "test-subscribe-asyncio-ch" |
| 407 | + message = "hey" |
| 408 | + space_id = "HelloSpace" |
| 409 | + pubnub_sub.add_listener(callback) |
| 410 | + pubnub_sub.subscribe().channels(channel).execute() |
| 411 | + |
| 412 | + await callback.wait_for_connect() |
| 413 | + |
| 414 | + publish_future = asyncio.ensure_future( |
| 415 | + pubnub_pub.publish().channel(channel).message(message).space_id(space_id).future() |
| 416 | + ) |
| 417 | + subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel)) |
| 418 | + |
| 419 | + await asyncio.wait([ |
| 420 | + publish_future, |
| 421 | + subscribe_message_future |
| 422 | + ]) |
| 423 | + |
| 424 | + publish_envelope = publish_future.result() |
| 425 | + subscribe_envelope = subscribe_message_future.result() |
| 426 | + |
| 427 | + assert isinstance(subscribe_envelope, PNMessageResult) |
| 428 | + assert subscribe_envelope.channel == channel |
| 429 | + assert subscribe_envelope.subscription is None |
| 430 | + assert subscribe_envelope.message == message |
| 431 | + assert subscribe_envelope.space_id == space_id |
| 432 | + assert subscribe_envelope.timetoken > 0 |
| 433 | + |
| 434 | + assert isinstance(publish_envelope, AsyncioEnvelope) |
| 435 | + assert publish_envelope.result.timetoken > 0 |
| 436 | + assert publish_envelope.status.original_response[0] == 1 |
| 437 | + |
| 438 | + pubnub_sub.unsubscribe().channels(channel).execute() |
| 439 | + # await callback.wait_for_disconnect() |
| 440 | + |
| 441 | + pubnub_pub.stop() |
| 442 | + pubnub_sub.stop() |
| 443 | + |
| 444 | + |
| 445 | +@get_sleeper('tests/integrational/fixtures/asyncio/subscription/publish_message_type.yaml') |
| 446 | +@pn_vcr.use_cassette( |
| 447 | + 'tests/integrational/fixtures/asyncio/subscription/publish_message_type.yaml', |
| 448 | + filter_query_parameters=['pnsdk', 'l_cg', 'l_pres'], |
| 449 | + match_on=['method', 'scheme', 'host', 'port', 'string_list_in_path', 'string_list_in_query'], |
| 450 | +) |
| 451 | +@pytest.mark.asyncio |
| 452 | +async def test_subscribe_publish_message_type(event_loop, sleeper=asyncio.sleep): |
| 453 | + pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) |
| 454 | + pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) |
| 455 | + |
| 456 | + patch_pubnub(pubnub_sub) |
| 457 | + patch_pubnub(pubnub_pub) |
| 458 | + |
| 459 | + pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub' |
| 460 | + pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub' |
| 461 | + |
| 462 | + callback = VCR599Listener(1) |
| 463 | + channel = "test-subscribe-asyncio-ch" |
| 464 | + message = "hey" |
| 465 | + message_type = "MyOwnCustomMessageType" |
| 466 | + pubnub_sub.add_listener(callback) |
| 467 | + pubnub_sub.subscribe().channels(channel).execute() |
| 468 | + |
| 469 | + await callback.wait_for_connect() |
| 470 | + |
| 471 | + publish_future = asyncio.ensure_future( |
| 472 | + pubnub_pub.publish().channel(channel).message(message).message_type(message_type).future() |
| 473 | + ) |
| 474 | + subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel)) |
| 475 | + |
| 476 | + await asyncio.wait([ |
| 477 | + publish_future, |
| 478 | + subscribe_message_future |
| 479 | + ]) |
| 480 | + |
| 481 | + publish_envelope = publish_future.result() |
| 482 | + subscribe_envelope = subscribe_message_future.result() |
| 483 | + |
| 484 | + assert isinstance(subscribe_envelope, PNMessageResult) |
| 485 | + assert subscribe_envelope.channel == channel |
| 486 | + assert subscribe_envelope.subscription is None |
| 487 | + assert subscribe_envelope.message == message |
| 488 | + |
| 489 | + assert isinstance(subscribe_envelope.type, PNMessageType) |
| 490 | + assert str(subscribe_envelope.type) == message_type |
| 491 | + assert subscribe_envelope.timetoken > 0 |
| 492 | + |
| 493 | + assert isinstance(publish_envelope, AsyncioEnvelope) |
| 494 | + assert publish_envelope.result.timetoken > 0 |
| 495 | + assert publish_envelope.status.original_response[0] == 1 |
| 496 | + |
| 497 | + pubnub_sub.unsubscribe().channels(channel).execute() |
| 498 | + # await callback.wait_for_disconnect() |
| 499 | + |
| 500 | + pubnub_pub.stop() |
| 501 | + pubnub_sub.stop() |
0 commit comments