2023-06-15 20:22:54 -04:00
|
|
|
# Routing context veilid tests
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
2023-06-17 14:34:09 -04:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import veilid
|
|
|
|
|
|
|
|
from . import api_connection, server_info
|
2023-06-15 20:22:54 -04:00
|
|
|
|
|
|
|
##################################################################
|
|
|
|
|
2023-06-17 14:34:09 -04:00
|
|
|
|
2023-06-15 20:22:54 -04:00
|
|
|
@pytest.mark.asyncio
|
2023-06-17 14:34:09 -04:00
|
|
|
async def test_routing_contexts(api_connection):
|
|
|
|
rc = await api_connection.new_routing_context()
|
|
|
|
rcp = await rc.with_privacy()
|
|
|
|
rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED)
|
|
|
|
await rcps.with_custom_privacy(veilid.Stability.RELIABLE)
|
|
|
|
|
2023-06-15 20:22:54 -04:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_routing_context_app_message_loopback():
|
|
|
|
app_message_queue = asyncio.Queue()
|
|
|
|
|
|
|
|
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 14:34:09 -04:00
|
|
|
hostname, port = server_info()
|
|
|
|
api = await veilid.json_api_connect(
|
|
|
|
hostname, port, app_message_queue_update_callback
|
|
|
|
)
|
2023-06-15 20:22:54 -04:00
|
|
|
async with api:
|
2023-06-16 13:14:34 -04:00
|
|
|
# purge routes to ensure we start fresh
|
|
|
|
await api.debug("purge routes")
|
2023-06-17 14:34:09 -04:00
|
|
|
|
2023-06-15 20:22:54 -04:00
|
|
|
# make a routing context that uses a safety route
|
|
|
|
rc = await (await api.new_routing_context()).with_privacy()
|
|
|
|
|
2023-06-16 11:57:55 -04:00
|
|
|
# make a new local private route
|
|
|
|
prl, blob = await api.new_private_route()
|
2023-06-15 20:22:54 -04:00
|
|
|
|
2023-06-16 11:57:55 -04: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-17 14:34:09 -04:00
|
|
|
|
2023-06-16 11:57:55 -04:00
|
|
|
# send an app message to our own private route
|
2023-06-15 20:22:54 -04:00
|
|
|
message = b"abcd1234"
|
2023-06-16 11:57:55 -04:00
|
|
|
await rc.app_message(prr, message)
|
2023-06-15 20:22:54 -04:00
|
|
|
|
|
|
|
# we should get the same message back
|
2023-06-17 14:34:09 -04:00
|
|
|
update: veilid.VeilidUpdate = await asyncio.wait_for(
|
|
|
|
app_message_queue.get(), timeout=10
|
|
|
|
)
|
2023-06-16 11:57:55 -04:00
|
|
|
appmsg: veilid.VeilidAppMessage = update.detail
|
|
|
|
assert appmsg.message == message
|
2023-06-15 20:22:54 -04:00
|
|
|
|
2023-06-16 11:57:55 -04:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_routing_context_app_call_loopback():
|
|
|
|
app_call_queue = asyncio.Queue()
|
|
|
|
|
|
|
|
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 14:34:09 -04:00
|
|
|
hostname, port = server_info()
|
|
|
|
api = await veilid.json_api_connect(hostname, port, app_call_queue_update_callback)
|
2023-06-16 11:57:55 -04:00
|
|
|
async with api:
|
2023-06-16 13:14:34 -04:00
|
|
|
# purge routes to ensure we start fresh
|
|
|
|
await api.debug("purge routes")
|
|
|
|
|
2023-06-16 11:57:55 -04:00
|
|
|
# make a routing context that uses a safety route
|
|
|
|
rc = await (await api.new_routing_context()).with_privacy()
|
|
|
|
|
|
|
|
# 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-17 14:34:09 -04:00
|
|
|
|
2023-06-16 11:57:55 -04:00
|
|
|
# send an app message to our own private route
|
|
|
|
request = b"abcd1234"
|
2023-06-17 14:34:09 -04:00
|
|
|
app_call_task = asyncio.create_task(
|
|
|
|
rc.app_call(prr, request), name="app call task"
|
|
|
|
)
|
2023-06-16 11:57:55 -04:00
|
|
|
|
|
|
|
# we should get the same request back
|
2023-06-17 14:34:09 -04:00
|
|
|
update: veilid.VeilidUpdate = await asyncio.wait_for(
|
|
|
|
app_call_queue.get(), timeout=10
|
|
|
|
)
|
2023-06-16 11:57:55 -04:00
|
|
|
appcall: veilid.VeilidAppCall = update.detail
|
|
|
|
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
|