/
test_wamp_component_aio.py
153 lines (133 loc) · 5.96 KB
/
test_wamp_component_aio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import os
import sys
import unittest.mock as mock
import pytest
import txaio
if os.environ.get('USE_ASYNCIO', False):
from autobahn.asyncio.component import Component
@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires Python 3.5+")
@pytest.mark.asyncio(forbid_global_loop=True)
def test_asyncio_component(event_loop):
orig_loop = txaio.config.loop
txaio.config.loop = event_loop
comp = Component(
transports=[
{
"url": "ws://localhost:12/bogus",
"max_retries": 1,
"max_retry_delay": 0.1,
}
]
)
# if having trouble, try starting some logging (and use
# "py.test -s" to get real-time output)
# txaio.start_logging(level="debug")
f = comp.start(loop=event_loop)
txaio.config.loop = event_loop
finished = txaio.create_future()
def fail():
finished.set_exception(AssertionError("timed out"))
txaio.config.loop = orig_loop
txaio.call_later(4.0, fail)
def done(f):
try:
f.result()
finished.set_exception(AssertionError("should get an error"))
except RuntimeError as e:
if 'Exhausted all transport connect attempts' not in str(e):
finished.set_exception(AssertionError("wrong exception caught"))
finished.set_result(None)
txaio.config.loop = orig_loop
assert comp._done_f is None
f.add_done_callback(done)
return finished
@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires Python 3.5+")
@pytest.mark.asyncio(forbid_global_loop=True)
def test_asyncio_component_404(event_loop):
"""
If something connects but then gets aborted, it should still try
to re-connect (in real cases this could be e.g. wrong path,
TLS failure, WebSocket handshake failure, etc)
"""
orig_loop = txaio.config.loop
txaio.config.loop = event_loop
class FakeTransport(object):
def close(self):
pass
def write(self, data):
pass
fake_transport = FakeTransport()
actual_protocol = [None] # set in a closure below
def create_connection(protocol_factory=None, server_hostname=None, host=None, port=None, ssl=False):
if actual_protocol[0] is None:
protocol = protocol_factory()
actual_protocol[0] = protocol
protocol.connection_made(fake_transport)
return txaio.create_future_success((fake_transport, protocol))
else:
return txaio.create_future_error(RuntimeError("second connection fails completely"))
with mock.patch.object(event_loop, 'create_connection', create_connection):
event_loop.create_connection = create_connection
comp = Component(
transports=[
{
"url": "ws://localhost:12/bogus",
"max_retries": 1,
"max_retry_delay": 0.1,
}
]
)
# if having trouble, try starting some logging (and use
# "py.test -s" to get real-time output)
# txaio.start_logging(level="debug")
f = comp.start(loop=event_loop)
txaio.config.loop = event_loop
# now that we've started connecting, we *should* be able
# to connetion_lost our transport .. but we do a
# call-later to ensure we're after the setup stuff in the
# event-loop (because asyncio doesn't synchronously
# process already-completed Futures like Twisted does)
def nuke_transport():
actual_protocol[0].connection_lost(None) # asyncio can call this with None
txaio.call_later(0.1, nuke_transport)
finished = txaio.create_future()
def fail():
finished.set_exception(AssertionError("timed out"))
txaio.config.loop = orig_loop
txaio.call_later(1.0, fail)
def done(f):
try:
f.result()
finished.set_exception(AssertionError("should get an error"))
except RuntimeError as e:
if 'Exhausted all transport connect attempts' not in str(e):
finished.set_exception(AssertionError("wrong exception caught"))
finished.set_result(None)
txaio.config.loop = orig_loop
f.add_done_callback(done)
return finished