Merge branch 'master' into 6473-fetch-events-starting-at
[arvados.git] / sdk / python / tests / test_websockets.py
1 import Queue
2 import run_test_server
3 import unittest
4 import arvados
5 import arvados.events
6 import mock
7 import threading
8 from datetime import datetime, timedelta
9
10 class WebsocketTest(run_test_server.TestCaseWithServers):
11     MAIN_SERVER = {}
12
13     def setUp(self):
14         self.ws = None
15
16     def tearDown(self):
17         if self.ws:
18             self.ws.close()
19         super(WebsocketTest, self).tearDown()
20
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")
35
36     def test_subscribe_websocket(self):
37         self._test_subscribe(
38             poll_fallback=False, expect_type=arvados.events.EventClient)
39
40     def test_subscribe_websocket_with_start_time_today(self):
41         now = datetime.today()
42         self._test_subscribe(
43             poll_fallback=False, expect_type=arvados.events.EventClient,
44                 additional_filters=[['created_at', '>', now.strftime('%Y-%m-%d')]])
45
46     def test_subscribe_websocket_with_start_time_last_hour(self):
47         lastHour = datetime.today() - timedelta(hours = 1)
48         self._test_subscribe(
49             poll_fallback=False, expect_type=arvados.events.EventClient,
50                 additional_filters=[['created_at', '>', lastHour.strftime('%Y-%m-%d %H:%M:%S')]])
51
52     def test_subscribe_websocket_with_start_time_next_hour(self):
53         nextHour = datetime.today() + timedelta(hours = 1)
54         with self.assertRaises(Queue.Empty):
55             self._test_subscribe(
56                 poll_fallback=False, expect_type=arvados.events.EventClient,
57                     additional_filters=[['created_at', '>', nextHour.strftime('%Y-%m-%d %H:%M:%S')]])
58
59     def test_subscribe_websocket_with_start_time_tomorrow(self):
60         tomorrow = datetime.today() + timedelta(hours = 24)
61         with self.assertRaises(Queue.Empty):
62             self._test_subscribe(
63                 poll_fallback=False, expect_type=arvados.events.EventClient,
64                     additional_filters=[['created_at', '>', tomorrow.strftime('%Y-%m-%d')]])
65
66     @mock.patch('arvados.events.EventClient.__init__')
67     def test_subscribe_poll(self, event_client_constr):
68         event_client_constr.side_effect = Exception('All is well')
69         self._test_subscribe(
70             poll_fallback=1, expect_type=arvados.events.PollClient)