def test_progress_multiple_requests(): start_test_server = websockets.serve(server_handle_two_requests, "localhost") test_server = asyncio.get_event_loop().run_until_complete(start_test_server) client = rockets.AsyncClient( "localhost:" + str(test_server.sockets[0].getsockname()[1]) ) request_a = rockets.Request("test_progress") request_b = rockets.Request("test_progress") request_task = client.async_batch([request_a, request_b]) class ProgressTracker: def __init__(self): self._num_calls = 0 def on_progress(self, progress): self._num_calls += 1 if self._num_calls == 1: assert_equal(progress.amount, 0.25) assert_equal(progress.operation, "Batch request") assert_equal(str(progress), "('Batch request', 0.25)") elif self._num_calls == 2: assert_equal(progress.amount, 0.5) assert_equal(progress.operation, "Batch request") assert_equal(str(progress), "('Batch request', 0.5)") self.called = True tracker = ProgressTracker() request_task.add_progress_callback(tracker.on_progress) responses = asyncio.get_event_loop().run_until_complete(request_task) assert_equal(len(responses), 2) results = list(map(lambda x: x.result, responses)) assert_equal(results, ["DONE", "DONE"]) assert_true(tracker.called)
async def run_notebook_cell(): self.server_ready.wait() client = rockets.AsyncClient('ws://'+self.server_url) request_1 = rockets.Request('double', [2]) request_2 = rockets.Request('double', [4]) task = client.async_batch([request_1, request_2]) task.add_done_callback(_on_done) await task
async def __init__(self, url, loop=None): """ Create a new client instance by connecting to the given URL. :param str url: a string 'hostname:port' to connect to a running Brayns instance :param asyncio.AbstractEventLoop loop: Event loop where this client should run in """ super().__init__(url) self.rockets_client = rockets.AsyncClient(url, subprotocols=['rockets'], loop=loop) registry, requests = build_schema_requests_from_registry(self.http_url) schemas = await self.rockets_client.batch(requests) super()._build_api(registry, requests, schemas)
def test_progress(): client = rockets.AsyncClient(server_url) request_task = client.async_request('test_progress') class ProgressTracker: def on_progress(self, progress): assert_equal(progress.amount, 0.5) assert_equal(progress.operation, 'almost done') assert_equal(str(progress), "('almost done', 0.5)") self.called = True tracker = ProgressTracker() request_task.add_progress_callback(tracker.on_progress) assert_equal(asyncio.get_event_loop().run_until_complete(request_task), 'DONE') assert_true(tracker.called)
def test_subscribe_async_client(): client = rockets.AsyncClient(server_url) received = asyncio.get_event_loop().create_future() async def _do_it(): await client.connect() def _on_message(message): received.set_result(message) client.ws_observable.subscribe(_on_message) await client.send("Rockets") await received asyncio.get_event_loop().run_until_complete(_do_it()) assert_equal(received.result(), "Hello Rockets!")
def test_progress_single_request(): client = rockets.AsyncClient(server_url) request = rockets.Request("test_progress") request_task = client.async_batch([request]) class ProgressTracker: def on_progress(self, progress): assert_equal(progress.amount, 0.5) assert_equal(progress.operation, "Batch request") assert_equal(str(progress), "('Batch request', 0.5)") self.called = True tracker = ProgressTracker() request_task.add_progress_callback(tracker.on_progress) responses = asyncio.get_event_loop().run_until_complete(request_task) assert_equal(len(responses), 1) assert_equal(responses[0].result, "DONE") assert_true(tracker.called)
def test_cancel(): client = rockets.AsyncClient(server_url) request_task = client.async_request('test_cancel') def _on_done(value): assert_equal(value.result(), None) asyncio.get_event_loop().stop() async def _do_cancel(): asyncio.sleep(10) request_task.cancel() request_task.add_done_callback(_on_done) asyncio.ensure_future(request_task) asyncio.ensure_future(_do_cancel()) asyncio.get_event_loop().run_forever() assert_true(request_task.done())
def test_cancel(): client = rockets.AsyncClient(server_url) request_1 = rockets.Request("test_cancel") request_2 = rockets.Request("test_cancel") request_task = client.async_batch([request_1, request_2]) def _on_done(value): assert_equal(value.result(), None) asyncio.get_event_loop().stop() async def _do_cancel(): await got_cancel request_task.cancel() request_task.add_done_callback(_on_done) asyncio.ensure_future(request_task) asyncio.ensure_future(_do_cancel()) asyncio.get_event_loop().run_forever() assert_true(request_task.done())
async def run_notebook_cell(): self.server_ready.wait() client = rockets.AsyncClient('ws://'+self.server_url) task = client.async_request('ping') task.add_done_callback(_on_done) await task