8 from datetime import datetime, timedelta
11 class WebsocketTest(run_test_server.TestCaseWithServers):
20 super(WebsocketTest, self).tearDown()
22 def _test_subscribe(self, poll_fallback, expect_type, last_log_id=None, additional_filters=None, expected=1):
23 run_test_server.authorize_with('active')
24 events = Queue.Queue(100)
26 # Create ancestor before subscribing.
27 # When listening with start_time in the past, this should also be retrieved.
28 # However, when start_time is omitted in subscribe, this should not be fetched.
29 ancestor = arvados.api('v1').humans().create(body={}).execute()
32 filters = [['object_uuid', 'is_a', 'arvados#human']]
33 if additional_filters:
34 filters = filters + additional_filters
36 self.ws = arvados.events.subscribe(
37 arvados.api('v1'), filters,
38 events.put, poll_fallback=poll_fallback, last_log_id=last_log_id)
39 self.assertIsInstance(self.ws, expect_type)
40 self.assertEqual(200, events.get(True, 5)['status'])
41 human = arvados.api('v1').humans().create(body={}).execute()
43 if last_log_id == None or expected == 0:
44 self.assertEqual(human['uuid'], events.get(True, 5)['object_uuid'])
45 self.assertTrue(events.empty(), "got more events than expected")
48 for i in range(0, 20):
50 event = events.get(True, 5)
51 self.assertTrue(event['object_uuid'] is not None)
52 log_events.append(event['object_uuid'])
56 self.assertTrue(len(log_events)>1)
57 self.assertTrue(human['uuid'] in log_events)
58 self.assertTrue(ancestor['uuid'] in log_events)
60 def test_subscribe_websocket(self):
62 poll_fallback=False, expect_type=arvados.events.EventClient)
64 @mock.patch('arvados.events.EventClient.__init__')
65 def test_subscribe_poll(self, event_client_constr):
66 event_client_constr.side_effect = Exception('All is well')
68 poll_fallback=1, expect_type=arvados.events.PollClient)
70 def test_subscribe_websocket_with_start_time_date_only(self):
71 lastHour = datetime.today() - timedelta(hours = 1)
73 poll_fallback=False, expect_type=arvados.events.EventClient, last_log_id=1,
74 additional_filters=[['created_at', '>=', lastHour.strftime('%Y-%m-%d')]])
76 @mock.patch('arvados.events.EventClient.__init__')
77 def test_poll_with_start_time_date_only(self, event_client_constr):
78 event_client_constr.side_effect = Exception('All is well')
79 lastHour = datetime.today() - timedelta(hours = 1)
81 poll_fallback=1, expect_type=arvados.events.PollClient, last_log_id=1,
82 additional_filters=[['created_at', '>=', lastHour.strftime('%Y-%m-%d')]])
84 def test_subscribe_websocket_with_start_time_last_hour(self):
85 lastHour = datetime.today() - timedelta(hours = 1)
87 poll_fallback=False, expect_type=arvados.events.EventClient, last_log_id=1,
88 additional_filters=[['created_at', '>=', lastHour.strftime('%Y-%m-%d %H:%M:%S')]])
90 @mock.patch('arvados.events.EventClient.__init__')
91 def test_subscribe_poll_with_start_time_last_hour(self, event_client_constr):
92 event_client_constr.side_effect = Exception('All is well')
93 lastHour = datetime.today() - timedelta(hours = 1)
95 poll_fallback=1, expect_type=arvados.events.PollClient, last_log_id=1,
96 additional_filters=[['created_at', '>=', lastHour.strftime('%Y-%m-%d %H:%M:%S')]])
98 def test_subscribe_websocket_with_start_time_next_hour(self):
99 nextHour = datetime.today() + timedelta(hours = 1)
100 with self.assertRaises(Queue.Empty):
101 self._test_subscribe(
102 poll_fallback=False, expect_type=arvados.events.EventClient, last_log_id=1,
103 additional_filters=[['created_at', '>=', nextHour.strftime('%Y-%m-%d %H:%M:%S')]], expected=0)
105 @mock.patch('arvados.events.EventClient.__init__')
106 def test_subscribe_poll_with_start_time_next_hour(self, event_client_constr):
107 event_client_constr.side_effect = Exception('All is well')
108 nextHour = datetime.today() + timedelta(hours = 1)
109 with self.assertRaises(Queue.Empty):
110 self._test_subscribe(
111 poll_fallback=1, expect_type=arvados.events.PollClient, last_log_id=1,
112 additional_filters=[['created_at', '>=', nextHour.strftime('%Y-%m-%d %H:%M:%S')]], expected=0)
114 def test_subscribe_websocket_with_start_time_tomorrow(self):
115 tomorrow = datetime.today() + timedelta(hours = 24)
116 with self.assertRaises(Queue.Empty):
117 self._test_subscribe(
118 poll_fallback=False, expect_type=arvados.events.EventClient, last_log_id=1,
119 additional_filters=[['created_at', '>=', tomorrow.strftime('%Y-%m-%d')]], expected=0)
121 @mock.patch('arvados.events.EventClient.__init__')
122 def test_subscribe_poll_with_start_time_tomorrow(self, event_client_constr):
123 event_client_constr.side_effect = Exception('All is well')
124 tomorrow = datetime.today() + timedelta(hours = 24)
125 with self.assertRaises(Queue.Empty):
126 self._test_subscribe(
127 poll_fallback=1, expect_type=arvados.events.PollClient, last_log_id=1,
128 additional_filters=[['created_at', '>=', tomorrow.strftime('%Y-%m-%d')]], expected=0)