3 from __future__ import absolute_import, print_function
10 import arvnodeman.clientactor as clientactor
11 from . import testutil
13 class RemotePollLoopActorTestCase(testutil.RemotePollLoopActorTestMixin,
15 class MockClientError(Exception):
18 class TestActor(clientactor.RemotePollLoopActor):
19 LOGGER_NAME = 'arvnodeman.testpoll'
21 def _send_request(self):
23 TestActor.CLIENT_ERRORS = (MockClientError,)
24 TEST_CLASS = TestActor
27 def build_monitor(self, side_effect, *args, **kwargs):
28 super(RemotePollLoopActorTestCase, self).build_monitor(*args, **kwargs)
29 self.client.side_effect = side_effect
31 def test_poll_loop_starts_after_subscription(self):
32 self.build_monitor(['test1'])
33 self.monitor.subscribe(self.subscriber).get(self.TIMEOUT)
34 self.stop_proxy(self.monitor)
35 self.subscriber.assert_called_with('test1')
36 self.assertTrue(self.timer.schedule.called)
38 def test_poll_loop_continues_after_failure(self):
39 self.build_monitor(self.MockClientError)
40 self.monitor.subscribe(self.subscriber).get(self.TIMEOUT)
41 self.assertTrue(self.stop_proxy(self.monitor),
42 "poll loop died after error")
43 self.assertTrue(self.timer.schedule.called,
44 "poll loop did not reschedule after error")
45 self.assertFalse(self.subscriber.called,
46 "poll loop notified subscribers after error")
48 def test_late_subscribers_get_responses(self):
49 self.build_monitor(['pre_late_test', 'late_test'])
50 self.monitor.subscribe(lambda response: None).get(self.TIMEOUT)
51 self.monitor.subscribe(self.subscriber)
52 self.monitor.poll().get(self.TIMEOUT)
53 self.stop_proxy(self.monitor)
54 self.subscriber.assert_called_with('late_test')
56 def test_survive_dead_subscriptions(self):
57 self.build_monitor(['survive1', 'survive2'])
58 dead_subscriber = mock.Mock(name='dead_subscriber')
59 dead_subscriber.side_effect = pykka.ActorDeadError
60 self.monitor.subscribe(dead_subscriber)
61 self.monitor.subscribe(self.subscriber)
62 self.monitor.poll().get(self.TIMEOUT)
63 self.assertTrue(self.stop_proxy(self.monitor),
64 "poll loop died from dead subscriber")
65 self.subscriber.assert_called_with('survive2')
67 def check_poll_timers(self, *test_times):
68 schedule_mock = self.timer.schedule
70 with mock.patch('time.time') as time_mock:
71 for fake_time, expect_next in test_times:
72 time_mock.return_value = fake_time
73 self.monitor.poll(last_expect).get(self.TIMEOUT)
74 self.assertTrue(schedule_mock.called)
75 self.assertEqual(expect_next, schedule_mock.call_args[0][0])
76 schedule_mock.reset_mock()
77 last_expect = expect_next
79 def test_poll_timing_on_consecutive_successes_with_drift(self):
80 self.build_monitor(['1', '2'], poll_wait=3, max_poll_wait=14)
81 self.check_poll_timers((0, 3), (4, 6))
83 def test_poll_backoff_on_failures(self):
84 self.build_monitor(self.MockClientError, poll_wait=3, max_poll_wait=14)
85 self.check_poll_timers((0, 6), (6, 18), (18, 32))
87 def test_poll_timing_after_error_recovery(self):
88 self.build_monitor(['a', self.MockClientError(), 'b'],
89 poll_wait=3, max_poll_wait=14)
90 self.check_poll_timers((0, 3), (4, 10), (10, 13))
92 def test_no_subscriptions_by_key_without_support(self):
93 self.build_monitor([])
94 with self.assertRaises(AttributeError):
95 self.monitor.subscribe_to('key')
98 class RemotePollLoopActorWithKeysTestCase(testutil.RemotePollLoopActorTestMixin,
100 class TestActor(RemotePollLoopActorTestCase.TestActor):
101 def _item_key(self, item):
103 TEST_CLASS = TestActor
106 def build_monitor(self, side_effect, *args, **kwargs):
107 super(RemotePollLoopActorWithKeysTestCase, self).build_monitor(
109 self.client.side_effect = side_effect
111 def test_key_subscription(self):
112 self.build_monitor([[{'key': 1}, {'key': 2}]])
113 self.monitor.subscribe_to(2, self.subscriber).get(self.TIMEOUT)
114 self.stop_proxy(self.monitor)
115 self.subscriber.assert_called_with({'key': 2})
117 def test_survive_dead_key_subscriptions(self):
119 self.build_monitor([[item], [item]])
120 dead_subscriber = mock.Mock(name='dead_subscriber')
121 dead_subscriber.side_effect = pykka.ActorDeadError
122 self.monitor.subscribe_to(3, dead_subscriber)
123 self.monitor.subscribe_to(3, self.subscriber)
124 self.monitor.poll().get(self.TIMEOUT)
125 self.assertTrue(self.stop_proxy(self.monitor),
126 "poll loop died from dead key subscriber")
127 self.subscriber.assert_called_with(item)
129 def test_mixed_subscriptions(self):
131 self.build_monitor([[item], [item]])
132 key_subscriber = mock.Mock(name='key_subscriber')
133 self.monitor.subscribe(self.subscriber)
134 self.monitor.subscribe_to(4, key_subscriber)
135 self.monitor.poll().get(self.TIMEOUT)
136 self.stop_proxy(self.monitor)
137 self.subscriber.assert_called_with([item])
138 key_subscriber.assert_called_with(item)
140 def test_subscription_to_missing_key(self):
141 self.build_monitor([[]])
142 self.monitor.subscribe_to('nonesuch', self.subscriber).get(self.TIMEOUT)
143 self.stop_proxy(self.monitor)
144 self.subscriber.assert_called_with(None)
147 if __name__ == '__main__':