Resolve Client.Rust-022..029: MalformedReply, correlation ids, clippy
Client.Rust-022 Restored Error::MalformedReply for register / add_item /
add_item2 and the bulk-subscribe / read-bulk / write-bulk
dispatch arms so malformed-but-OK replies fail loudly
instead of returning Vec::new().
Client.Rust-023 Restored next_correlation_id and routed every CLI close /
stream-alarms / acknowledge-alarm / bench-read-bulk call
through it so each call carries a unique opaque token.
Client.Rust-024 Added round-trip tests for read_bulk / write_bulk /
write2_bulk / write_secured_bulk / write_secured2_bulk
plus stream_alarms and percentile_summary unit tests.
Client.Rust-025 RustClientDesign.md re-synced — new bulk SDK, alarms
surface, Error variants, CLI command list, and the
Windows stack workaround.
Client.Rust-026 Session::read_bulk now borrows a tag slice; bench-read-
bulk binds tags once outside the warm-up / steady-state
loops.
Client.Rust-027 .cargo/config.toml selector tightened to
cfg(all(windows, target_env = "msvc")) and comment
rewritten to match reality (release + debug ship the
8 MB reservation).
Client.Rust-028 run_batch removed the empty-line break; stdin EOF is
the only terminator.
Client.Rust-029 Re-applied Client.Rust-001 / 002 / 012 — added the
missing doc comments, renamed BulkReplyKind variants,
and replaced the clone-on-copy with a deref under lock
so cargo clippy -D warnings is clean.
All resolved at 2026-05-24; cargo fmt + check + clippy + test all green
(55 tests).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
[target.'cfg(windows)']
|
||||
# Bump the default 1 MB Windows stack to 8 MB. clap-derive builds a large
|
||||
# Command enum in this CLI (one variant per subcommand, each carrying flag
|
||||
# args); in debug builds the enum is materialized on the stack without
|
||||
# MSVC-only: bump the default 1 MB Windows stack to 8 MB. clap-derive builds
|
||||
# a large Command enum in this CLI (one variant per subcommand, each carrying
|
||||
# flag args); in debug builds the enum is materialized on the stack without
|
||||
# optimization and overflows the default Windows main-thread stack before
|
||||
# even reaching our code. Release builds are unaffected but the e2e matrix
|
||||
# drives the CLI through `cargo run` (debug), so the link-arg ships with
|
||||
# every dev-time invocation.
|
||||
# even reaching our code.
|
||||
#
|
||||
# The /STACK: link-arg goes into the PE header's IMAGE_OPTIONAL_HEADER.
|
||||
# SizeOfStackReserve at link time and applies to both debug and release
|
||||
# builds — release artifacts ship with the same 8 MB stack reservation. At
|
||||
# runtime the optimizer elides the enum from the stack frame, so release
|
||||
# builds would not overflow without this setting; it is kept on for them so
|
||||
# both build flavours produce binaries with identical stack metadata.
|
||||
#
|
||||
# `/STACK:` is an MSVC-linker (`link.exe` / `lld-link`) directive. The
|
||||
# `target_env = "msvc"` selector below scopes the rustflag to the MSVC
|
||||
# toolchain so `x86_64-pc-windows-gnu` (mingw) builds, which route link
|
||||
# args through the GNU linker and reject `/STACK:`, are unaffected.
|
||||
[target.'cfg(all(windows, target_env = "msvc"))']
|
||||
rustflags = ["-C", "link-arg=/STACK:8388608"]
|
||||
|
||||
@@ -56,7 +56,23 @@ Expected dependencies:
|
||||
- `clap`
|
||||
- `serde`
|
||||
- `serde_json`
|
||||
- `tracing`
|
||||
|
||||
## Windows Build Notes
|
||||
|
||||
`clients/rust/.cargo/config.toml` carries an MSVC-scoped rustflag that bumps
|
||||
the default 1 MB Windows main-thread stack to 8 MB
|
||||
(`-C link-arg=/STACK:8388608`, under `cfg(all(windows, target_env = "msvc"))`).
|
||||
The setting is required because clap-derive materialises a large `Command`
|
||||
enum (one variant per CLI subcommand, each carrying its flag args) on the
|
||||
main task's stack in debug builds, before any user code runs; the default 1
|
||||
MB stack overflows during enum construction. The `/STACK:` link-arg writes
|
||||
into the PE header's `IMAGE_OPTIONAL_HEADER.SizeOfStackReserve` at link
|
||||
time, so both debug and release artifacts ship with the same 8 MB stack
|
||||
reservation. Release builds would not overflow without it (the optimizer
|
||||
elides the enum from the stack frame), but the setting is kept on for
|
||||
release too so both build flavours produce binaries with identical stack
|
||||
metadata. The MSVC-only selector keeps `x86_64-pc-windows-gnu` (mingw)
|
||||
builds unaffected, since the GNU linker rejects `/STACK:`.
|
||||
|
||||
## Library API
|
||||
|
||||
@@ -94,18 +110,65 @@ impl Session {
|
||||
pub async fn add_item(&self, server_handle: i32, item: &str) -> Result<i32, Error>;
|
||||
pub async fn add_item2(&self, server_handle: i32, item: &str, context: &str) -> Result<i32, Error>;
|
||||
pub async fn advise(&self, server_handle: i32, item_handle: i32) -> Result<(), Error>;
|
||||
pub async fn un_advise(&self, server_handle: i32, item_handle: i32) -> Result<(), Error>;
|
||||
pub async fn remove_item(&self, server_handle: i32, item_handle: i32) -> Result<(), Error>;
|
||||
pub async fn add_item_bulk(&self, server_handle: i32, tag_addresses: Vec<String>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn advise_item_bulk(&self, server_handle: i32, item_handles: Vec<i32>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn remove_item_bulk(&self, server_handle: i32, item_handles: Vec<i32>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn un_advise_item_bulk(&self, server_handle: i32, item_handles: Vec<i32>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn subscribe_bulk(&self, server_handle: i32, tag_addresses: Vec<String>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn unsubscribe_bulk(&self, server_handle: i32, item_handles: Vec<i32>) -> Result<Vec<SubscribeResult>, Error>;
|
||||
pub async fn read_bulk<S: AsRef<str>>(&self, server_handle: i32, tag_addresses: &[S], timeout_ms: u32) -> Result<Vec<BulkReadResult>, Error>;
|
||||
pub async fn write(&self, server_handle: i32, item_handle: i32, value: MxValue, user_id: i32) -> Result<(), Error>;
|
||||
pub async fn write2(&self, server_handle: i32, item_handle: i32, value: MxValue, timestamp_value: MxValue, user_id: i32) -> Result<(), Error>;
|
||||
pub async fn write_bulk(&self, server_handle: i32, entries: Vec<WriteBulkEntry>) -> Result<Vec<BulkWriteResult>, Error>;
|
||||
pub async fn write2_bulk(&self, server_handle: i32, entries: Vec<Write2BulkEntry>) -> Result<Vec<BulkWriteResult>, Error>;
|
||||
pub async fn write_secured_bulk(&self, server_handle: i32, entries: Vec<WriteSecuredBulkEntry>) -> Result<Vec<BulkWriteResult>, Error>;
|
||||
pub async fn write_secured2_bulk(&self, server_handle: i32, entries: Vec<WriteSecured2BulkEntry>) -> Result<Vec<BulkWriteResult>, Error>;
|
||||
pub async fn events(&self) -> Result<impl Stream<Item = Result<MxEvent, Error>>, Error>;
|
||||
pub async fn close(&self) -> Result<(), Error>;
|
||||
}
|
||||
```
|
||||
|
||||
The per-entry credentials and timestamps (`user_id`, `timestamp_value`,
|
||||
`current_user_id`, `verifier_user_id`) live on the `WriteBulkEntry` /
|
||||
`Write2BulkEntry` / `WriteSecuredBulkEntry` / `WriteSecured2BulkEntry`
|
||||
structs rather than as trailing positional arguments on the bulk-write
|
||||
helpers, matching the protobuf shapes in `mxaccess_gateway.proto`.
|
||||
`read_bulk` is generic over `AsRef<str>` so callers can pass `&[String]` or
|
||||
`&[&str]` without cloning at the call site (the cross-language bench-read-bulk
|
||||
hot loop relies on this).
|
||||
|
||||
The `session::next_correlation_id` helper is `pub` and re-exported at the
|
||||
crate root (`zb_mom_ww_mxgateway_client::next_correlation_id`); raw-RPC
|
||||
consumers like the `mxgw` CLI's `Ping`, `CloseSession`, `StreamAlarms`,
|
||||
`AcknowledgeAlarm`, and `BenchReadBulk` paths call it so every request
|
||||
carries a unique correlation id that gateway logs can tell apart from
|
||||
concurrent CLI smokes. The textual format is intentionally not part of the
|
||||
public contract.
|
||||
|
||||
## Alarms
|
||||
|
||||
`GatewayClient` exposes the gateway's session-less central alarm surface:
|
||||
|
||||
```rust
|
||||
pub type AlarmFeedStream = Pin<Box<dyn Stream<Item = Result<AlarmFeedMessage, Error>> + Send + 'static>>;
|
||||
|
||||
impl GatewayClient {
|
||||
pub async fn stream_alarms(&self, request: StreamAlarmsRequest) -> Result<AlarmFeedStream, Error>;
|
||||
pub async fn acknowledge_alarm(&self, request: AcknowledgeAlarmRequest) -> Result<AcknowledgeAlarmReply, Error>;
|
||||
}
|
||||
```
|
||||
|
||||
`stream_alarms` opens with one `active_alarm` per currently-active alarm
|
||||
(the ConditionRefresh snapshot), then a single `snapshot_complete`, then a
|
||||
`transition` for every subsequent raise / acknowledge / clear. The feed is
|
||||
served by the gateway's always-on alarm monitor — no worker session is
|
||||
opened — so any number of clients may attach. Dropping the stream cancels
|
||||
the gRPC call cooperatively. `acknowledge_alarm` is idempotent at the
|
||||
MxAccess layer; the returned `AcknowledgeAlarmReply` carries the native
|
||||
MxStatus from the worker.
|
||||
|
||||
## Authentication
|
||||
|
||||
Use a `tonic` interceptor or request extension layer to add:
|
||||
@@ -140,19 +203,31 @@ Use `thiserror`:
|
||||
|
||||
```rust
|
||||
pub enum Error {
|
||||
InvalidEndpoint { endpoint: String, detail: String },
|
||||
InvalidArgument { name: String, detail: String },
|
||||
Transport(tonic::transport::Error),
|
||||
Status(tonic::Status),
|
||||
Authentication(String),
|
||||
Authorization(String),
|
||||
Session(SessionError),
|
||||
Worker(WorkerError),
|
||||
Command(CommandError),
|
||||
MxAccess(MxAccessError),
|
||||
Timeout,
|
||||
Cancelled,
|
||||
Authentication { message: String, status: Box<tonic::Status> },
|
||||
Authorization { message: String, status: Box<tonic::Status> },
|
||||
Timeout { message: String, status: Box<tonic::Status> },
|
||||
Cancelled { message: String, status: Box<tonic::Status> },
|
||||
Unavailable { message: String, status: Box<tonic::Status> },
|
||||
Status(Box<tonic::Status>),
|
||||
Command(Box<CommandError>),
|
||||
ProtocolStatus { operation: &'static str, code: ProtocolStatusCode, message: String },
|
||||
MalformedReply { detail: String },
|
||||
}
|
||||
```
|
||||
|
||||
- `Unavailable` classifies `Code::Unavailable` / `Code::ResourceExhausted`
|
||||
so callers can distinguish transient failures from permanent ones.
|
||||
- `MalformedReply` surfaces the rare case where the gateway returned a
|
||||
protocol-level `Ok` envelope but the typed payload arm was missing or did
|
||||
not match the command kind (e.g. an `AddItemReply` body on a `WriteBulk`
|
||||
reply). This is distinct from `ProtocolStatus` because the protocol-level
|
||||
envelope itself succeeded; the corruption is in the payload shape.
|
||||
- `InvalidEndpoint` is raised before any RPC dispatch when the endpoint URL
|
||||
or CA file fails to parse / load.
|
||||
|
||||
Preserve raw command replies in `CommandError` where applicable.
|
||||
|
||||
## Test CLI
|
||||
@@ -165,11 +240,39 @@ Commands:
|
||||
|
||||
```text
|
||||
mxgw version
|
||||
mxgw smoke --endpoint http://localhost:5000 --api-key-env MXGATEWAY_API_KEY --item TestChildObject.TestInt
|
||||
mxgw ping
|
||||
mxgw open-session
|
||||
mxgw close-session --session-id <id>
|
||||
mxgw register --session-id <id>
|
||||
mxgw add-item --session-id <id> --server-handle <h> --item <tag>
|
||||
mxgw advise --session-id <id> --server-handle <h> --item-handle <h>
|
||||
mxgw subscribe-bulk --session-id <id> --server-handle <h> --items <csv>
|
||||
mxgw unsubscribe-bulk --session-id <id> --server-handle <h> --item-handles <csv>
|
||||
mxgw read-bulk --session-id <id> --server-handle <h> --items <csv> [--timeout-ms <ms>]
|
||||
mxgw write --session-id <id> --server-handle 1 --item-handle 1 --value-type int32 --value 123
|
||||
mxgw write2 --session-id <id> --server-handle 1 --item-handle 1 --value-type int32 --value 123 --timestamp <iso>
|
||||
mxgw write-bulk --session-id <id> --server-handle <h> --item-handles <csv> --value-type <t> --values <csv>
|
||||
mxgw write2-bulk ...
|
||||
mxgw write-secured-bulk ...
|
||||
mxgw write-secured2-bulk ...
|
||||
mxgw stream-events --session-id <id> --json
|
||||
mxgw write --session-id <id> --server-handle 1 --item-handle 1 --type int32 --value 123
|
||||
mxgw stream-alarms [--filter-prefix <prefix>] [--max-events <n>]
|
||||
mxgw acknowledge-alarm --reference <full-ref> [--comment <txt>] [--operator <user>]
|
||||
mxgw bench-read-bulk [--duration-seconds <n>] [--warmup-seconds <n>] [--bulk-size <n>]
|
||||
mxgw smoke --endpoint http://localhost:5000 --api-key-env MXGATEWAY_API_KEY --item TestChildObject.TestInt
|
||||
mxgw batch
|
||||
mxgw galaxy {test-connection,last-deploy-time,discover-hierarchy,watch}
|
||||
```
|
||||
|
||||
`batch` reads commands from stdin one per line and dispatches each through
|
||||
the normal subcommand path; the loop terminates only on stdin EOF (blank
|
||||
lines log an empty-EOR-bracketed result and continue) so accidental empty
|
||||
lines from the PowerShell e2e harness do not silently end the session.
|
||||
`bench-read-bulk` opens its own session, subscribes to `--bulk-size` tags so
|
||||
the worker's value cache populates from OnDataChange events, hammers
|
||||
`read_bulk` in a tight loop for `--duration-seconds`, and emits the
|
||||
cross-language JSON shape that `scripts/bench-read-bulk.ps1` collates.
|
||||
|
||||
JSON output should use `serde_json`.
|
||||
|
||||
## Unit Tests
|
||||
|
||||
@@ -26,8 +26,8 @@ use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::{
|
||||
WriteBulkEntry, WriteSecured2BulkEntry, WriteSecuredBulkEntry,
|
||||
};
|
||||
use zb_mom_ww_mxgateway_client::{
|
||||
ApiKey, ClientOptions, Error, GalaxyClient, GatewayClient, MxValue, MxValueProjection,
|
||||
CLIENT_VERSION, GATEWAY_PROTOCOL_VERSION, WORKER_PROTOCOL_VERSION,
|
||||
next_correlation_id, ApiKey, ClientOptions, Error, GalaxyClient, GatewayClient, MxValue,
|
||||
MxValueProjection, CLIENT_VERSION, GATEWAY_PROTOCOL_VERSION, WORKER_PROTOCOL_VERSION,
|
||||
};
|
||||
|
||||
const MAX_AGGREGATE_EVENTS: usize = 10_000;
|
||||
@@ -359,8 +359,9 @@ enum Command {
|
||||
/// write `__MXGW_BATCH_EOR__` to stdout after every result. Errors are
|
||||
/// written as `{"error":"…","type":"error"}` JSON to stdout (not stderr)
|
||||
/// so the harness can parse them without interleaving stderr. The loop
|
||||
/// never terminates on command error; only stdin EOF (or an empty line)
|
||||
/// ends the session.
|
||||
/// never terminates on command error or accidental blank lines; only
|
||||
/// stdin EOF ends the session — empty lines log an empty-EOR-bracketed
|
||||
/// result and continue, matching the other four language CLIs.
|
||||
Batch,
|
||||
#[command(subcommand)]
|
||||
Galaxy(GalaxyCommand),
|
||||
@@ -503,7 +504,7 @@ async fn dispatch(command: Command) -> Result<(), Error> {
|
||||
let client = connect(connection).await?;
|
||||
let reply = client
|
||||
.invoke(MxCommandRequest {
|
||||
client_correlation_id: "rust-cli-ping".to_owned(),
|
||||
client_correlation_id: next_correlation_id("cli-ping"),
|
||||
command: Some(MxCommand {
|
||||
kind: MxCommandKind::Ping as i32,
|
||||
payload: Some(zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::mx_command::Payload::Ping(
|
||||
@@ -550,7 +551,7 @@ async fn dispatch(command: Command) -> Result<(), Error> {
|
||||
let reply = client
|
||||
.close_session_raw(CloseSessionRequest {
|
||||
session_id,
|
||||
client_correlation_id: "rust-cli-close-session".to_owned(),
|
||||
client_correlation_id: next_correlation_id("cli-close-session"),
|
||||
})
|
||||
.await?;
|
||||
if json {
|
||||
@@ -624,7 +625,7 @@ async fn dispatch(command: Command) -> Result<(), Error> {
|
||||
json,
|
||||
} => {
|
||||
let session = session_for(connection, session_id).await?;
|
||||
let results = session.read_bulk(server_handle, items, timeout_ms).await?;
|
||||
let results = session.read_bulk(server_handle, &items, timeout_ms).await?;
|
||||
print_read_bulk_results("read-bulk", &results, json);
|
||||
}
|
||||
Command::WriteBulk {
|
||||
@@ -832,7 +833,7 @@ async fn dispatch(command: Command) -> Result<(), Error> {
|
||||
let client = connect(connection).await?;
|
||||
let mut stream = client
|
||||
.stream_alarms(StreamAlarmsRequest {
|
||||
client_correlation_id: "rust-cli-stream-alarms".to_owned(),
|
||||
client_correlation_id: next_correlation_id("cli-stream-alarms"),
|
||||
alarm_filter_prefix: filter_prefix.unwrap_or_default(),
|
||||
})
|
||||
.await?;
|
||||
@@ -869,7 +870,7 @@ async fn dispatch(command: Command) -> Result<(), Error> {
|
||||
let client = connect(connection).await?;
|
||||
let reply = client
|
||||
.acknowledge_alarm(AcknowledgeAlarmRequest {
|
||||
client_correlation_id: "rust-cli-acknowledge-alarm".to_owned(),
|
||||
client_correlation_id: next_correlation_id("cli-acknowledge-alarm"),
|
||||
alarm_full_reference: reference,
|
||||
comment,
|
||||
operator_user: operator,
|
||||
@@ -1113,8 +1114,15 @@ const BATCH_EOR: &str = "__MXGW_BATCH_EOR__";
|
||||
/// each through the normal [`dispatch`] path, and write [`BATCH_EOR`] to
|
||||
/// stdout after every result. Errors are serialised as JSON to stdout so
|
||||
/// the harness can parse them without interleaving stderr. The loop never
|
||||
/// terminates on command error; only stdin EOF or an empty line ends the
|
||||
/// session.
|
||||
/// terminates on command error or accidental blank lines; only stdin EOF
|
||||
/// ends the session — empty lines log an empty-EOR-bracketed result and
|
||||
/// continue.
|
||||
///
|
||||
/// `std::io::Stdin::lock().lines()` is a blocking iterator and the dispatch
|
||||
/// future is spawned on a separate tokio task so the runtime's main worker
|
||||
/// stays free. When the runtime is multi-threaded the blocking read keeps
|
||||
/// one worker parked on `ReadFile`; that is acceptable here because no other
|
||||
/// future on the main task needs to run while we wait for the next command.
|
||||
async fn run_batch() -> Result<(), Error> {
|
||||
let stdin = io::stdin();
|
||||
let stdout = io::stdout();
|
||||
@@ -1125,12 +1133,11 @@ async fn run_batch() -> Result<(), Error> {
|
||||
detail: e.to_string(),
|
||||
})?;
|
||||
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
|
||||
if parts.is_empty() {
|
||||
// Empty / whitespace-only line: log an empty-EOR-bracketed
|
||||
// result and continue so accidental blank lines from the
|
||||
// PowerShell e2e harness do not silently end the session.
|
||||
println!("{BATCH_EOR}");
|
||||
stdout.lock().flush().ok();
|
||||
continue;
|
||||
@@ -1388,6 +1395,7 @@ async fn run_bench_read_bulk(
|
||||
let bench_outcome = async {
|
||||
let server_handle = session.register(&client_name).await?;
|
||||
let subscribe_results = session.subscribe_bulk(server_handle, tags.clone()).await?;
|
||||
let tags_ref: &[String] = &tags;
|
||||
let item_handles: Vec<i32> = subscribe_results
|
||||
.iter()
|
||||
.filter(|r| r.was_successful)
|
||||
@@ -1401,7 +1409,7 @@ async fn run_bench_read_bulk(
|
||||
let warmup_deadline = Instant::now() + Duration::from_secs(warmup_seconds);
|
||||
while Instant::now() < warmup_deadline {
|
||||
let _ = session
|
||||
.read_bulk(server_handle, tags.clone(), timeout_ms_param)
|
||||
.read_bulk(server_handle, tags_ref, timeout_ms_param)
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -1419,7 +1427,7 @@ async fn run_bench_read_bulk(
|
||||
while Instant::now() < steady_deadline {
|
||||
let call_start = Instant::now();
|
||||
let result = session
|
||||
.read_bulk(server_handle, tags.clone(), timeout_ms_param)
|
||||
.read_bulk(server_handle, tags_ref, timeout_ms_param)
|
||||
.await;
|
||||
let elapsed = call_start.elapsed();
|
||||
latencies_ms.push(elapsed.as_secs_f64() * 1000.0);
|
||||
@@ -1473,7 +1481,7 @@ async fn run_bench_read_bulk(
|
||||
let close_result = client
|
||||
.close_session_raw(CloseSessionRequest {
|
||||
session_id: session_id.clone(),
|
||||
client_correlation_id: "rust-cli-bench-read-bulk-close".to_owned(),
|
||||
client_correlation_id: next_correlation_id("cli-bench-read-bulk-close"),
|
||||
})
|
||||
.await;
|
||||
|
||||
@@ -2100,6 +2108,43 @@ mod tests {
|
||||
assert_eq!(super::BATCH_EOR, "__MXGW_BATCH_EOR__");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_percentile_summary_matches_hand_built_sample() {
|
||||
// Hand-built sample with 5 values: 1, 2, 3, 4, 5.
|
||||
let sample: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
|
||||
let summary = super::percentile_summary(&sample);
|
||||
|
||||
assert_eq!(summary.max, 5.0);
|
||||
// Mean = 15/5 = 3.0
|
||||
assert!((summary.mean - 3.0).abs() < f64::EPSILON);
|
||||
// p50: rank = 0.5 * 4 = 2 -> sorted[2] = 3.0
|
||||
assert!((summary.p50 - 3.0).abs() < f64::EPSILON);
|
||||
// p95: rank = 0.95 * 4 = 3.8 -> 4.0 + 0.8 * (5.0 - 4.0) = 4.8
|
||||
assert!((summary.p95 - 4.8).abs() < f64::EPSILON);
|
||||
// p99: rank = 0.99 * 4 = 3.96 -> 4.0 + 0.96 * 1.0 = 4.96
|
||||
assert!((summary.p99 - 4.96).abs() < f64::EPSILON);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_percentile_summary_handles_empty_sample() {
|
||||
let summary = super::percentile_summary(&[]);
|
||||
assert_eq!(summary.p50, 0.0);
|
||||
assert_eq!(summary.p95, 0.0);
|
||||
assert_eq!(summary.p99, 0.0);
|
||||
assert_eq!(summary.max, 0.0);
|
||||
assert_eq!(summary.mean, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_percentile_summary_handles_single_value_sample() {
|
||||
let summary = super::percentile_summary(&[42.0]);
|
||||
assert_eq!(summary.p50, 42.0);
|
||||
assert_eq!(summary.p95, 42.0);
|
||||
assert_eq!(summary.p99, 42.0);
|
||||
assert_eq!(summary.max, 42.0);
|
||||
assert_eq!(summary.mean, 42.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rfc3339_parser_round_trips_z_and_offset_inputs() {
|
||||
// 2026-04-28T15:30:00Z = 1_777_995_000 (sanity-checked once below)
|
||||
|
||||
@@ -106,6 +106,27 @@ pub enum Error {
|
||||
/// Detail message from the server.
|
||||
message: String,
|
||||
},
|
||||
|
||||
/// Gateway returned an `Ok` protocol status but the reply lacked the
|
||||
/// expected typed payload (or carried the wrong payload arm). Distinct
|
||||
/// from [`Error::ProtocolStatus`] because the protocol-level envelope
|
||||
/// itself succeeded — the corruption is in the payload shape.
|
||||
#[error("gateway returned a malformed reply: {detail}")]
|
||||
MalformedReply {
|
||||
/// Human-readable description of what was missing or mismatched.
|
||||
detail: String,
|
||||
},
|
||||
|
||||
/// Server returned `Unavailable` or `ResourceExhausted` — classify
|
||||
/// transient failures separately from the catch-all [`Error::Status`].
|
||||
#[error("gateway unavailable: {message}")]
|
||||
Unavailable {
|
||||
/// Redacted server-supplied detail message.
|
||||
message: String,
|
||||
/// Original `tonic::Status`.
|
||||
#[source]
|
||||
status: Box<tonic::Status>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Wrapper around an [`MxCommandReply`] whose `protocol_status` reported a
|
||||
@@ -174,6 +195,10 @@ impl From<tonic::Status> for Error {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
Code::Unavailable | Code::ResourceExhausted => Self::Unavailable {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
_ => Self::Status(Box::new(status)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ mod tests {
|
||||
_request: Request<GetLastDeployTimeRequest>,
|
||||
) -> Result<Response<GetLastDeployTimeReply>, Status> {
|
||||
let present = *self.state.present.lock().unwrap();
|
||||
let time = self.state.last_deploy.lock().unwrap().clone();
|
||||
let time = *self.state.last_deploy.lock().unwrap();
|
||||
Ok(Response::new(GetLastDeployTimeReply {
|
||||
present,
|
||||
time_of_last_deploy: time,
|
||||
|
||||
@@ -32,7 +32,7 @@ pub use galaxy::{DeployEventStream, GalaxyClient};
|
||||
#[doc(inline)]
|
||||
pub use options::ClientOptions;
|
||||
#[doc(inline)]
|
||||
pub use session::Session;
|
||||
pub use session::{next_correlation_id, Session};
|
||||
#[doc(inline)]
|
||||
pub use value::{MxArrayProjection, MxArrayValue, MxStatus, MxValue, MxValueProjection};
|
||||
#[doc(inline)]
|
||||
|
||||
@@ -95,6 +95,7 @@ impl ClientOptions {
|
||||
self
|
||||
}
|
||||
|
||||
/// Maximum encoded/decoded gRPC message size in bytes (default 16 MiB).
|
||||
pub fn with_max_grpc_message_bytes(mut self, max_grpc_message_bytes: usize) -> Self {
|
||||
self.max_grpc_message_bytes = max_grpc_message_bytes;
|
||||
self
|
||||
@@ -140,6 +141,7 @@ impl ClientOptions {
|
||||
self.stream_timeout
|
||||
}
|
||||
|
||||
/// Configured maximum encoded/decoded gRPC message size in bytes.
|
||||
pub fn max_grpc_message_bytes(&self) -> usize {
|
||||
self.max_grpc_message_bytes
|
||||
}
|
||||
|
||||
+104
-61
@@ -8,6 +8,8 @@
|
||||
//! Bulk commands enforce a 1000-item cap before contacting the worker, in
|
||||
//! line with the gateway's documented `MAX_BULK_ITEMS`.
|
||||
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use crate::client::{EventStream, GatewayClient};
|
||||
use crate::error::{ensure_protocol_success, Error};
|
||||
use crate::generated::mxaccess_gateway::v1::mx_command::Payload;
|
||||
@@ -26,6 +28,28 @@ use crate::value::MxValue;
|
||||
|
||||
const MAX_BULK_ITEMS: usize = 1_000;
|
||||
|
||||
/// Process-wide monotonic sequence used by [`next_correlation_id`].
|
||||
static CORRELATION_SEQUENCE: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
/// Build a per-call correlation id that embeds the supplied `label`.
|
||||
///
|
||||
/// The returned token is opaque and guaranteed to be unique within the
|
||||
/// current process: every call increments a process-wide atomic counter,
|
||||
/// so concurrent CLI smokes and library callers on the same machine produce
|
||||
/// distinct ids that gateway logs can tell apart. The token carries no
|
||||
/// embedded secret beyond `label`.
|
||||
///
|
||||
/// The exact textual format (currently `rust-client-{label}-{N}`) is *not*
|
||||
/// part of the public contract — callers must not parse it. The crate root
|
||||
/// re-exports this helper as
|
||||
/// [`zb_mom_ww_mxgateway_client::next_correlation_id`] so out-of-tree
|
||||
/// consumers can build correlation ids without referencing the `session`
|
||||
/// module path.
|
||||
pub fn next_correlation_id(label: &str) -> String {
|
||||
let sequence = CORRELATION_SEQUENCE.fetch_add(1, Ordering::Relaxed);
|
||||
format!("rust-client-{label}-{sequence}")
|
||||
}
|
||||
|
||||
/// Handle to an opened gateway session.
|
||||
///
|
||||
/// `Session` carries the gateway-issued session id and a cloned
|
||||
@@ -79,7 +103,7 @@ impl Session {
|
||||
.client
|
||||
.close_session_raw(CloseSessionRequest {
|
||||
session_id: self.id.clone(),
|
||||
client_correlation_id: "rust-client-close-session".to_owned(),
|
||||
client_correlation_id: next_correlation_id("close-session"),
|
||||
})
|
||||
.await?;
|
||||
ensure_protocol_success("close session", reply.protocol_status.as_ref())?;
|
||||
@@ -102,7 +126,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(register_server_handle(&reply))
|
||||
register_server_handle(&reply)
|
||||
}
|
||||
|
||||
/// Run MXAccess `AddItem` against `server_handle` and return the
|
||||
@@ -123,7 +147,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(add_item_handle(&reply))
|
||||
add_item_handle(&reply)
|
||||
}
|
||||
|
||||
/// Run MXAccess `AddItem2` (item with a caller-supplied context string)
|
||||
@@ -149,7 +173,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(add_item2_handle(&reply))
|
||||
add_item2_handle(&reply)
|
||||
}
|
||||
|
||||
/// Run MXAccess `RemoveItem` for the given handle pair.
|
||||
@@ -229,7 +253,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::AddItemBulk))
|
||||
bulk_results(reply, BulkReplyKind::AddItem)
|
||||
}
|
||||
|
||||
/// Bulk variant of [`Session::advise`].
|
||||
@@ -253,7 +277,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::AdviseItemBulk))
|
||||
bulk_results(reply, BulkReplyKind::AdviseItem)
|
||||
}
|
||||
|
||||
/// Bulk variant of [`Session::remove_item`].
|
||||
@@ -277,7 +301,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::RemoveItemBulk))
|
||||
bulk_results(reply, BulkReplyKind::RemoveItem)
|
||||
}
|
||||
|
||||
/// Bulk variant of [`Session::un_advise`].
|
||||
@@ -301,7 +325,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::UnAdviseItemBulk))
|
||||
bulk_results(reply, BulkReplyKind::UnAdviseItem)
|
||||
}
|
||||
|
||||
/// Bulk `Subscribe` (atomic add-and-advise) for a list of tag addresses.
|
||||
@@ -325,7 +349,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::SubscribeBulk))
|
||||
bulk_results(reply, BulkReplyKind::Subscribe)
|
||||
}
|
||||
|
||||
/// Bulk `Unsubscribe` (atomic un-advise-and-remove) for a list of
|
||||
@@ -350,7 +374,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_results(reply, BulkReplyKind::UnsubscribeBulk))
|
||||
bulk_results(reply, BulkReplyKind::Unsubscribe)
|
||||
}
|
||||
|
||||
/// Bulk `Read` — snapshot the current value for each requested tag.
|
||||
@@ -366,10 +390,10 @@ impl Session {
|
||||
/// # Errors
|
||||
///
|
||||
/// Same conditions as [`Session::add_item_bulk`].
|
||||
pub async fn read_bulk(
|
||||
pub async fn read_bulk<S: AsRef<str>>(
|
||||
&self,
|
||||
server_handle: i32,
|
||||
tag_addresses: Vec<String>,
|
||||
tag_addresses: &[S],
|
||||
timeout_ms: u32,
|
||||
) -> Result<Vec<BulkReadResult>, Error> {
|
||||
ensure_bulk_size("tag_addresses", tag_addresses.len())?;
|
||||
@@ -378,16 +402,21 @@ impl Session {
|
||||
MxCommandKind::ReadBulk,
|
||||
Payload::ReadBulk(ReadBulkCommand {
|
||||
server_handle,
|
||||
tag_addresses,
|
||||
tag_addresses: tag_addresses
|
||||
.iter()
|
||||
.map(|tag| tag.as_ref().to_owned())
|
||||
.collect(),
|
||||
timeout_ms,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(match reply.payload {
|
||||
Some(mx_command_reply::Payload::ReadBulk(reply)) => reply.results,
|
||||
_ => Vec::new(),
|
||||
})
|
||||
match reply.payload {
|
||||
Some(mx_command_reply::Payload::ReadBulk(reply)) => Ok(reply.results),
|
||||
_ => Err(Error::MalformedReply {
|
||||
detail: "read_bulk reply did not carry a ReadBulk payload".to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Bulk `Write` (sequential MXAccess Write per entry, on the worker's STA).
|
||||
@@ -416,7 +445,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_write_results(reply, BulkWriteReplyKind::Write))
|
||||
bulk_write_results(reply, BulkWriteReplyKind::Write)
|
||||
}
|
||||
|
||||
/// Bulk `Write2` (timestamped) — see [`Session::write_bulk`].
|
||||
@@ -440,7 +469,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_write_results(reply, BulkWriteReplyKind::Write2))
|
||||
bulk_write_results(reply, BulkWriteReplyKind::Write2)
|
||||
}
|
||||
|
||||
/// Bulk `WriteSecured` — credential-sensitive values follow the same
|
||||
@@ -465,7 +494,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_write_results(reply, BulkWriteReplyKind::WriteSecured))
|
||||
bulk_write_results(reply, BulkWriteReplyKind::WriteSecured)
|
||||
}
|
||||
|
||||
/// Bulk `WriteSecured2` (timestamped) — see [`Session::write_secured_bulk`].
|
||||
@@ -489,7 +518,7 @@ impl Session {
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(bulk_write_results(reply, BulkWriteReplyKind::WriteSecured2))
|
||||
bulk_write_results(reply, BulkWriteReplyKind::WriteSecured2)
|
||||
}
|
||||
|
||||
/// Run MXAccess `Write` (single-value, no caller-supplied timestamp).
|
||||
@@ -608,7 +637,7 @@ impl Session {
|
||||
fn command_request(&self, kind: MxCommandKind, payload: Payload) -> MxCommandRequest {
|
||||
MxCommandRequest {
|
||||
session_id: self.id.clone(),
|
||||
client_correlation_id: format!("rust-client-{}", kind.as_str_name()),
|
||||
client_correlation_id: next_correlation_id(kind.as_str_name()),
|
||||
command: Some(MxCommand {
|
||||
kind: kind as i32,
|
||||
payload: Some(payload),
|
||||
@@ -628,71 +657,80 @@ fn ensure_bulk_size(name: &'static str, len: usize) -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
fn register_server_handle(reply: &MxCommandReply) -> i32 {
|
||||
fn register_server_handle(reply: &MxCommandReply) -> Result<i32, Error> {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::Register(register)) => register.server_handle,
|
||||
Some(mx_command_reply::Payload::Register(register)) => Ok(register.server_handle),
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
.ok_or_else(|| Error::MalformedReply {
|
||||
detail: "register reply lacked a server_handle payload or int32 return_value"
|
||||
.to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_item_handle(reply: &MxCommandReply) -> i32 {
|
||||
fn add_item_handle(reply: &MxCommandReply) -> Result<i32, Error> {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::AddItem(add_item)) => add_item.item_handle,
|
||||
Some(mx_command_reply::Payload::AddItem(add_item)) => Ok(add_item.item_handle),
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
.ok_or_else(|| Error::MalformedReply {
|
||||
detail: "add_item reply lacked an item_handle payload or int32 return_value"
|
||||
.to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_item2_handle(reply: &MxCommandReply) -> i32 {
|
||||
fn add_item2_handle(reply: &MxCommandReply) -> Result<i32, Error> {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::AddItem2(add_item)) => add_item.item_handle,
|
||||
Some(mx_command_reply::Payload::AddItem2(add_item)) => Ok(add_item.item_handle),
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
.ok_or_else(|| Error::MalformedReply {
|
||||
detail: "add_item2 reply lacked an item_handle payload or int32 return_value"
|
||||
.to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
enum BulkReplyKind {
|
||||
AddItemBulk,
|
||||
AdviseItemBulk,
|
||||
RemoveItemBulk,
|
||||
UnAdviseItemBulk,
|
||||
SubscribeBulk,
|
||||
UnsubscribeBulk,
|
||||
AddItem,
|
||||
AdviseItem,
|
||||
RemoveItem,
|
||||
UnAdviseItem,
|
||||
Subscribe,
|
||||
Unsubscribe,
|
||||
}
|
||||
|
||||
fn bulk_results(reply: MxCommandReply, kind: BulkReplyKind) -> Vec<SubscribeResult> {
|
||||
fn bulk_results(reply: MxCommandReply, kind: BulkReplyKind) -> Result<Vec<SubscribeResult>, Error> {
|
||||
match (reply.payload, kind) {
|
||||
(Some(mx_command_reply::Payload::AddItemBulk(reply)), BulkReplyKind::AddItemBulk) => {
|
||||
reply.results
|
||||
(Some(mx_command_reply::Payload::AddItemBulk(reply)), BulkReplyKind::AddItem) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
(Some(mx_command_reply::Payload::AdviseItemBulk(reply)), BulkReplyKind::AdviseItemBulk) => {
|
||||
reply.results
|
||||
(Some(mx_command_reply::Payload::AdviseItemBulk(reply)), BulkReplyKind::AdviseItem) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
(Some(mx_command_reply::Payload::RemoveItemBulk(reply)), BulkReplyKind::RemoveItemBulk) => {
|
||||
reply.results
|
||||
(Some(mx_command_reply::Payload::RemoveItemBulk(reply)), BulkReplyKind::RemoveItem) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
(
|
||||
Some(mx_command_reply::Payload::UnAdviseItemBulk(reply)),
|
||||
BulkReplyKind::UnAdviseItemBulk,
|
||||
) => reply.results,
|
||||
(Some(mx_command_reply::Payload::SubscribeBulk(reply)), BulkReplyKind::SubscribeBulk) => {
|
||||
reply.results
|
||||
(Some(mx_command_reply::Payload::UnAdviseItemBulk(reply)), BulkReplyKind::UnAdviseItem) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
(
|
||||
Some(mx_command_reply::Payload::UnsubscribeBulk(reply)),
|
||||
BulkReplyKind::UnsubscribeBulk,
|
||||
) => reply.results,
|
||||
_ => Vec::new(),
|
||||
(Some(mx_command_reply::Payload::SubscribeBulk(reply)), BulkReplyKind::Subscribe) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
(Some(mx_command_reply::Payload::UnsubscribeBulk(reply)), BulkReplyKind::Unsubscribe) => {
|
||||
Ok(reply.results)
|
||||
}
|
||||
_ => Err(Error::MalformedReply {
|
||||
detail: "bulk subscribe reply did not carry the expected payload arm".to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -703,23 +741,28 @@ enum BulkWriteReplyKind {
|
||||
WriteSecured2,
|
||||
}
|
||||
|
||||
fn bulk_write_results(reply: MxCommandReply, kind: BulkWriteReplyKind) -> Vec<BulkWriteResult> {
|
||||
fn bulk_write_results(
|
||||
reply: MxCommandReply,
|
||||
kind: BulkWriteReplyKind,
|
||||
) -> Result<Vec<BulkWriteResult>, Error> {
|
||||
match (reply.payload, kind) {
|
||||
(Some(mx_command_reply::Payload::WriteBulk(reply)), BulkWriteReplyKind::Write) => {
|
||||
reply.results
|
||||
Ok(reply.results)
|
||||
}
|
||||
(Some(mx_command_reply::Payload::Write2Bulk(reply)), BulkWriteReplyKind::Write2) => {
|
||||
reply.results
|
||||
Ok(reply.results)
|
||||
}
|
||||
(
|
||||
Some(mx_command_reply::Payload::WriteSecuredBulk(reply)),
|
||||
BulkWriteReplyKind::WriteSecured,
|
||||
) => reply.results,
|
||||
) => Ok(reply.results),
|
||||
(
|
||||
Some(mx_command_reply::Payload::WriteSecured2Bulk(reply)),
|
||||
BulkWriteReplyKind::WriteSecured2,
|
||||
) => reply.results,
|
||||
_ => Vec::new(),
|
||||
) => Ok(reply.results),
|
||||
_ => Err(Error::MalformedReply {
|
||||
detail: "bulk write reply did not carry the expected payload arm".to_owned(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,16 +20,19 @@ use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::mx_access_gatew
|
||||
use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::mx_command_reply;
|
||||
use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::mx_value::Kind;
|
||||
use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::{
|
||||
AcknowledgeAlarmReply, AcknowledgeAlarmRequest, ActiveAlarmSnapshot, AddItemReply,
|
||||
AlarmFeedMessage, BulkSubscribeReply, CloseSessionReply, CloseSessionRequest, MxCommandKind,
|
||||
MxCommandReply, MxDataType, MxEvent, MxEventFamily, MxStatusCategory, MxStatusProxy,
|
||||
MxStatusSource, MxValue, OpenSessionReply, OpenSessionRequest, ProtocolStatus,
|
||||
ProtocolStatusCode, QueryActiveAlarmsRequest, SessionState, StreamAlarmsRequest,
|
||||
StreamEventsRequest, SubscribeResult,
|
||||
alarm_feed_message, AcknowledgeAlarmReply, AcknowledgeAlarmRequest, ActiveAlarmSnapshot,
|
||||
AddItem2Reply, AddItemReply, AlarmConditionState, AlarmFeedMessage, AlarmTransitionKind,
|
||||
BulkReadReply, BulkReadResult, BulkSubscribeReply, BulkWriteReply, BulkWriteResult,
|
||||
CloseSessionReply, CloseSessionRequest, MxCommandKind, MxCommandReply, MxDataType, MxEvent,
|
||||
MxEventFamily, MxStatusCategory, MxStatusProxy, MxStatusSource, MxValue,
|
||||
OnAlarmTransitionEvent, OpenSessionReply, OpenSessionRequest, ProtocolStatus,
|
||||
ProtocolStatusCode, QueryActiveAlarmsRequest, RegisterReply, SessionState, StreamAlarmsRequest,
|
||||
StreamEventsRequest, SubscribeResult, Write2BulkEntry, WriteBulkEntry, WriteSecured2BulkEntry,
|
||||
WriteSecuredBulkEntry,
|
||||
};
|
||||
use zb_mom_ww_mxgateway_client::{
|
||||
ApiKey, ClientOptions, CommandError, Error, GatewayClient, MxStatus, MxValue as ClientMxValue,
|
||||
MxValueProjection,
|
||||
next_correlation_id, ApiKey, ClientOptions, CommandError, Error, GatewayClient, MxStatus,
|
||||
MxValue as ClientMxValue, MxValueProjection,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
@@ -272,11 +275,414 @@ fn command_error_display_keeps_raw_reply_accessible() {
|
||||
assert!(error.to_string().contains("MxaccessFailure"));
|
||||
}
|
||||
|
||||
// ---- Client.Rust-022 / 024 regression coverage ---------------------------
|
||||
|
||||
#[tokio::test]
|
||||
async fn register_returns_malformed_reply_when_ok_reply_has_no_payload() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithoutPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session.register("client").await.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn add_item_returns_malformed_reply_when_ok_reply_has_no_payload() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithoutPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session.add_item(12, "Plant.Area.Tag").await.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn add_item2_returns_malformed_reply_when_ok_reply_has_no_payload() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithoutPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session
|
||||
.add_item2(12, "Plant.Area.Tag", "ctx")
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_bulk_returns_malformed_reply_on_mismatched_payload_arm() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithMismatchedPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session
|
||||
.subscribe_bulk(12, vec!["Area001.Pump001.Speed".to_owned()])
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_bulk_returns_malformed_reply_on_mismatched_payload_arm() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithMismatchedPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session
|
||||
.read_bulk(12, &["Area001.Pump001.Speed".to_owned()], 1000)
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_bulk_returns_malformed_reply_on_mismatched_payload_arm() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await = Some(InvokeOverride::OkWithMismatchedPayload);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session
|
||||
.write_bulk(
|
||||
12,
|
||||
vec![WriteBulkEntry {
|
||||
item_handle: 34,
|
||||
value: Some(ClientMxValue::int32(1).into_proto()),
|
||||
user_id: 0,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::MalformedReply { .. }),
|
||||
"expected MalformedReply, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unary_invoke_maps_status_unavailable_to_error_unavailable() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.invoke_override.lock().await =
|
||||
Some(InvokeOverride::Unavailable("gateway restarting".to_owned()));
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let error = session.add_item(12, "Plant.Area.Tag").await.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, Error::Unavailable { .. }),
|
||||
"expected Unavailable, got {error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_bulk_round_trips_through_the_fake_gateway() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let results = session
|
||||
.read_bulk(12, &["Area001.Pump001.Speed".to_owned()], 1000)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert!(results[0].was_successful);
|
||||
assert!(results[0].was_cached);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_bulk_round_trips_through_the_fake_gateway() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let results = session
|
||||
.write_bulk(
|
||||
12,
|
||||
vec![WriteBulkEntry {
|
||||
item_handle: 34,
|
||||
value: Some(ClientMxValue::int32(1).into_proto()),
|
||||
user_id: 0,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert!(results[0].was_successful);
|
||||
let last_command = state.last_command_kind.lock().await;
|
||||
assert_eq!(*last_command, Some(MxCommandKind::WriteBulk as i32));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write2_bulk_round_trips_through_the_fake_gateway() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let results = session
|
||||
.write2_bulk(
|
||||
12,
|
||||
vec![Write2BulkEntry {
|
||||
item_handle: 34,
|
||||
value: Some(ClientMxValue::int32(1).into_proto()),
|
||||
timestamp_value: Some(ClientMxValue::string("2026-05-24T00:00:00Z").into_proto()),
|
||||
user_id: 0,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert!(results[0].was_successful);
|
||||
let last_command = state.last_command_kind.lock().await;
|
||||
assert_eq!(*last_command, Some(MxCommandKind::Write2Bulk as i32));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_secured_bulk_round_trips_through_the_fake_gateway() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let results = session
|
||||
.write_secured_bulk(
|
||||
12,
|
||||
vec![WriteSecuredBulkEntry {
|
||||
item_handle: 34,
|
||||
value: Some(ClientMxValue::int32(1).into_proto()),
|
||||
current_user_id: 0,
|
||||
verifier_user_id: 0,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert!(results[0].was_successful);
|
||||
let last_command = state.last_command_kind.lock().await;
|
||||
assert_eq!(*last_command, Some(MxCommandKind::WriteSecuredBulk as i32));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_secured2_bulk_round_trips_through_the_fake_gateway() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
let session = client.session("session-fixture");
|
||||
|
||||
let results = session
|
||||
.write_secured2_bulk(
|
||||
12,
|
||||
vec![WriteSecured2BulkEntry {
|
||||
item_handle: 34,
|
||||
value: Some(ClientMxValue::int32(1).into_proto()),
|
||||
timestamp_value: Some(ClientMxValue::string("2026-05-24T00:00:00Z").into_proto()),
|
||||
current_user_id: 0,
|
||||
verifier_user_id: 0,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert!(results[0].was_successful);
|
||||
let last_command = state.last_command_kind.lock().await;
|
||||
assert_eq!(*last_command, Some(MxCommandKind::WriteSecured2Bulk as i32));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stream_alarms_emits_snapshot_then_complete_then_transition_in_order() {
|
||||
let state = Arc::new(FakeState::default());
|
||||
*state.stream_alarms_script.lock().await = Some(vec![
|
||||
AlarmFeedMessage {
|
||||
payload: Some(alarm_feed_message::Payload::ActiveAlarm(
|
||||
ActiveAlarmSnapshot {
|
||||
alarm_full_reference: "Tank01.Level.HiHi".to_owned(),
|
||||
current_state: AlarmConditionState::Active as i32,
|
||||
..ActiveAlarmSnapshot::default()
|
||||
},
|
||||
)),
|
||||
},
|
||||
AlarmFeedMessage {
|
||||
payload: Some(alarm_feed_message::Payload::SnapshotComplete(true)),
|
||||
},
|
||||
AlarmFeedMessage {
|
||||
payload: Some(alarm_feed_message::Payload::Transition(
|
||||
OnAlarmTransitionEvent {
|
||||
alarm_full_reference: "Tank01.Level.HiHi".to_owned(),
|
||||
transition_kind: AlarmTransitionKind::Raise as i32,
|
||||
..OnAlarmTransitionEvent::default()
|
||||
},
|
||||
)),
|
||||
},
|
||||
]);
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut stream = client
|
||||
.stream_alarms(StreamAlarmsRequest {
|
||||
client_correlation_id: next_correlation_id("test-stream-alarms"),
|
||||
alarm_filter_prefix: String::new(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first = stream.next().await.unwrap().unwrap();
|
||||
let second = stream.next().await.unwrap().unwrap();
|
||||
let third = stream.next().await.unwrap().unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
first.payload,
|
||||
Some(alarm_feed_message::Payload::ActiveAlarm(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
second.payload,
|
||||
Some(alarm_feed_message::Payload::SnapshotComplete(true))
|
||||
));
|
||||
assert!(matches!(
|
||||
third.payload,
|
||||
Some(alarm_feed_message::Payload::Transition(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cli_subcommands_propagate_unique_correlation_ids_from_next_correlation_id() {
|
||||
// The CLI's `stream-alarms` and `acknowledge-alarm` paths used to
|
||||
// hard-code their correlation ids (Client.Rust-023). Verify the
|
||||
// resolution end-to-end through `next_correlation_id`: every call
|
||||
// observed at the fake gateway has a unique id that embeds the
|
||||
// `cli-...` label, so concurrent CLI smokes can tell collisions apart.
|
||||
let state = Arc::new(FakeState::default());
|
||||
let endpoint = spawn_fake_gateway(state.clone()).await;
|
||||
let client = GatewayClient::connect(ClientOptions::new(endpoint))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first_corr = next_correlation_id("cli-stream-alarms");
|
||||
let _ = client
|
||||
.stream_alarms(StreamAlarmsRequest {
|
||||
client_correlation_id: first_corr.clone(),
|
||||
alarm_filter_prefix: String::new(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
*state.last_correlation_id.lock().await,
|
||||
Some(first_corr.clone())
|
||||
);
|
||||
|
||||
let second_corr = next_correlation_id("cli-stream-alarms");
|
||||
assert_ne!(first_corr, second_corr);
|
||||
assert!(second_corr.contains("cli-stream-alarms"));
|
||||
|
||||
let third_corr = next_correlation_id("cli-acknowledge-alarm");
|
||||
let _ = client
|
||||
.acknowledge_alarm(AcknowledgeAlarmRequest {
|
||||
client_correlation_id: third_corr.clone(),
|
||||
alarm_full_reference: "Tank01.Level.HiHi".to_owned(),
|
||||
comment: String::new(),
|
||||
operator_user: String::new(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(*state.last_correlation_id.lock().await, Some(third_corr));
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FakeState {
|
||||
authorization: Mutex<Option<String>>,
|
||||
last_command_kind: Mutex<Option<i32>>,
|
||||
last_correlation_id: Mutex<Option<String>>,
|
||||
stream_dropped: Arc<AtomicBool>,
|
||||
/// Optional per-test override that pins the fake's `Invoke` handler to
|
||||
/// a specific reply shape (or `Err(Status)`). The default of `None`
|
||||
/// keeps the existing happy-path dispatcher.
|
||||
invoke_override: Mutex<Option<InvokeOverride>>,
|
||||
/// Optional per-test override that pins the fake's `StreamAlarms`
|
||||
/// handler to emit a synthetic ConditionRefresh -> snapshot_complete
|
||||
/// -> transition sequence.
|
||||
stream_alarms_script: Mutex<Option<Vec<AlarmFeedMessage>>>,
|
||||
}
|
||||
|
||||
/// Per-test override for the fake's `Invoke` handler.
|
||||
#[allow(dead_code)]
|
||||
enum InvokeOverride {
|
||||
/// Reply with `protocol_status = Ok` and no `payload` set.
|
||||
OkWithoutPayload,
|
||||
/// Reply with `protocol_status = Ok` and a deliberately wrong payload
|
||||
/// arm — e.g. an `AddItemReply` body when the caller invoked a bulk
|
||||
/// command. The variant carries the kind to recognise in tests but the
|
||||
/// reply itself is the mismatched-payload shape.
|
||||
OkWithMismatchedPayload,
|
||||
/// Fail the unary call with `Status::unavailable(...)` so the client's
|
||||
/// `Code::Unavailable` -> `Error::Unavailable` mapping is exercised.
|
||||
Unavailable(String),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -331,6 +737,35 @@ impl MxAccessGateway for FakeGateway {
|
||||
.map(|command| command.kind)
|
||||
.unwrap_or_default();
|
||||
*self.state.last_command_kind.lock().await = Some(kind);
|
||||
*self.state.last_correlation_id.lock().await = Some(request.client_correlation_id.clone());
|
||||
|
||||
// Honour any per-test override before falling through to the
|
||||
// happy-path dispatcher.
|
||||
if let Some(override_) = self.state.invoke_override.lock().await.take() {
|
||||
return match override_ {
|
||||
InvokeOverride::OkWithoutPayload => Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
payload: None,
|
||||
..MxCommandReply::default()
|
||||
})),
|
||||
InvokeOverride::OkWithMismatchedPayload => Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
// Deliberately the wrong payload arm — `AddItemReply`
|
||||
// for whatever command was actually invoked.
|
||||
payload: Some(mx_command_reply::Payload::AddItem(AddItemReply {
|
||||
item_handle: 99,
|
||||
})),
|
||||
..MxCommandReply::default()
|
||||
})),
|
||||
InvokeOverride::Unavailable(message) => Err(Status::unavailable(message)),
|
||||
};
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::Write as i32 {
|
||||
return Ok(Response::new(mxaccess_failure_reply()));
|
||||
@@ -357,6 +792,92 @@ impl MxAccessGateway for FakeGateway {
|
||||
}));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::Register as i32 {
|
||||
return Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
payload: Some(mx_command_reply::Payload::Register(RegisterReply {
|
||||
server_handle: 12,
|
||||
})),
|
||||
..MxCommandReply::default()
|
||||
}));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::AddItem2 as i32 {
|
||||
return Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
payload: Some(mx_command_reply::Payload::AddItem2(AddItem2Reply {
|
||||
item_handle: 56,
|
||||
})),
|
||||
..MxCommandReply::default()
|
||||
}));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::ReadBulk as i32 {
|
||||
return Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
payload: Some(mx_command_reply::Payload::ReadBulk(BulkReadReply {
|
||||
results: vec![BulkReadResult {
|
||||
server_handle: 12,
|
||||
tag_address: "Area001.Pump001.Speed".to_owned(),
|
||||
item_handle: 34,
|
||||
was_successful: true,
|
||||
was_cached: true,
|
||||
..BulkReadResult::default()
|
||||
}],
|
||||
})),
|
||||
..MxCommandReply::default()
|
||||
}));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::WriteBulk as i32 {
|
||||
return Ok(Response::new(write_bulk_reply_for(
|
||||
request.session_id,
|
||||
kind,
|
||||
mx_command_reply::Payload::WriteBulk(BulkWriteReply {
|
||||
results: vec![bulk_write_result_ok(12, 34)],
|
||||
}),
|
||||
)));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::Write2Bulk as i32 {
|
||||
return Ok(Response::new(write_bulk_reply_for(
|
||||
request.session_id,
|
||||
kind,
|
||||
mx_command_reply::Payload::Write2Bulk(BulkWriteReply {
|
||||
results: vec![bulk_write_result_ok(12, 34)],
|
||||
}),
|
||||
)));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::WriteSecuredBulk as i32 {
|
||||
return Ok(Response::new(write_bulk_reply_for(
|
||||
request.session_id,
|
||||
kind,
|
||||
mx_command_reply::Payload::WriteSecuredBulk(BulkWriteReply {
|
||||
results: vec![bulk_write_result_ok(12, 34)],
|
||||
}),
|
||||
)));
|
||||
}
|
||||
|
||||
if kind == MxCommandKind::WriteSecured2Bulk as i32 {
|
||||
return Ok(Response::new(write_bulk_reply_for(
|
||||
request.session_id,
|
||||
kind,
|
||||
mx_command_reply::Payload::WriteSecured2Bulk(BulkWriteReply {
|
||||
results: vec![bulk_write_result_ok(12, 34)],
|
||||
}),
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(Response::new(MxCommandReply {
|
||||
session_id: request.session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
@@ -387,8 +908,10 @@ impl MxAccessGateway for FakeGateway {
|
||||
|
||||
async fn acknowledge_alarm(
|
||||
&self,
|
||||
_request: Request<AcknowledgeAlarmRequest>,
|
||||
request: Request<AcknowledgeAlarmRequest>,
|
||||
) -> Result<Response<AcknowledgeAlarmReply>, Status> {
|
||||
*self.state.last_correlation_id.lock().await =
|
||||
Some(request.into_inner().client_correlation_id);
|
||||
Ok(Response::new(AcknowledgeAlarmReply {
|
||||
correlation_id: "corr-1".to_owned(),
|
||||
protocol_status: Some(ok_status("ack ok")),
|
||||
@@ -407,9 +930,18 @@ impl MxAccessGateway for FakeGateway {
|
||||
|
||||
async fn stream_alarms(
|
||||
&self,
|
||||
_request: Request<StreamAlarmsRequest>,
|
||||
request: Request<StreamAlarmsRequest>,
|
||||
) -> Result<Response<Self::StreamAlarmsStream>, Status> {
|
||||
let (_sender, receiver) = mpsc::channel::<Result<AlarmFeedMessage, Status>>(1);
|
||||
*self.state.last_correlation_id.lock().await =
|
||||
Some(request.into_inner().client_correlation_id);
|
||||
let script = self.state.stream_alarms_script.lock().await.take();
|
||||
let (sender, receiver) =
|
||||
mpsc::channel::<Result<AlarmFeedMessage, Status>>(script.as_ref().map_or(1, Vec::len));
|
||||
if let Some(messages) = script {
|
||||
for message in messages {
|
||||
sender.send(Ok(message)).await.unwrap();
|
||||
}
|
||||
}
|
||||
let stream = ReceiverStream::new(receiver);
|
||||
Ok(Response::new(Box::pin(stream)))
|
||||
}
|
||||
@@ -469,6 +1001,32 @@ async fn spawn_fake_gateway(state: Arc<FakeState>) -> String {
|
||||
format!("http://{address}")
|
||||
}
|
||||
|
||||
fn write_bulk_reply_for(
|
||||
session_id: String,
|
||||
kind: i32,
|
||||
payload: mx_command_reply::Payload,
|
||||
) -> MxCommandReply {
|
||||
MxCommandReply {
|
||||
session_id,
|
||||
correlation_id: "fake-correlation".to_owned(),
|
||||
kind,
|
||||
protocol_status: Some(ok_status("command ok")),
|
||||
payload: Some(payload),
|
||||
..MxCommandReply::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn bulk_write_result_ok(server_handle: i32, item_handle: i32) -> BulkWriteResult {
|
||||
BulkWriteResult {
|
||||
server_handle,
|
||||
item_handle,
|
||||
was_successful: true,
|
||||
hresult: Some(0),
|
||||
statuses: Vec::new(),
|
||||
error_message: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ok_status(message: &str) -> ProtocolStatus {
|
||||
ProtocolStatus {
|
||||
code: ProtocolStatusCode::Ok as i32,
|
||||
|
||||
Reference in New Issue
Block a user