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(self, poll_fallback, expect_type, start_time=None, expected=1): run_test_server.authorize_with('active') events = Queue.Queue(100) # Create ancestor before subscribing. # When listening with start_time in the past, this should also be retrieved. # However, when start_time is omitted in subscribe, this should not be fetched. ancestor = arvados.api('v1').humans().create(body={}).execute() filters = [['object_uuid', 'is_a', 'arvados#human']] if start_time: filters.append(['created_at', '>=', start_time]) self.ws = arvados.events.subscribe( arvados.api('v1'), filters, events.put_nowait, poll_fallback=poll_fallback, last_log_id=(1 if start_time else None)) self.assertIsInstance(self.ws, expect_type) self.assertEqual(200, events.get(True, 5)['status']) human = arvados.api('v1').humans().create(body={}).execute() log_object_uuids = [] for i in range(0, expected): log_object_uuids.append(events.get(True, 5)['object_uuid']) if expected > 0: self.assertIn(human['uuid'], log_object_uuids) if expected > 1: self.assertIn(ancestor['uuid'], log_object_uuids) with self.assertRaises(Queue.Empty): # assertEqual just serves to show us what unexpected thing # comes out of the queue when the assertRaises fails; when # the test passes, this assertEqual doesn't get called. self.assertEqual(events.get(True, 2), None)
def _test_websocket_reconnect(self, close_unexpected): run_test_server.authorize_with('active') events = Queue.Queue(100) logstream = StringIO.StringIO() rootLogger = logging.getLogger() streamHandler = logging.StreamHandler(logstream) rootLogger.addHandler(streamHandler) filters = [['object_uuid', 'is_a', 'arvados#human']] filters.append(['created_at', '>=', self.localiso(self.TIME_PAST)]) self.ws = arvados.events.subscribe( arvados.api('v1'), filters, events.put_nowait, poll_fallback=False, last_log_id=None) self.assertIsInstance(self.ws, arvados.events.EventClient) self.assertEqual(200, events.get(True, 5)['status']) # create obj human = arvados.api('v1').humans().create(body={}).execute() # expect an event self.assertIn(human['uuid'], events.get(True, 5)['object_uuid']) with self.assertRaises(Queue.Empty): self.assertEqual(events.get(True, 2), None) # close (im)properly if close_unexpected: self.ws.close_connection() else: self.ws.close() # create one more obj human2 = arvados.api('v1').humans().create(body={}).execute() # (un)expect the object creation event if close_unexpected: log_object_uuids = [] for i in range(0, 2): event = events.get(True, 5) if event.get('object_uuid') != None: log_object_uuids.append(event['object_uuid']) with self.assertRaises(Queue.Empty): self.assertEqual(events.get(True, 2), None) self.assertNotIn(human['uuid'], log_object_uuids) self.assertIn(human2['uuid'], log_object_uuids) else: with self.assertRaises(Queue.Empty): self.assertEqual(events.get(True, 2), None) # verify log message to ensure that an (un)expected close log_messages = logstream.getvalue() closeLogFound = log_messages.find("Unexpected close. Reconnecting.") retryLogFound = log_messages.find("Error during websocket reconnect. Will retry") if close_unexpected: self.assertNotEqual(closeLogFound, -1) else: self.assertEqual(closeLogFound, -1) rootLogger.removeHandler(streamHandler)