3609: Add test case for PollClient. arvados.event.subscribe() starts a new
[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 time
6
7 class WebsocketTest(run_test_server.TestCaseWithServers):
8     MAIN_SERVER = {'websockets': True}
9
10     def on_event(self, ev):
11         if self.state == 1:
12             self.assertEqual(200, ev['status'])
13             self.state = 2
14         elif self.state == 2:
15             self.assertEqual(self.h[u'uuid'], ev[u'object_uuid'])
16             self.state = 3
17         elif self.state == 3:
18             self.fail()
19
20     def runTest(self):
21         self.state = 1
22
23         run_test_server.authorize_with("admin")
24         api = arvados.api('v1', cache=False)
25         ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], lambda ev: self.on_event(ev))
26         time.sleep(1)
27         self.h = api.humans().create(body={}).execute()
28         time.sleep(2)
29         self.assertEqual(3, self.state)
30         ws.close()
31
32 class PollClientTest(run_test_server.TestCaseWithServers):
33     MAIN_SERVER = {}
34
35     def on_event(self, ev):
36         if self.state == 1:
37             self.assertEqual(200, ev['status'])
38             self.state = 2
39         elif self.state == 2:
40             self.assertEqual(self.h[u'uuid'], ev[u'object_uuid'])
41             self.state = 3
42         elif self.state == 3:
43             self.fail()
44
45     def runTest(self):
46         self.state = 1
47
48         run_test_server.authorize_with("admin")
49         api = arvados.api('v1', cache=False)
50         ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], lambda ev: self.on_event(ev), poll_fallback=2)
51         time.sleep(1)
52         self.h = api.humans().create(body={}).execute()
53         time.sleep(5)
54         self.assertEqual(3, self.state)
55         ws.close()