-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy pathtest_asyncioreactor.py
77 lines (61 loc) · 2.38 KB
/
test_asyncioreactor.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
AsyncioConnection, ASYNCIO_AVAILABLE = None, False
try:
from cassandra.io.asyncioreactor import AsyncioConnection
import asynctest
ASYNCIO_AVAILABLE = True
except (ImportError, SyntaxError):
AsyncioConnection = None
ASYNCIO_AVAILABLE = False
from tests import is_monkey_patched, connection_class
from tests.unit.io.utils import TimerCallback, TimerTestMixin
from unittest.mock import patch
import unittest
import time
skip_me = (is_monkey_patched() or
(not ASYNCIO_AVAILABLE) or
(connection_class is not AsyncioConnection))
@unittest.skipIf(is_monkey_patched(), 'runtime is monkey patched for another reactor')
@unittest.skipIf(connection_class is not AsyncioConnection,
'not running asyncio tests; current connection_class is {}'.format(connection_class))
@unittest.skipUnless(ASYNCIO_AVAILABLE, "asyncio is not available for this runtime")
class AsyncioTimerTests(TimerTestMixin, unittest.TestCase):
@classmethod
def setUpClass(cls):
if skip_me:
return
cls.connection_class = AsyncioConnection
AsyncioConnection.initialize_reactor()
@classmethod
def tearDownClass(cls):
if skip_me:
return
if ASYNCIO_AVAILABLE and AsyncioConnection._loop:
AsyncioConnection._loop.stop()
@property
def create_timer(self):
return self.connection.create_timer
@property
def _timers(self):
raise RuntimeError('no TimerManager for AsyncioConnection')
def setUp(self):
if skip_me:
return
socket_patcher = patch('socket.socket')
self.addCleanup(socket_patcher.stop)
socket_patcher.start()
old_selector = AsyncioConnection._loop._selector
AsyncioConnection._loop._selector = asynctest.TestSelector()
def reset_selector():
AsyncioConnection._loop._selector = old_selector
self.addCleanup(reset_selector)
super(AsyncioTimerTests, self).setUp()
def test_timer_cancellation(self):
# Various lists for tracking callback stage
timeout = .1
callback = TimerCallback(timeout)
timer = self.create_timer(timeout, callback.invoke)
timer.cancel()
# Release context allow for timer thread to run.
time.sleep(.2)
# Assert that the cancellation was honored
self.assertFalse(callback.was_invoked())