File: test_application_runner.py

package info (click to toggle)
python-autobahn 0.14.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,132 kB
  • ctags: 3,069
  • sloc: python: 18,460; makefile: 285; sh: 3
file content (126 lines) | stat: -rw-r--r-- 5,090 bytes parent folder | download
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
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Tavendo 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.
#
###############################################################################

from __future__ import absolute_import

# t.i.reactor doesn't exist until we've imported it once, but we
# need it to exist so we can @patch it out in the tests ...
from twisted.internet import reactor  # noqa
from twisted.internet.defer import inlineCallbacks, succeed
from twisted.trial import unittest

from mock import patch, Mock

from autobahn.twisted.wamp import ApplicationRunner


def raise_error(*args, **kw):
    raise RuntimeError("we always fail")


class TestApplicationRunner(unittest.TestCase):
    @patch('twisted.internet.reactor')
    def test_runner_default(self, fakereactor):
        fakereactor.connectTCP = Mock(side_effect=raise_error)
        runner = ApplicationRunner(u'ws://fake:1234/ws', u'dummy realm')

        # we should get "our" RuntimeError when we call run
        self.assertRaises(RuntimeError, runner.run, raise_error)

        # both reactor.run and reactor.stop should have been called
        self.assertEqual(fakereactor.run.call_count, 1)
        self.assertEqual(fakereactor.stop.call_count, 1)

    @patch('twisted.internet.reactor')
    @inlineCallbacks
    def test_runner_no_run(self, fakereactor):
        fakereactor.connectTCP = Mock(side_effect=raise_error)
        runner = ApplicationRunner(u'ws://fake:1234/ws', u'dummy realm')

        try:
            yield runner.run(raise_error, start_reactor=False)
            self.fail()  # should have raise an exception, via Deferred

        except RuntimeError as e:
            # make sure it's "our" exception
            self.assertEqual(e.args[0], "we always fail")

        # neither reactor.run() NOR reactor.stop() should have been called
        # (just connectTCP() will have been called)
        self.assertEqual(fakereactor.run.call_count, 0)
        self.assertEqual(fakereactor.stop.call_count, 0)

    @patch('twisted.internet.reactor')
    def test_runner_no_run_happypath(self, fakereactor):
        proto = Mock()
        fakereactor.connectTCP = Mock(return_value=succeed(proto))
        runner = ApplicationRunner(u'ws://fake:1234/ws', u'dummy realm')

        d = runner.run(Mock(), start_reactor=False)

        # shouldn't have actually connected to anything
        # successfully, and the run() call shouldn't have inserted
        # any of its own call/errbacks. (except the cleanup handler)
        self.assertFalse(d.called)
        self.assertEqual(1, len(d.callbacks))

        # neither reactor.run() NOR reactor.stop() should have been called
        # (just connectTCP() will have been called)
        self.assertEqual(fakereactor.run.call_count, 0)
        self.assertEqual(fakereactor.stop.call_count, 0)

    @patch('twisted.internet.reactor')
    def test_runner_bad_proxy(self, fakereactor):
        proxy = u'myproxy'

        self.assertRaises(
            AssertionError,
            ApplicationRunner,
            u'ws://fake:1234/ws', u'dummy realm',
            proxy=proxy
        )

    @patch('twisted.internet.reactor')
    def test_runner_proxy(self, fakereactor):
        proto = Mock()
        fakereactor.connectTCP = Mock(return_value=succeed(proto))

        proxy = {'host': u'myproxy', 'port': 3128}

        runner = ApplicationRunner(u'ws://fake:1234/ws', u'dummy realm', proxy=proxy)

        d = runner.run(Mock(), start_reactor=False)

        # shouldn't have actually connected to anything
        # successfully, and the run() call shouldn't have inserted
        # any of its own call/errbacks. (except the cleanup handler)
        self.assertFalse(d.called)
        self.assertEqual(1, len(d.callbacks))

        # neither reactor.run() NOR reactor.stop() should have been called
        # (just connectTCP() will have been called)
        self.assertEqual(fakereactor.run.call_count, 0)
        self.assertEqual(fakereactor.stop.call_count, 0)