open-standards/eoc/eoc-rs/crates/eoc-streaming/tests/cancel_propagates.rs
Transaction Science 7ef14c2ea9 ingest wave B: 3 crates · op-payouts + smart-byte-nostr + eoc-streaming
OpenPay (1 crate, 2174 LOC, 14 tests)
  op-payouts          Multi-rail payout engine. Visa Direct, Mastercard Send,
                      ACH credits (NACHA), Fedwire + SWIFT MT103 + ISO 20022
                      pacs.008, SEPA SCT + SCT Inst (mod-97 IBAN), UK FPS,
                      FedNow, RTP, PayPal, Wise. Crypto: USDC/USDT on
                      ETH/Polygon/Base/Solana + BTC + Lightning with real
                      ERC-20 transfer(address,uint256) calldata. Offline-pure
                      drivers (emit wire payloads — operators transmit).

Smart Byte (1 crate, 2605 LOC, 38 tests)
  smart-byte-nostr    Nostr adapter. NIP-01 canonical event id + BIP-340
                      Schnorr sign/verify; from-scratch bech32 + NIP-19 TLV
                      (npub/nsec/note/nprofile/nevent/nrelay); NIP-04 legacy
                      AES-256-CBC DMs; NIP-44 v2 HKDF-SHA256→ChaCha20+HMAC
                      with bucketed padding; NIP-17 gift-wrap (kind 1059) +
                      seal (kind 13) + rumor (kind 14); NIP-05/11/13/26/65.
                      Relay client message vocabulary (REQ/EVENT/CLOSE/AUTH/
                      EOSE/OK/NOTICE/CLOSED) + in-memory router.

EOC (1 crate, 2024 LOC, 30 tests)
  eoc-streaming       Token-by-token + tool-call streaming primitives.
                      SSE framing (data: lines); Anthropic event mapper
                      (message_start/content_block_*/message_delta/stop/ping);
                      OpenAI delta mapper; Vercel AI SDK mapper. Bounded-channel
                      backpressure with watermarks; CancelToken propagation;
                      Last-Event-ID resumable streams; per-stream joule meter.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 13:39:09 -04:00

69 lines
1.9 KiB
Rust

//! Cancellation: a client disconnect (modeled as a `CancelToken::cancel`)
//! propagates to upstream awaiters via `select!`.
use std::time::Duration;
use eoc_streaming::cancel::CancelToken;
use eoc_streaming::stream::{Event, Role, Sink, TokenStream};
#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn cancel_wakes_awaiters() {
let tok = CancelToken::new();
let tok2 = tok.clone();
let h = tokio::spawn(async move {
tok2.cancelled().await;
});
tokio::time::sleep(Duration::from_millis(10)).await;
assert!(!h.is_finished());
tok.cancel();
h.await.unwrap();
assert!(tok.is_cancelled());
}
#[tokio::test(flavor = "current_thread")]
async fn cancel_short_circuits_select_against_stream() {
let tok = CancelToken::new();
let mut s = TokenStream::bounded(4);
let sink = s.sink();
// Push one event then sit idle.
sink.send(Event::MessageStart {
id: None,
role: Role::Assistant,
})
.await
.unwrap();
// Cancel after a short delay.
let tok2 = tok.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(20)).await;
tok2.cancel();
});
// Consume the queued event, then a second `recv` should be
// cancelled rather than blocking forever.
let first = tokio::select! {
ev = s.recv() => ev,
_ = tok.cancelled() => panic!("cancel fired before first event"),
};
assert!(matches!(first, Some(Event::MessageStart { .. })));
let outcome = tokio::select! {
_ = s.recv() => "got_event",
_ = tok.cancelled() => "cancelled",
};
assert_eq!(outcome, "cancelled");
}
#[tokio::test]
async fn cancel_is_observable_from_clones() {
let tok = CancelToken::new();
let a = tok.clone();
let b = tok.clone();
assert!(!a.is_cancelled());
assert!(!b.is_cancelled());
tok.cancel();
assert!(a.is_cancelled());
assert!(b.is_cancelled());
}