2023-06-16 00:22:54 +00:00
|
|
|
# Routing context veilid tests
|
|
|
|
|
|
|
|
import asyncio
|
2023-06-23 21:01:52 +00:00
|
|
|
import random
|
2023-06-17 18:34:09 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import veilid
|
2023-06-17 19:09:40 +00:00
|
|
|
from veilid.types import OperationId
|
2023-06-17 18:34:09 +00:00
|
|
|
|
2023-06-17 18:37:39 +00:00
|
|
|
from .conftest import server_info
|
2023-06-16 00:22:54 +00:00
|
|
|
|
|
|
|
##################################################################
|
|
|
|
|
2023-06-17 18:34:09 +00:00
|
|
|
|
2023-06-16 00:22:54 +00:00
|
|
|
@pytest.mark.asyncio
|
2023-06-18 22:47:39 +00:00
|
|
|
async def test_routing_contexts(api_connection: veilid.VeilidAPI):
|
2023-06-17 18:34:09 +00:00
|
|
|
rc = await api_connection.new_routing_context()
|
2023-06-18 22:47:39 +00:00
|
|
|
async with rc:
|
2023-06-24 01:12:48 +00:00
|
|
|
rcp = await rc.with_privacy(release=False)
|
2023-06-18 22:47:39 +00:00
|
|
|
async with rcp:
|
2023-06-24 01:12:48 +00:00
|
|
|
rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED, release=False)
|
2023-06-18 22:47:39 +00:00
|
|
|
async with rcps:
|
2023-06-24 01:12:48 +00:00
|
|
|
rcpscp = await rcps.with_custom_privacy(veilid.Stability.RELIABLE, release=False)
|
2023-06-18 22:47:39 +00:00
|
|
|
await rcpscp.release()
|
2023-06-17 18:34:09 +00:00
|
|
|
|
2023-06-16 00:22:54 +00:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_routing_context_app_message_loopback():
|
2023-06-17 19:09:40 +00:00
|
|
|
# Seriously, mypy?
|
|
|
|
app_message_queue: asyncio.Queue = asyncio.Queue()
|
2023-06-16 00:22:54 +00:00
|
|
|
|
|
|
|
async def app_message_queue_update_callback(update: veilid.VeilidUpdate):
|
|
|
|
if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE:
|
|
|
|
await app_message_queue.put(update)
|
|
|
|
|
2023-06-17 18:34:09 +00:00
|
|
|
hostname, port = server_info()
|
|
|
|
api = await veilid.json_api_connect(
|
|
|
|
hostname, port, app_message_queue_update_callback
|
|
|
|
)
|
2023-06-16 00:22:54 +00:00
|
|
|
async with api:
|
2023-06-16 17:14:34 +00:00
|
|
|
# purge routes to ensure we start fresh
|
|
|
|
await api.debug("purge routes")
|
2023-06-17 18:34:09 +00:00
|
|
|
|
2023-06-16 00:22:54 +00:00
|
|
|
# make a routing context that uses a safety route
|
|
|
|
rc = await (await api.new_routing_context()).with_privacy()
|
2023-06-18 22:47:39 +00:00
|
|
|
async with rc:
|
2023-06-24 01:12:48 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
# make a new local private route
|
|
|
|
prl, blob = await api.new_private_route()
|
2023-06-16 00:22:54 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
# import it as a remote route as well so we can send to it
|
|
|
|
prr = await api.import_remote_private_route(blob)
|
2023-06-16 00:22:54 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
# send an app message to our own private route
|
|
|
|
message = b"abcd1234"
|
|
|
|
await rc.app_message(prr, message)
|
2023-06-17 18:34:09 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
# we should get the same message back
|
|
|
|
update: veilid.VeilidUpdate = await asyncio.wait_for(
|
|
|
|
app_message_queue.get(), timeout=10
|
|
|
|
)
|
2023-06-16 00:22:54 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
assert isinstance(update.detail, veilid.VeilidAppMessage)
|
|
|
|
assert update.detail.message == message
|
2023-06-16 00:22:54 +00:00
|
|
|
|
2023-06-16 15:57:55 +00:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_routing_context_app_call_loopback():
|
2023-06-17 19:09:40 +00:00
|
|
|
app_call_queue: asyncio.Queue = asyncio.Queue()
|
2023-06-16 15:57:55 +00:00
|
|
|
|
|
|
|
async def app_call_queue_update_callback(update: veilid.VeilidUpdate):
|
|
|
|
if update.kind == veilid.VeilidUpdateKind.APP_CALL:
|
|
|
|
await app_call_queue.put(update)
|
|
|
|
|
2023-06-17 18:34:09 +00:00
|
|
|
hostname, port = server_info()
|
|
|
|
api = await veilid.json_api_connect(hostname, port, app_call_queue_update_callback)
|
2023-06-16 15:57:55 +00:00
|
|
|
async with api:
|
2023-06-16 17:14:34 +00:00
|
|
|
# purge routes to ensure we start fresh
|
|
|
|
await api.debug("purge routes")
|
|
|
|
|
2023-06-16 15:57:55 +00:00
|
|
|
# make a routing context that uses a safety route
|
|
|
|
rc = await (await api.new_routing_context()).with_privacy()
|
2023-06-18 22:47:39 +00:00
|
|
|
async with rc:
|
2023-06-24 01:12:48 +00:00
|
|
|
|
2023-06-18 22:47:39 +00:00
|
|
|
# make a new local private route
|
|
|
|
prl, blob = await api.new_private_route()
|
|
|
|
|
|
|
|
# import it as a remote route as well so we can send to it
|
|
|
|
prr = await api.import_remote_private_route(blob)
|
|
|
|
|
|
|
|
# send an app message to our own private route
|
|
|
|
request = b"abcd1234"
|
|
|
|
app_call_task = asyncio.create_task(
|
|
|
|
rc.app_call(prr, request), name="app call task"
|
|
|
|
)
|
|
|
|
|
|
|
|
# we should get the same request back
|
|
|
|
update: veilid.VeilidUpdate = await asyncio.wait_for(
|
|
|
|
app_call_queue.get(), timeout=10
|
|
|
|
)
|
|
|
|
appcall = update.detail
|
|
|
|
|
|
|
|
assert isinstance(appcall, veilid.VeilidAppCall)
|
|
|
|
assert appcall.message == request
|
|
|
|
|
|
|
|
# now we reply to the request
|
|
|
|
reply = b"qwer5678"
|
|
|
|
await api.app_call_reply(appcall.call_id, reply)
|
|
|
|
|
|
|
|
# now we should get the reply from the call
|
|
|
|
result = await app_call_task
|
|
|
|
assert result == reply
|
2023-06-23 21:01:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_routing_context_app_message_loopback_big_packets():
|
|
|
|
|
|
|
|
app_message_queue: asyncio.Queue = asyncio.Queue()
|
|
|
|
|
2023-06-25 05:23:24 +00:00
|
|
|
global got_message
|
|
|
|
got_message = 0
|
2023-06-23 21:01:52 +00:00
|
|
|
async def app_message_queue_update_callback(update: veilid.VeilidUpdate):
|
|
|
|
if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE:
|
2023-06-25 05:23:24 +00:00
|
|
|
global got_message
|
|
|
|
got_message += 1
|
|
|
|
print("got {}".format(got_message))
|
2023-06-23 21:01:52 +00:00
|
|
|
await app_message_queue.put(update)
|
|
|
|
|
2023-06-24 15:16:34 +00:00
|
|
|
sent_messages: set[bytes] = set()
|
|
|
|
|
2023-06-23 21:01:52 +00:00
|
|
|
hostname, port = server_info()
|
|
|
|
api = await veilid.json_api_connect(
|
|
|
|
hostname, port, app_message_queue_update_callback
|
|
|
|
)
|
|
|
|
async with api:
|
|
|
|
# purge routes to ensure we start fresh
|
|
|
|
await api.debug("purge routes")
|
|
|
|
|
|
|
|
# make a routing context that uses a safety route
|
2023-06-24 15:16:34 +00:00
|
|
|
rc = await (await (await api.new_routing_context()).with_privacy()).with_sequencing(veilid.Sequencing.ENSURE_ORDERED)
|
2023-06-23 21:01:52 +00:00
|
|
|
async with rc:
|
2023-06-24 01:12:48 +00:00
|
|
|
|
2023-06-23 21:01:52 +00:00
|
|
|
# make a new local private route
|
|
|
|
prl, blob = await api.new_private_route()
|
|
|
|
|
|
|
|
# import it as a remote route as well so we can send to it
|
|
|
|
prr = await api.import_remote_private_route(blob)
|
|
|
|
|
2023-06-24 15:16:34 +00:00
|
|
|
# do this test 100 times
|
2023-06-25 05:23:24 +00:00
|
|
|
for _ in range(100):
|
2023-06-23 21:01:52 +00:00
|
|
|
|
|
|
|
# send a random sized random app message to our own private route
|
2023-06-24 01:12:48 +00:00
|
|
|
message = random.randbytes(random.randint(0, 32768))
|
2023-06-23 21:01:52 +00:00
|
|
|
await rc.app_message(prr, message)
|
|
|
|
|
2023-06-24 15:16:34 +00:00
|
|
|
sent_messages.add(message)
|
|
|
|
|
|
|
|
# we should get the same messages back
|
2023-06-25 05:23:24 +00:00
|
|
|
print(len(sent_messages))
|
|
|
|
for n in range(len(sent_messages)):
|
|
|
|
print(n)
|
2023-06-23 21:01:52 +00:00
|
|
|
update: veilid.VeilidUpdate = await asyncio.wait_for(
|
|
|
|
app_message_queue.get(), timeout=10
|
|
|
|
)
|
|
|
|
assert isinstance(update.detail, veilid.VeilidAppMessage)
|
2023-06-24 15:16:34 +00:00
|
|
|
|
|
|
|
assert update.detail.message in sent_messages
|