Merge branch 'master' into 4062-infinite-scroll-repeat-issue
[arvados.git] / sdk / python / tests / test_websockets.py
1 import run_test_server
2 import unittest
3 import arvados
4 import arvados.events
5 import threading
6
7 class EventTestBase(object):
8     def on_event(self, ev):
9         if self.state == 1:
10             self.assertEqual(200, ev['status'])
11             self.state = 2
12             self.subscribed.set()
13         elif self.state == 2:
14             self.assertEqual(self.h[u'uuid'], ev[u'object_uuid'])
15             self.state = 3
16             self.done.set()
17         elif self.state == 3:
18             self.fail()
19
20     def runTest(self):
21         self.ws = None
22         self.state = 1
23         self.subscribed = threading.Event()
24         self.done = threading.Event()
25
26         run_test_server.authorize_with("admin")
27         api = arvados.api('v1', cache=False)
28         self.ws = arvados.events.subscribe(arvados.api('v1', cache=False), [['object_uuid', 'is_a', 'arvados#human']], self.on_event, poll_fallback=2)
29         self.assertIsInstance(self.ws, self.WS_TYPE)
30         self.subscribed.wait(10)
31         self.h = api.humans().create(body={}).execute()
32         self.done.wait(10)
33         self.assertEqual(3, self.state)
34
35 class WebsocketTest(run_test_server.TestCaseWithServers, EventTestBase):
36     MAIN_SERVER = {'websockets': True}
37     WS_TYPE = arvados.events.EventClient
38
39     def tearDown(self):
40         if self.ws:
41             self.ws.close()
42         super(WebsocketTest, self).tearDown()
43
44
45 class PollClientTest(run_test_server.TestCaseWithServers, EventTestBase):
46     MAIN_SERVER = {}
47     WS_TYPE = arvados.events.PollClient
48
49     def tearDown(self):
50         if self.ws:
51             self.ws.close()
52         super(PollClientTest, self).tearDown()