3 from datetime import datetime, timedelta, tzinfo
11 class WebsocketTest(run_test_server.TestCaseWithServers):
14 TIME_PAST = time.time()-3600
15 TIME_FUTURE = time.time()+3600
23 super(WebsocketTest, self).tearDown()
24 run_test_server.reset()
26 def _test_subscribe(self, poll_fallback, expect_type, start_time=None, expected=1):
27 run_test_server.authorize_with('active')
28 events = Queue.Queue(100)
30 # Create ancestor before subscribing.
31 # When listening with start_time in the past, this should also be retrieved.
32 # However, when start_time is omitted in subscribe, this should not be fetched.
33 ancestor = arvados.api('v1').humans().create(body={}).execute()
35 filters = [['object_uuid', 'is_a', 'arvados#human']]
37 filters.append(['created_at', '>=', start_time])
39 self.ws = arvados.events.subscribe(
40 arvados.api('v1'), filters,
42 poll_fallback=poll_fallback,
43 last_log_id=(1 if start_time else None))
44 self.assertIsInstance(self.ws, expect_type)
45 self.assertEqual(200, events.get(True, 5)['status'])
46 human = arvados.api('v1').humans().create(body={}).execute()
49 for i in range(0, expected):
50 log_object_uuids.append(events.get(True, 5)['object_uuid'])
53 self.assertIn(human['uuid'], log_object_uuids)
56 self.assertIn(ancestor['uuid'], log_object_uuids)
58 with self.assertRaises(Queue.Empty):
59 # assertEqual just serves to show us what unexpected thing
60 # comes out of the queue when the assertRaises fails; when
61 # the test passes, this assertEqual doesn't get called.
62 self.assertEqual(events.get(True, 2), None)
64 def test_subscribe_websocket(self):
66 poll_fallback=False, expect_type=arvados.events.EventClient, expected=1)
68 @mock.patch('arvados.events.EventClient.__init__')
69 def test_subscribe_poll(self, event_client_constr):
70 event_client_constr.side_effect = Exception('All is well')
72 poll_fallback=0.25, expect_type=arvados.events.PollClient, expected=1)
74 def test_subscribe_websocket_with_start_time_past(self):
76 poll_fallback=False, expect_type=arvados.events.EventClient,
77 start_time=self.localiso(self.TIME_PAST),
80 @mock.patch('arvados.events.EventClient.__init__')
81 def test_subscribe_poll_with_start_time_past(self, event_client_constr):
82 event_client_constr.side_effect = Exception('All is well')
84 poll_fallback=0.25, expect_type=arvados.events.PollClient,
85 start_time=self.localiso(self.TIME_PAST),
88 def test_subscribe_websocket_with_start_time_future(self):
90 poll_fallback=False, expect_type=arvados.events.EventClient,
91 start_time=self.localiso(self.TIME_FUTURE),
94 @mock.patch('arvados.events.EventClient.__init__')
95 def test_subscribe_poll_with_start_time_future(self, event_client_constr):
96 event_client_constr.side_effect = Exception('All is well')
98 poll_fallback=0.25, expect_type=arvados.events.PollClient,
99 start_time=self.localiso(self.TIME_FUTURE),
102 def test_subscribe_websocket_with_start_time_past_utc(self):
103 self._test_subscribe(
104 poll_fallback=False, expect_type=arvados.events.EventClient,
105 start_time=self.utciso(self.TIME_PAST),
108 def test_subscribe_websocket_with_start_time_future_utc(self):
109 self._test_subscribe(
110 poll_fallback=False, expect_type=arvados.events.EventClient,
111 start_time=self.utciso(self.TIME_FUTURE),
115 return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(t))
117 def localiso(self, t):
118 return time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(t)) + self.isotz(-time.timezone/60)
120 def isotz(self, offset):
121 """Convert minutes-east-of-UTC to ISO8601 time zone designator"""
122 return '{:+03d}{:02d}'.format(offset/60, offset%60)