8 from datetime import datetime, timedelta
10 class WebsocketTest(run_test_server.TestCaseWithServers):
19 super(WebsocketTest, self).tearDown()
21 def _test_subscribe(self, poll_fallback, expect_type, additional_filters=None):
22 run_test_server.authorize_with('active')
23 events = Queue.Queue(3)
24 filters = [['object_uuid', 'is_a', 'arvados#human']]
25 if additional_filters:
26 filters = filters + additional_filters
27 self.ws = arvados.events.subscribe(
28 arvados.api('v1'), filters,
29 events.put, poll_fallback=poll_fallback)
30 self.assertIsInstance(self.ws, expect_type)
31 self.assertEqual(200, events.get(True, 10)['status'])
32 human = arvados.api('v1').humans().create(body={}).execute()
33 self.assertEqual(human['uuid'], events.get(True, 10)['object_uuid'])
34 self.assertTrue(events.empty(), "got more events than expected")
36 def test_subscribe_websocket(self):
38 poll_fallback=False, expect_type=arvados.events.EventClient)
40 def test_subscribe_websocket_with_start_time_today(self):
41 lastHour = datetime.today() - timedelta(hours = 1)
43 poll_fallback=False, expect_type=arvados.events.EventClient,
44 additional_filters=[['created_at', '>', lastHour.strftime('%Y-%m-%d')]])
46 def test_subscribe_websocket_with_start_time_last_hour(self):
47 lastHour = datetime.today() - timedelta(hours = 1)
49 poll_fallback=False, expect_type=arvados.events.EventClient,
50 additional_filters=[['created_at', '>', lastHour.strftime('%Y-%m-%d %H:%M:%S')]])
52 def test_subscribe_websocket_with_start_time_next_hour(self):
53 nextHour = datetime.today() + timedelta(hours = 1)
54 with self.assertRaises(Queue.Empty):
56 poll_fallback=False, expect_type=arvados.events.EventClient,
57 additional_filters=[['created_at', '>', nextHour.strftime('%Y-%m-%d %H:%M:%S')]])
59 @mock.patch('arvados.events.EventClient.__init__')
60 def test_subscribe_poll(self, event_client_constr):
61 event_client_constr.side_effect = Exception('All is well')
63 poll_fallback=1, expect_type=arvados.events.PollClient)