Skip to content

Commit 8aed80f

Browse files
committed
Test to ensure we get a connection error from ApplicationRunner.run()
The Deferred returned by connect() doesn't get an errback added to it, so if that failed the reactor will keep running and the user won't know.
1 parent 6d448ff commit 8aed80f

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: python
22

33
install:
4-
- pip install tox
4+
- pip install tox
55

66
env:
77
- TOX_ENV=flake8
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
###############################################################################
2+
##
3+
# Copyright (C) 2014 Tavendo GmbH
4+
##
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
##
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
##
17+
###############################################################################
18+
19+
from __future__ import absolute_import
20+
21+
try:
22+
import unittest2 as unittest
23+
except ImportError:
24+
import unittest
25+
from mock import patch
26+
27+
28+
class FakeReactor:
29+
'''
30+
This just fakes out enough reactor methods so .run() can work.
31+
'''
32+
stop_called = False
33+
34+
def __init__(self, to_raise):
35+
self.stop_called = False
36+
self.to_raise = to_raise
37+
38+
def run(self, *args, **kw):
39+
raise self.to_raise
40+
41+
def stop(self):
42+
self.stop_called = True
43+
44+
def connectTCP(self, *args, **kw):
45+
raise RuntimeError("ConnectTCP shouldn't get called")
46+
47+
48+
class TestWampTwistedRunner(unittest.TestCase):
49+
50+
def test_connect_error(self):
51+
'''
52+
Ensure the runner doesn't swallow errors and that it exits the
53+
reactor properly if there is one.
54+
'''
55+
try:
56+
from autobahn.twisted.wamp import ApplicationRunner
57+
from twisted.internet.error import ConnectionRefusedError
58+
# the 'reactor' member doesn't exist until we import it
59+
from twisted.internet import reactor # noqa: F401
60+
except ImportError:
61+
raise unittest.SkipTest('No twisted')
62+
63+
runner = ApplicationRunner('ws://localhost:1', 'realm')
64+
exception = ConnectionRefusedError("It's a trap!")
65+
66+
with patch('twisted.internet.reactor', FakeReactor(exception)) as mockreactor:
67+
self.assertRaises(
68+
ConnectionRefusedError,
69+
# pass a no-op session-creation method
70+
runner.run, lambda _: None, start_reactor=True
71+
)
72+
self.assertTrue(mockreactor.stop_called)
73+
74+
if __name__ == '__main__':
75+
unittest.main()

autobahn/tox.ini

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ whitelist_externals = sh
1616

1717

1818
[testenv:flake8]
19-
deps = flake8
19+
deps =
20+
flake8
21+
mock
2022
commands =
2123
sh -c "which python"
2224
python -V
@@ -29,6 +31,7 @@ basepython = python2.7
2931
deps =
3032
twisted
3133
unittest2
34+
mock
3235
commands =
3336
sh -c "which python"
3437
sh -c "which trial"
@@ -45,6 +48,7 @@ setenv =
4548
deps =
4649
pytest
4750
unittest2
51+
mock
4852
commands =
4953
sh -c "which python"
5054
python -V
@@ -56,7 +60,9 @@ setenv =
5660

5761

5862
[testenv:py27twisted]
59-
deps = twisted
63+
deps =
64+
twisted
65+
mock
6066
commands =
6167
sh -c "which python"
6268
sh -c "which trial"
@@ -70,7 +76,9 @@ setenv =
7076

7177

7278
[testenv:py27asyncio]
73-
deps = pytest
79+
deps =
80+
pytest
81+
mock
7482
commands =
7583
sh -c "which python"
7684
python -V
@@ -82,7 +90,9 @@ setenv =
8290

8391

8492
[testenv:py33asyncio]
85-
deps = pytest
93+
deps =
94+
pytest
95+
mock
8696
commands =
8797
sh -c "which python"
8898
python -V
@@ -94,7 +104,9 @@ setenv =
94104

95105

96106
[testenv:py34asyncio]
97-
deps = pytest
107+
deps =
108+
pytest
109+
mock
98110
commands =
99111
sh -c "which python"
100112
python -V
@@ -106,7 +118,9 @@ setenv =
106118

107119

108120
[testenv:pypy2twisted]
109-
deps = twisted
121+
deps =
122+
twisted
123+
mock
110124
commands =
111125
sh -c "which python"
112126
sh -c "which trial"
@@ -120,7 +134,9 @@ setenv =
120134

121135

122136
[testenv:pypy2asyncio]
123-
deps = pytest
137+
deps =
138+
pytest
139+
mock
124140
commands =
125141
sh -c "which python"
126142
python -V

0 commit comments

Comments
 (0)