Prototype native Rust MXAccess worker
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
#![cfg(all(windows, target_arch = "x86"))]
|
||||
|
||||
#[path = "../src/wire.rs"]
|
||||
mod wire;
|
||||
|
||||
use std::process::{Child, Command};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use tokio::io::{split, AsyncRead, AsyncWrite};
|
||||
use tokio::net::windows::named_pipe::ServerOptions;
|
||||
use tokio::time::{sleep, timeout};
|
||||
use wire::mxaccess_gateway::v1::{
|
||||
mx_command, mx_command_reply, AddItemCommand, MxCommand, MxCommandKind, MxCommandReply,
|
||||
ProtocolStatusCode, RegisterCommand, RemoveItemCommand, UnregisterCommand,
|
||||
};
|
||||
use wire::mxaccess_worker::v1::{GatewayHello, WorkerCommand, WorkerShutdown};
|
||||
use wire::{Body, Writer, DEFAULT_MAX, VERSION};
|
||||
|
||||
const DEADLINE: Duration = Duration::from_secs(20);
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a registered 32-bit System Platform MXAccess installation"]
|
||||
async fn activates_registers_and_shuts_down_over_a_windows_pipe() {
|
||||
let token = format!(
|
||||
"{}-{}",
|
||||
std::process::id(),
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
);
|
||||
let pipe_name = format!("mxgw-rust-smoke-{token}");
|
||||
let pipe = ServerOptions::new()
|
||||
.first_pipe_instance(true)
|
||||
.create(format!(r"\\.\pipe\{pipe_name}"))
|
||||
.unwrap();
|
||||
let worker_path = std::env::var_os("MXGATEWAY_RUST_SMOKE_WORKER")
|
||||
.unwrap_or_else(|| env!("CARGO_BIN_EXE_mxgateway-worker-rust").into());
|
||||
let mut child = ChildGuard(
|
||||
Command::new(worker_path)
|
||||
.args([
|
||||
"--session-id",
|
||||
&token,
|
||||
"--pipe-name",
|
||||
&pipe_name,
|
||||
"--protocol-version",
|
||||
"1",
|
||||
])
|
||||
.env("MXGATEWAY_WORKER_NONCE", &token)
|
||||
.env("MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS", "100")
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let session = &token;
|
||||
timeout(DEADLINE, async {
|
||||
pipe.connect().await.unwrap();
|
||||
let (mut reader, sender) = split(pipe);
|
||||
let mut writer = Writer::new(sender);
|
||||
let Body::WorkerHello(hello) = read_body(&mut reader, session).await else {
|
||||
panic!("expected WorkerHello");
|
||||
};
|
||||
assert_eq!(hello.nonce, token);
|
||||
assert_eq!(hello.worker_process_id, child.0.id() as i32);
|
||||
writer
|
||||
.send(
|
||||
session,
|
||||
"",
|
||||
Body::GatewayHello(GatewayHello {
|
||||
supported_protocol_version: VERSION,
|
||||
nonce: token.clone(),
|
||||
max_frame_bytes: 4096,
|
||||
..Default::default()
|
||||
}),
|
||||
DEFAULT_MAX,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let ready = read_body(&mut reader, session).await;
|
||||
let Body::WorkerReady(ready) = ready else {
|
||||
panic!("expected WorkerReady, got {ready:?}");
|
||||
};
|
||||
assert_eq!(ready.worker_process_id, child.0.id() as i32);
|
||||
let Body::WorkerHeartbeat(heartbeat) = read_body(&mut reader, session).await else {
|
||||
panic!("expected an idle heartbeat");
|
||||
};
|
||||
assert_eq!(heartbeat.worker_process_id, ready.worker_process_id);
|
||||
|
||||
let registration = invoke(
|
||||
&mut reader,
|
||||
&mut writer,
|
||||
session,
|
||||
"register",
|
||||
MxCommandKind::Register,
|
||||
mx_command::Payload::Register(RegisterCommand {
|
||||
client_name: format!("mxgw-rust-smoke-{}", child.0.id()),
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let Some(mx_command_reply::Payload::Register(registration)) = registration.payload else {
|
||||
panic!("missing registration handle");
|
||||
};
|
||||
assert!(registration.server_handle > 0);
|
||||
if let Ok(definition) = std::env::var("MXGATEWAY_RUST_SMOKE_ITEM") {
|
||||
let add_item = invoke(
|
||||
&mut reader,
|
||||
&mut writer,
|
||||
session,
|
||||
"add",
|
||||
MxCommandKind::AddItem,
|
||||
mx_command::Payload::AddItem(AddItemCommand {
|
||||
server_handle: registration.server_handle,
|
||||
item_definition: definition,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let Some(mx_command_reply::Payload::AddItem(item)) = add_item.payload else {
|
||||
panic!("missing item handle");
|
||||
};
|
||||
assert!(item.item_handle > 0);
|
||||
invoke(
|
||||
&mut reader,
|
||||
&mut writer,
|
||||
session,
|
||||
"remove",
|
||||
MxCommandKind::RemoveItem,
|
||||
mx_command::Payload::RemoveItem(RemoveItemCommand {
|
||||
server_handle: registration.server_handle,
|
||||
item_handle: item.item_handle,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
invoke(
|
||||
&mut reader,
|
||||
&mut writer,
|
||||
session,
|
||||
"unregister",
|
||||
MxCommandKind::Unregister,
|
||||
mx_command::Payload::Unregister(UnregisterCommand {
|
||||
server_handle: registration.server_handle,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Leave a registration active to exercise the worker's shutdown cleanup.
|
||||
invoke(
|
||||
&mut reader,
|
||||
&mut writer,
|
||||
session,
|
||||
"cleanup",
|
||||
MxCommandKind::Register,
|
||||
mx_command::Payload::Register(RegisterCommand {
|
||||
client_name: format!("mxgw-rust-cleanup-{}", child.0.id()),
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
writer
|
||||
.send(
|
||||
session,
|
||||
"",
|
||||
Body::WorkerShutdown(WorkerShutdown::default()),
|
||||
4096,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
loop {
|
||||
match read_body(&mut reader, session).await {
|
||||
Body::WorkerHeartbeat(_) => {}
|
||||
Body::WorkerShutdownAck(ack) => {
|
||||
let status = ack.status.unwrap();
|
||||
assert_eq!(status.code(), ProtocolStatusCode::Ok);
|
||||
assert_eq!(status.message, "Graceful shutdown completed");
|
||||
break;
|
||||
}
|
||||
other => panic!("expected shutdown acknowledgement, got {other:?}"),
|
||||
}
|
||||
}
|
||||
loop {
|
||||
if let Some(status) = child.0.try_wait().unwrap() {
|
||||
assert!(status.success(), "{status}");
|
||||
break;
|
||||
}
|
||||
sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("live worker smoke test timed out");
|
||||
}
|
||||
|
||||
async fn invoke(
|
||||
reader: &mut (impl AsyncRead + Unpin),
|
||||
writer: &mut Writer<impl AsyncWrite + Unpin>,
|
||||
session: &str,
|
||||
correlation: &str,
|
||||
kind: MxCommandKind,
|
||||
payload: mx_command::Payload,
|
||||
) -> MxCommandReply {
|
||||
writer
|
||||
.send(
|
||||
session,
|
||||
correlation,
|
||||
Body::WorkerCommand(WorkerCommand {
|
||||
command: Some(MxCommand {
|
||||
kind: kind as i32,
|
||||
payload: Some(payload),
|
||||
}),
|
||||
enqueue_timestamp: None,
|
||||
}),
|
||||
4096,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
loop {
|
||||
let frame = wire::read(reader, session, 4096).await.unwrap();
|
||||
match frame.body.unwrap() {
|
||||
Body::WorkerHeartbeat(_) => {}
|
||||
Body::WorkerCommandReply(result) => {
|
||||
let reply = result.reply.unwrap();
|
||||
assert_eq!(frame.correlation_id, correlation);
|
||||
assert_eq!(reply.correlation_id, correlation);
|
||||
assert_eq!(reply.kind(), kind);
|
||||
assert_eq!(
|
||||
reply.protocol_status.as_ref().unwrap().code(),
|
||||
ProtocolStatusCode::Ok,
|
||||
"{reply:?}"
|
||||
);
|
||||
return reply;
|
||||
}
|
||||
other => panic!("expected command reply, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_body(reader: &mut (impl AsyncRead + Unpin), session: &str) -> Body {
|
||||
wire::read(reader, session, 4096)
|
||||
.await
|
||||
.unwrap()
|
||||
.body
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
struct ChildGuard(Child);
|
||||
|
||||
impl Drop for ChildGuard {
|
||||
fn drop(&mut self) {
|
||||
if matches!(self.0.try_wait(), Ok(None)) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user