Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Jul 27, 2023
1 parent dd67ffc commit e6b8e71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
1 change: 0 additions & 1 deletion pubnub/event_engine/manage_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def emit_message(self, effect: effects.EmitMessagesEffect):
user_metadata=subscribe_message.publish_metadata,
publisher=subscribe_message.issuing_client_id
)
pn_message_result = 'fo'
self.pubnub._subscription_manager._listener_manager.announce_message(pn_message_result)

def emit_status(self, effect: effects.EmitStatusEffect):
Expand Down
52 changes: 35 additions & 17 deletions tests/functional/event_engine/test_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,49 @@

from pubnub.pubnub_asyncio import PubNubAsyncio, EventEngineSubscriptionManager, SubscribeCallback
from pubnub.event_engine.models import states
from pubnub.models.consumer.common import PNStatus
from pubnub.enums import PNStatusCategory

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)


class TestCallback(SubscribeCallback):
def status(self, pubnub, status: PNStatus):
assert status.error is False
assert status.category is PNStatusCategory.PNConnectedCategory
self.status_called()

def message(self, pubnub, message):
assert message.channel == 'foo'
assert message.message == 'test'
self.message_called()

def status_called(self):
pass

def message_called(self):
pass


@pytest.mark.asyncio
async def test_subscribe_triggers_event():
@patch.object(TestCallback, 'status_called')
@patch.object(TestCallback, 'message_called')
async def test_subscribe_triggers_event(mocked_status, mocked_message):
config = pnconf_env_copy()
config.enable_subscribe = True

callback = TestCallback()
pubnub = PubNubAsyncio(config, subscription_manager=EventEngineSubscriptionManager)

with patch.object(SubscribeCallback, 'status') as mocked_status, \
patch.object(SubscribeCallback, 'message') as mocked_message:
callback = SubscribeCallback()
pubnub.add_listener(callback)
pubnub.subscribe().channels('foo').execute()
await delayed_publish('foo', 'test', 2)
await asyncio.sleep(5)
assert pubnub._subscription_manager.event_engine.get_state_name() == states.ReceivingState.__name__
mocked_status.assert_called()
mocked_message.assert_called()
pubnub.unsubscribe_all()
await asyncio.sleep(2)
pubnub._subscription_manager.stop()
await asyncio.sleep(0.1)
pubnub.add_listener(callback)
pubnub.subscribe().channels('foo').execute()
await delayed_publish('foo', 'test', 1)
await asyncio.sleep(5)
assert pubnub._subscription_manager.event_engine.get_state_name() == states.ReceivingState.__name__
mocked_status.assert_called()
mocked_message.assert_called()
pubnub.unsubscribe_all()
await asyncio.sleep(1)
pubnub._subscription_manager.stop()
await asyncio.sleep(0.1)


async def delayed_publish(channel, message, delay):
Expand Down

0 comments on commit e6b8e71

Please sign in to comment.