async def test_route_any_args(server): address = Address(url=server.url) foo = server.route('/foo', ANY_ARGS) task = create_task(subscribe(foo.sub(), 1)) address.send(foo, 'foo') results = list(await task) assert results == [['foo']]
async def test_multiple_servers(any_server_class, unused_tcp_port_factory): """ Test that multiple servers can run okay in the same process. """ servers = [] addresses = [] foo = Route('/foo', int) for i in range(3): server = any_server_class(url='osc.tcp://:%s' % unused_tcp_port_factory()) server.route(foo) server.start() servers.append(server) address = Address(url=server.url) addresses.append(address) task = create_task(subscribe(foo.sub(), 3)) for i, address in enumerate(addresses): address.send(foo, i) try: results = await task assert set([r[0] for r in results]) == {0, 1, 2} finally: for server in servers: server.stop()
async def test_unroute(server): address = Address(url=server.url) foo = server.route('/foo', 's') task = create_task(subscribe(foo.sub(), 1)) server.unroute(foo) address.send(foo, 'bar') with pytest.raises(asyncio.CancelledError): await task
async def test_reuse_subs(any_server): address = Address(url=any_server.url) foo = any_server.route('/foo', 's') tasks = asyncio.gather( *[create_task(subscribe(foo.sub(), 1)) for i in range(3)]) address.send(foo, 'bar') with pytest.raises(asyncio.CancelledError): await tasks
async def test_bundle(server): address = Address(url=server.url) foo = server.route('/foo', 's') bar = server.route('/bar', 's') baz = server.route('/baz', 's') subs = foo.sub() | bar.sub() | baz.sub() task = create_task(subscribe(subs, 3)) address.bundle([ Message(foo, 'foo'), Message(bar, 'bar'), Message(baz, 'baz'), ]) results = await task assert_results(results, {foo: [['foo']], bar: [['bar']], baz: [['baz']]})
async def test_multiple_addresses(any_server): """ Test that multiple clients can send data to a single server """ foo = any_server.route('/foo', int) addresses = [Address(port=any_server.port) for i in range(3)] task = create_task(subscribe(foo.sub(), 3)) for i, address in enumerate(addresses): address.send(foo, i) # await asyncio.sleep(0.1) results = await task assert results == [[0], [1], [2]]
async def main(): server = Server(port=12001) server.start() # Create endpoints # /foo accepts an int, a float, and a MIDI packet foo = server.route('/foo', [int, float, Midi]) ex = server.route('/exit') address = Address(port=12001) for i in range(5): address.send(foo, i, float(i), Midi(i, i, i, i)) # Notify subscriptions to exit in 1 sec address.delay(1, ex) # Subscribe to messages for any of the routes subs = foo.sub() | ex.sub() async for route, data in subs: print(f'echo_server: {str(route.path)} received {data}') if route == ex: await subs.unsub() server.stop()
async def test_sub_join(server): address = Address(url=server.url) foo = server.route('/foo', 's') bar = server.route('/bar', 's') baz = server.route('/baz', 's') foo_sub = foo.sub() bar_sub = bar.sub() baz_sub = baz.sub() sub = foo_sub | bar_sub assert foo in sub assert foo_sub in sub assert baz not in sub sub |= baz_sub sub |= sub # no-op assert len(sub) == 3 assert sub == foo_sub | bar_sub | baz_sub assert sub == foo.sub() | bar.sub() | baz.sub() assert sub != foo.sub() | bar.sub() assert sub not in foo.sub() | bar.sub() assert sub in foo.sub() | bar.sub() | baz.sub() assert (foo.sub() | bar.sub()) in sub task = create_task(subscribe(sub, 6)) for route, data in { foo: ['foo1', 'foo2'], bar: ['bar1', 'bar2'], baz: ['baz1', 'baz2'] }.items(): for d in data: address.send(route, d) results = await task assert_results( results, { foo: [['foo1'], ['foo2']], bar: [['bar1'], ['bar2']], baz: [['baz1'], ['baz2']], })
async def test_route_any_path(server): address = Address(url=server.url) any_path = server.route(ANY_PATH, 's') task = create_task(subscribe(any_path.sub(), 1)) with pytest.raises(ValueError): address.send(any_path, ['foo']) address.send(Route('/foo', 's'), ['foo']) results = list(await task) assert results == [['foo']]
async def test_route_pattern(server): address = Address(url=server.url) foo = server.route('/aaa/foo', 's') bar = server.route('/bbb/foo', 's') sub = foo.sub() | bar.sub() task = create_task(subscribe(sub, 4)) address.send(Route('//foo', 's'), ['xpath']) address.send(Route('/{aaa,bbb}/foo', 's'), ['array']) results = await task assert_results(results, { foo: [['xpath'], ['array']], bar: [['xpath'], ['array']] })
async def test_bundle_delayed(server): address = Address(url=server.url) foo = server.route('/foo', 's') task = create_task(subscribe(foo.sub(), 1)) address.bundle([ Message(foo, 'now'), Bundle([Message(foo, 'later')], timetag=now() + datetime.timedelta(seconds=0.5)) ]) address.delay(1, foo, 'later than later') results = list(await task) assert results == [['now']] assert server.events_pending assert 0 < server.next_event_delay < 1 task = create_task(subscribe(foo.sub(), 1)) results = list(await task) assert results == [['later']] assert server.events_pending assert 0 < server.next_event_delay < 1 task = create_task(subscribe(foo.sub(), 1)) results = list(await task) assert results == [['later than later']] assert not server.events_pending
async def test_multiple_addresses(any_server): """ Test that multiple clients can send data to a single server """ foo = any_server.route('/foo', int) addresses = [Address(port=any_server.port) for i in range(3)] task = create_task(subscribe(foo.sub(), 3)) for i, address in enumerate(addresses): address.send(foo, i) # await asyncio.sleep(0.1) results = await task assert results == [[0], [1], [2]] @pytest.fixture(params=[ lambda port, ip, iface: (Address(url='osc.tcp://%s:%s' % (ip, port())), ip, iface), lambda port, ip, iface: (Address(url='osc.udp://%s:%s' % (ip, port())), ip, iface), lambda port, ip, iface: (Address(url='osc.unix:///aiolo-test-%s.osc' % port()), ip, iface), lambda port, ip, iface: (Address(proto=PROTO_TCP, host=ip, port=port()), ip, iface), lambda port, ip, iface: (Address(proto=PROTO_UDP, host=ip, port=port()), ip, iface), lambda port, ip, iface: (Address(proto=PROTO_UNIX, host=ip, port='/tmp/aiolo-test-%s.osc' % port() ), ip, iface), ]) def address_init_factory(request, ip_interface, unused_tcp_port_factory): ip, iface = ip_interface return functools.partial(request.param, unused_tcp_port_factory, ip, iface)