Skip to content

Commit 7f2db7b

Browse files
committed
Improve tests cleanup
1 parent 65935d8 commit 7f2db7b

4 files changed

Lines changed: 53 additions & 74 deletions

File tree

tests/integrational/asyncio/test_change_uuid.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import pytest
32

43
from pubnub.models.consumer.signal import PNSignalResult
@@ -31,6 +30,8 @@ async def test_change_uuid():
3130
assert isinstance(envelope.result, PNSignalResult)
3231
assert isinstance(envelope.status, PNStatus)
3332

33+
await pn.stop()
34+
3435

3536
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/signal/uuid_no_lock.json',
3637
filter_query_parameters=['seqn', 'pnsdk', 'l_sig'], serializer='pn_json')
@@ -52,25 +53,15 @@ async def test_change_uuid_no_lock():
5253
assert isinstance(envelope.result, PNSignalResult)
5354
assert isinstance(envelope.status, PNStatus)
5455

55-
56-
@pytest.fixture
57-
def event_loop():
58-
loop = asyncio.new_event_loop()
59-
asyncio.set_event_loop(loop)
60-
try:
61-
yield loop
62-
finally:
63-
loop.run_until_complete(asyncio.sleep(0))
64-
asyncio.set_event_loop(None)
65-
loop.close()
56+
await pn.stop()
6657

6758

68-
def test_uuid_validation_at_init(event_loop):
59+
def test_uuid_validation_at_init():
6960
with pytest.raises(AssertionError) as exception:
7061
pnconf = PNConfiguration()
7162
pnconf.publish_key = "demo"
7263
pnconf.subscribe_key = "demo"
73-
PubNubAsyncio(pnconf, custom_event_loop=event_loop)
64+
PubNubAsyncio(pnconf)
7465

7566
assert str(exception.value) == 'UUID missing or invalid type'
7667

tests/integrational/asyncio/test_heartbeat.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
import pubnub as pn
6-
from pubnub.pubnub_asyncio import AsyncioSubscriptionManager, PubNubAsyncio, SubscribeListener
6+
from pubnub.pubnub_asyncio import PubNubAsyncio, SubscribeListener
77
from tests import helper
88
from tests.helper import pnconf_env_copy
99

@@ -21,54 +21,54 @@ async def test_timeout_event_on_broken_heartbeat():
2121
listener_config = pnconf_env_copy(uuid=helper.gen_channel("listener"), enable_subscribe=True)
2222
pubnub_listener = PubNubAsyncio(listener_config)
2323

24-
# - connect to :ch-pnpres
25-
callback_presence = SubscribeListener()
26-
pubnub_listener.add_listener(callback_presence)
27-
pubnub_listener.subscribe().channels(ch).with_presence().execute()
28-
await callback_presence.wait_for_connect()
24+
try:
25+
# - connect to :ch-pnpres
26+
callback_presence = SubscribeListener()
27+
pubnub_listener.add_listener(callback_presence)
28+
pubnub_listener.subscribe().channels(ch).with_presence().execute()
29+
await callback_presence.wait_for_connect()
2930

30-
envelope = await callback_presence.wait_for_presence_on(ch)
31-
assert ch == envelope.channel
32-
assert 'join' == envelope.event
33-
assert pubnub_listener.uuid == envelope.uuid
31+
envelope = await callback_presence.wait_for_presence_on(ch)
32+
assert ch == envelope.channel
33+
assert 'join' == envelope.event
34+
assert pubnub_listener.uuid == envelope.uuid
3435

35-
# # - connect to :ch
36-
callback_messages = SubscribeListener()
37-
pubnub.add_listener(callback_messages)
38-
pubnub.subscribe().channels(ch).execute()
36+
# # - connect to :ch
37+
callback_messages = SubscribeListener()
38+
pubnub.add_listener(callback_messages)
39+
pubnub.subscribe().channels(ch).execute()
3940

40-
useless_connect_future = asyncio.ensure_future(callback_messages.wait_for_connect())
41-
presence_future = asyncio.ensure_future(callback_presence.wait_for_presence_on(ch))
41+
useless_connect_future = asyncio.ensure_future(callback_messages.wait_for_connect())
42+
presence_future = asyncio.ensure_future(callback_presence.wait_for_presence_on(ch))
4243

43-
# - assert join event
44-
await asyncio.wait([useless_connect_future, presence_future])
44+
# - assert join event
45+
await asyncio.wait([useless_connect_future, presence_future], return_when=asyncio.ALL_COMPLETED)
4546

46-
prs_envelope = presence_future.result()
47+
prs_envelope = presence_future.result()
4748

48-
assert ch == prs_envelope.channel
49-
assert 'join' == prs_envelope.event
50-
assert pubnub.uuid == prs_envelope.uuid
51-
# - break messenger heartbeat loop
52-
pubnub._subscription_manager._stop_heartbeat_timer()
49+
assert ch == prs_envelope.channel
50+
assert 'join' == prs_envelope.event
51+
assert pubnub.uuid == prs_envelope.uuid
52+
# - break messenger heartbeat loop
53+
pubnub._subscription_manager._stop_heartbeat_timer()
5354

54-
# wait for one heartbeat call
55-
await asyncio.sleep(8)
55+
# wait for one heartbeat call
56+
await asyncio.sleep(8)
5657

57-
# - assert for timeout
58-
envelope = await callback_presence.wait_for_presence_on(ch)
59-
assert ch == envelope.channel
60-
assert 'timeout' == envelope.event
61-
assert pubnub.uuid == envelope.uuid
58+
# - assert for timeout
59+
envelope = await callback_presence.wait_for_presence_on(ch)
60+
assert ch == envelope.channel
61+
assert 'timeout' == envelope.event
62+
assert pubnub.uuid == envelope.uuid
6263

63-
pubnub.unsubscribe().channels(ch).execute()
64-
if isinstance(pubnub._subscription_manager, AsyncioSubscriptionManager):
64+
pubnub.unsubscribe().channels(ch).execute()
6565
await callback_messages.wait_for_disconnect()
6666

67-
# - disconnect from :ch-pnpres
68-
pubnub_listener.unsubscribe().channels(ch).execute()
69-
if isinstance(pubnub._subscription_manager, AsyncioSubscriptionManager):
67+
# - disconnect from :ch-pnpres
68+
pubnub_listener.unsubscribe().channels(ch).execute()
7069
await callback_presence.wait_for_disconnect()
7170

72-
await pubnub.stop()
73-
await pubnub_listener.stop()
74-
await asyncio.sleep(0.5)
71+
finally:
72+
await pubnub.stop()
73+
await pubnub_listener.stop()
74+
await asyncio.sleep(0.5)

tests/integrational/asyncio/test_message_count.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import asyncio
21
import pytest
2+
import pytest_asyncio
33

44
from pubnub.pubnub_asyncio import PubNubAsyncio
55
from pubnub.models.envelopes import AsyncioEnvelope
@@ -9,27 +9,13 @@
99
from tests.integrational.vcr_helper import pn_vcr
1010

1111

12-
@pytest.fixture
13-
def event_loop():
14-
loop = asyncio.new_event_loop()
15-
asyncio.set_event_loop(loop)
16-
try:
17-
yield loop
18-
finally:
19-
loop.run_until_complete(asyncio.sleep(0))
20-
asyncio.set_event_loop(None)
21-
loop.close()
22-
23-
24-
@pytest.fixture
25-
def pn(event_loop):
12+
@pytest_asyncio.fixture
13+
async def pn():
2614
config = pnconf_mc_copy()
2715
config.enable_subscribe = False
28-
pn = PubNubAsyncio(config, custom_event_loop=event_loop)
29-
try:
30-
yield pn
31-
finally:
32-
event_loop.run_until_complete(pn.stop())
16+
pn = PubNubAsyncio(config)
17+
yield pn
18+
await pn.stop()
3319

3420

3521
@pn_vcr.use_cassette(

tests/pytest.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ filterwarnings =
66
ignore:The function .* is deprecated. Use.* Include Object instead:DeprecationWarning
77
ignore:The function .* is deprecated. Use.* PNUserMember class instead:DeprecationWarning
88

9-
asyncio_default_fixture_loop_scope = module
9+
asyncio_default_fixture_loop_scope = function
10+
timeout = 60
11+
timeout_func_only = true

0 commit comments

Comments
 (0)