X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/245c6abec4bf824ad8c8683e59ab6ee08c836cf7..5b1602a521d3f6689f5988cdc2b25b74ff9fc1e1:/sdk/python/tests/test_websockets.py diff --git a/sdk/python/tests/test_websockets.py b/sdk/python/tests/test_websockets.py index fe065e1f2b..d879ebe1f8 100644 --- a/sdk/python/tests/test_websockets.py +++ b/sdk/python/tests/test_websockets.py @@ -1,60 +1,40 @@ +import Queue import run_test_server import unittest import arvados import arvados.events -import time +import mock import threading class WebsocketTest(run_test_server.TestCaseWithServers): - MAIN_SERVER = {'websockets': True} - - def on_event(self, ev): - if self.state == 1: - self.assertEqual(200, ev['status']) - self.state = 2 - elif self.state == 2: - self.assertEqual(self.h[u'uuid'], ev[u'object_uuid']) - self.state = 3 - self.done.set() - elif self.state == 3: - self.fail() - - def runTest(self): - self.state = 1 - self.done = threading.Event() - - run_test_server.authorize_with("admin") - api = arvados.api('v1', cache=False) - ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], self.on_event) - time.sleep(1) - self.h = api.humans().create(body={}).execute() - self.done.wait(10) - self.assertEqual(3, self.state) - ws.close() - -class PollClientTest(run_test_server.TestCaseWithServers): MAIN_SERVER = {} - def on_event(self, ev): - if self.state == 1: - self.assertEqual(200, ev['status']) - self.state = 2 - elif self.state == 2: - self.assertEqual(self.h[u'uuid'], ev[u'object_uuid']) - self.state = 3 - self.done.set() - elif self.state == 3: - self.fail() - - def runTest(self): - self.state = 1 - self.done = threading.Event() - - run_test_server.authorize_with("admin") - api = arvados.api('v1', cache=False) - ws = arvados.events.subscribe(api, [['object_uuid', 'is_a', 'arvados#human']], self.on_event, poll_fallback=2) - time.sleep(1) - self.h = api.humans().create(body={}).execute() - self.done.wait(10) - self.assertEqual(3, self.state) - ws.close() + def setUp(self): + self.ws = None + + def tearDown(self): + if self.ws: + self.ws.close() + super(WebsocketTest, self).tearDown() + + def _test_subscribe(self, poll_fallback, expect_type): + run_test_server.authorize_with('active') + events = Queue.Queue(3) + self.ws = arvados.events.subscribe( + arvados.api('v1'), [['object_uuid', 'is_a', 'arvados#human']], + events.put, poll_fallback=poll_fallback) + self.assertIsInstance(self.ws, expect_type) + self.assertEqual(200, events.get(True, 10)['status']) + human = arvados.api('v1').humans().create(body={}).execute() + self.assertEqual(human['uuid'], events.get(True, 10)['object_uuid']) + self.assertTrue(events.empty(), "got more events than expected") + + def test_subscribe_websocket(self): + self._test_subscribe( + poll_fallback=False, expect_type=arvados.events.EventClient) + + @mock.patch('arvados.events.EventClient.__init__') + def test_subscribe_poll(self, event_client_constr): + event_client_constr.side_effect = Exception('All is well') + self._test_subscribe( + poll_fallback=1, expect_type=arvados.events.PollClient)