5037: Remove connection pool. Now, api(cache=False) refers only to the discovery...
[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         self.ws = arvados.events.subscribe(arvados.api('v1'), [['object_uuid', 'is_a', 'arvados#human']], self.on_event, poll_fallback=2)
28         self.assertIsInstance(self.ws, self.WS_TYPE)
29         self.subscribed.wait(10)
30         self.h = arvados.api('v1').humans().create(body={}).execute()
31         self.done.wait(10)
32         self.assertEqual(3, self.state)
33
34 class WebsocketTest(run_test_server.TestCaseWithServers, EventTestBase):
35     MAIN_SERVER = {'websockets': True}
36     WS_TYPE = arvados.events.EventClient
37
38     def tearDown(self):
39         if self.ws:
40             self.ws.close()
41         super(WebsocketTest, 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         if self.ws:
50             self.ws.close()
51         super(PollClientTest, self).tearDown()