3609: Add additional api_ fields to api client object so that subscribe() can
[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 import threading
7
8 class WebsocketTest(run_test_server.TestCaseWithServers):
9     MAIN_SERVER = {'websockets': True}
10
11     def on_event(self, ev):
12         if self.state == 1:
13             self.assertEqual(200, ev['status'])
14             self.state = 2
15         elif self.state == 2:
16             self.assertEqual(self.h[u'uuid'], ev[u'object_uuid'])
17             self.state = 3
18             self.done.set()
19         elif self.state == 3:
20             self.fail()
21
22     def runTest(self):
23         self.state = 1
24         self.done = threading.Event()
25
26         run_test_server.authorize_with("admin")
27         api = arvados.api('v1', cache=False)
28         ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], self.on_event)
29         time.sleep(1)
30         self.h = api.humans().create(body={}).execute()
31         self.done.wait(10)
32         self.assertEqual(3, self.state)
33         ws.close()
34
35 class PollClientTest(run_test_server.TestCaseWithServers):
36     MAIN_SERVER = {}
37
38     def on_event(self, ev):
39         if self.state == 1:
40             self.assertEqual(200, ev['status'])
41             self.state = 2
42         elif self.state == 2:
43             self.assertEqual(self.h[u'uuid'], ev[u'object_uuid'])
44             self.state = 3
45             self.done.set()
46         elif self.state == 3:
47             self.fail()
48
49     def runTest(self):
50         self.state = 1
51         self.done = threading.Event()
52
53         run_test_server.authorize_with("admin")
54         api = arvados.api('v1', cache=False)
55         ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], self.on_event, poll_fallback=2)
56         time.sleep(1)
57         self.h = api.humans().create(body={}).execute()
58         self.done.wait(10)
59         self.assertEqual(3, self.state)
60         ws.close()