Debian Codegraph @

tests/stubs/io_services_test_stubs.py

raw
1 """
2 Test stubs for running tests against all supported adaptations of
3 nbio_interface.AbstractIOServices and variations such as without SSL and
4 with SSL.
5
6 Usage example:
7
8 ```
9 import unittest
10
11 from ..io_services_test_stubs import IOServicesTestStubs
12
13
14 class TestGetNativeIOLoop(unittest.TestCase,
15 IOServicesTestStubs):
16
17 def start(self):
18 native_loop = self.create_nbio().get_native_ioloop()
19 self.assertIsNotNone(self._native_loop)
20 self.assertIs(native_loop, self._native_loop)
21 ```
22
23 """
24
25 from tests.wrappers.threaded_test_wrapper import run_in_thread_with_timeout
26
27 # Suppress missing-docstring to allow test method names to be printed by our the
28 # test runner
29 # pylint: disable=C0111
30
31 # invalid-name
32 # pylint: disable=C0103
33
34
35 class IOServicesTestStubs(object):
36 """Provides a stub test method for each combination of parameters we wish to
37 test
38
39 """
40 # Overridden by framework-specific test methods
41 _nbio_factory = None
42 _native_loop = None
43 _use_ssl = None
44
45 def start(self):
46 """ Subclasses must override to run the test. This method is called
47 from a thread.
48
49 """
50 raise NotImplementedError
51
52 def create_nbio(self):
53 """Create the configured AbstractIOServices adaptation and schedule
54 it to be closed automatically when the test terminates.
55
56 :param unittest.TestCase self:
57 :rtype: pika.adapters.utils.nbio_interface.AbstractIOServices
58
59 """
60 nbio = self._nbio_factory()
61 self.addCleanup(nbio.close) # pylint: disable=E1101
62 return nbio
63
64 def _run_start(self, nbio_factory, native_loop, use_ssl=False):
65 """Called by framework-specific test stubs to initialize test paramters
66 and execute the `self.start()` method.
67
68 :param nbio_interface.AbstractIOServices _() nbio_factory: function
69 to call to create an instance of `AbstractIOServices` adaptation.
70 :param native_loop: native loop implementation instance
71 :param bool use_ssl: Whether to test with SSL instead of Plaintext
72 transport. Defaults to Plaintext.
73 """
74 self._nbio_factory = nbio_factory
75 self._native_loop = native_loop
76 self._use_ssl = use_ssl
77
78 self.start()
79
80 # Suppress missing-docstring to allow test method names to be printed by our
81 # test runner
82 # pylint: disable=C0111
83
84 @run_in_thread_with_timeout
85 def test_with_select_connection_io_services(self):
86 # Test entry point for `select_connection.IOLoop`-based async services
87 # implementation.
88
89 from pika.adapters.select_connection import IOLoop
90 from pika.adapters.utils.selector_ioloop_adapter import (
91 SelectorIOServicesAdapter)
92 native_loop = IOLoop()
93 self._run_start(
94 nbio_factory=lambda: SelectorIOServicesAdapter(native_loop),
95 native_loop=native_loop)
96
97 @run_in_thread_with_timeout
98 def test_with_tornado_io_services(self):
99 # Test entry point for `tornado.ioloop.IOLoop`-based async services
100 # implementation.
101
102 from tornado.ioloop import IOLoop
103 from pika.adapters.utils.selector_ioloop_adapter import (
104 SelectorIOServicesAdapter)
105
106 native_loop = IOLoop()
107 self._run_start(
108 nbio_factory=lambda: SelectorIOServicesAdapter(native_loop),
109 native_loop=native_loop)
110
111 @run_in_thread_with_timeout
112 def test_with_asyncio_io_services(self):
113 # Test entry point for `asyncio` event loop-based io services
114 # implementation.
115
116 import asyncio
117 from pika.adapters.asyncio_connection import (_AsyncioIOServicesAdapter)
118
119 native_loop = asyncio.new_event_loop()
120 self._run_start(
121 nbio_factory=lambda: _AsyncioIOServicesAdapter(native_loop),
122 native_loop=native_loop)