Merge branch '3609-arv-ws' into 3609-arv-run
[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.state = 1
22         self.subscribed = threading.Event()
23         self.done = threading.Event()
24
25         run_test_server.authorize_with("admin")
26         api = arvados.api('v1', cache=False)
27         self.ws = arvados.events.subscribe(arvados.api('v1', cache=False), [['object_uuid', 'is_a', 'arvados#human']], self.on_event, poll_fallback=2)
28         if not isinstance(self.ws, self.WS_TYPE):
29             self.fail()
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         self.ws.close()
41         super(run_test_server.TestCaseWithServers, self).tearDown()
42
43
44 class PollClientTest(run_test_server.TestCaseWithServers, EventTestBase):
45     MAIN_SERVER = {}
46     WS_TYPE = arvados.events.PollClient
47
48     def tearDown(self):
49         self.ws.close()
50         super(run_test_server.TestCaseWithServers, self).tearDown()