open-net 0.3.5

A cross-platform async networking library for Rust with HTTP and WebSocket clients, network status monitoring, configurable retries, WebSocket reconnection and heartbeats, and managed client lifecycles. Reuse common networking infrastructure across applications to avoid repeated implementation and debugging, helping save development time and AI coding tokens.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use super::*;
use crate::error::TryReceiveError;
use crate::module::transport::failure::ConnectStage;
use crate::subscription::CallbackExecutor;
use futures::task::noop_waker_ref;
use std::error::Error;
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll, Wake, Waker};
use std::time::Duration;

type TestResult<T = ()> = std::result::Result<T, crate::BoxError>;

fn check(condition: bool, message: &str) -> TestResult {
    if condition {
        Ok(())
    } else {
        Err(std::io::Error::other(message).into())
    }
}

fn ready<F: Future>(future: F) -> TestResult<F::Output> {
    let mut future = Box::pin(future);
    let mut cx = Context::from_waker(noop_waker_ref());
    match future.as_mut().poll(&mut cx) {
        Poll::Ready(value) => Ok(value),
        Poll::Pending => Err("operation unexpectedly required consumer progress".into()),
    }
}

#[derive(Default)]
struct CountWake(AtomicUsize);
impl Wake for CountWake {
    fn wake(self: Arc<Self>) {
        self.wake_by_ref();
    }
    fn wake_by_ref(self: &Arc<Self>) {
        self.0.fetch_add(1, Ordering::SeqCst);
    }
}

struct InlineExecutor;
impl CallbackExecutor for InlineExecutor {
    fn ensure_ready(&self) -> crate::Result<()> {
        Ok(())
    }
    fn submit(&self, job: Box<dyn FnOnce() + Send>) -> crate::Result<()> {
        job();
        Ok(())
    }
}

fn minimum_journal() -> JournalOptions {
    JournalOptions {
        max_events: 4,
        max_bytes: 4 * MAX_CONNECTION_EVENT_BYTES,
    }
}

fn fixture(
    journal: Option<JournalOptions>,
    history_items: usize,
    event_slots: usize,
) -> crate::Result<(
    Arc<ConnectionSession>,
    Option<EventReceiver<ConnectionEvent>>,
)> {
    fixture_with_executor(
        journal,
        history_items,
        event_slots,
        Arc::new(InlineExecutor),
    )
}

fn fixture_with_executor(
    journal: Option<JournalOptions>,
    history_items: usize,
    event_slots: usize,
    executor: Arc<dyn CallbackExecutor>,
) -> crate::Result<(
    Arc<ConnectionSession>,
    Option<EventReceiver<ConnectionEvent>>,
)> {
    ConnectionSession::new_with_observers(
        11,
        21,
        journal,
        Arc::new(crate::Metadata::new()),
        EventOptions {
            max_events: history_items,
            max_bytes: 16 * MAX_CONNECTION_EVENT_BYTES,
        },
        ConnectionObservers {
            state_executor: executor.clone(),
            state_quota: Arc::new(Semaphore::new(8)),
            event_executor: executor,
            event_quota: Arc::new(Semaphore::new(event_slots)),
        },
    )
}

fn fail_attempt(session: &ConnectionSession, attempt: u64) -> TestResult {
    session.begin_attempt(
        session.handshake_attempt(41, attempt),
        ready(session.reserve_attempt())??,
    )?;
    session.attempt_failed(
        41,
        attempt,
        ConnectionFailure::new(NetError::from(ErrorKind::Io), ConnectStage::Tcp, None, true),
        RetryDecision::Scheduled {
            after: Duration::ZERO,
        },
    )?;
    Ok(())
}

fn establish_reserved(
    session: &ConnectionSession,
    attempt: u64,
    reservation: AttemptReservation,
) -> TestResult {
    session.begin_attempt(session.handshake_attempt(41, attempt), reservation)?;
    session.set_attempt_credential_version(41, attempt, None)?;
    session.prepare_established(41, attempt)?;
    session.commit_established(41, attempt)?;
    Ok(())
}

#[test]
fn absent_journal_never_applies_backpressure_and_history_stays_bounded() -> TestResult {
    let (session, journal) = fixture(None, 2, 8)?;
    check(journal.is_none(), "None created a journal subscription")?;
    session.begin_cycle(41, false)?;
    for attempt in 0..8 {
        fail_attempt(&session, attempt)?;
    }
    check(
        !session.cancel_token().is_cancelled(),
        "unobserved retries cancelled the session",
    )?;
    session.terminate(TerminationReason::LocalClose, None)?;
    check(
        ready(session.closed())??.reason == TerminationReason::LocalClose,
        "closed result required a journal receiver",
    )?;
    let mut history = session.subscribe_events()?;
    check(
        matches!(
            history.try_recv(),
            Err(TryReceiveError::Lagged { skipped: 15 })
        ),
        "history failed to bound the first fifteen facts",
    )?;
    check(
        history.try_recv()?.sequence == 16,
        "retained failure missing",
    )?;
    let closed = history.try_recv()?;
    check(
        closed.sequence == 17
            && matches!(closed.kind, ConnectionEventKind::Closed { result: Ok(_) }),
        "retained final fact missing",
    )?;
    check(
        matches!(history.try_recv(), Err(TryReceiveError::Closed)),
        "finished history was not fused",
    )
}

#[test]
fn full_minimum_journal_unsubscribe_releases_reservation_and_keeps_history_continuous() -> TestResult
{
    let (session, journal) = fixture(Some(minimum_journal()), 8, 8)?;
    let journal = journal.ok_or("journal missing")?;
    session.begin_cycle(41, false)?;
    fail_attempt(&session, 0)?;
    let mut waiting = Box::pin(session.reserve_attempt());
    let wakes = Arc::new(CountWake::default());
    let waker = Waker::from(wakes.clone());
    let mut cx = Context::from_waker(&waker);
    check(
        waiting.as_mut().poll(&mut cx).is_pending(),
        "minimum journal did not retain reserved event capacity",
    )?;
    check(journal.unsubscribe(), "initial detach was rejected")?;
    check(!journal.unsubscribe(), "detach was not idempotent")?;
    check(
        wakes.0.load(Ordering::SeqCst) > 0,
        "detaching journal did not wake reservation",
    )?;
    let reservation = match waiting.as_mut().poll(&mut cx) {
        Poll::Ready(result) => result?,
        Poll::Pending => return Err("detached journal continued to block reservation".into()),
    };
    check(
        !session.cancel_token().is_cancelled(),
        "journal unsubscribe cancelled its session",
    )?;
    establish_reserved(&session, 1, reservation)?;
    check(
        ready(session.wait_connected())??.attempt_id.as_u64() == 1,
        "session could not connect after journal detach",
    )?;
    session.terminate(TerminationReason::LocalClose, None)?;
    check(
        ready(session.closed())?.is_ok(),
        "detached session lost closed result",
    )?;
    let mut history = session.subscribe_events()?;
    for sequence in 1..=6 {
        check(
            history.try_recv()?.sequence == sequence,
            "journal detach interrupted ordinary history sequence",
        )?;
    }
    check(
        matches!(history.try_recv(), Err(TryReceiveError::Closed)),
        "history did not end after the six lifecycle facts",
    )
}

#[test]
fn detaching_during_the_byte_wait_releases_the_partial_reservation() -> TestResult {
    let (session, journal) = fixture(
        Some(JournalOptions {
            max_events: 16,
            max_bytes: 4 * MAX_CONNECTION_EVENT_BYTES,
        }),
        8,
        8,
    )?;
    let journal = journal.ok_or("journal missing")?;
    session.begin_cycle(41, false)?;
    fail_attempt(&session, 0)?;
    // Two queued facts and the Closed reservation leave thirteen event slots.
    // Their nonzero bytes prevent reserving three full maximum-size events.
    let mut waiting = Box::pin(session.reserve_attempt());
    let wakes = Arc::new(CountWake::default());
    let waker = Waker::from(wakes.clone());
    let mut cx = Context::from_waker(&waker);
    check(
        waiting.as_mut().poll(&mut cx).is_pending(),
        "byte limit did not block while event slots were available",
    )?;
    drop(journal);
    check(
        wakes.0.load(Ordering::SeqCst) > 0,
        "journal Drop did not wake the byte reservation",
    )?;
    let reservation = match waiting.as_mut().poll(&mut cx) {
        Poll::Ready(result) => result?,
        Poll::Pending => return Err("byte reservation remained blocked after detach".into()),
    };
    check(
        !session.cancel_token().is_cancelled(),
        "byte reservation detach became SessionCancelled",
    )?;
    drop(reservation);
    for _ in 0..8 {
        drop(ready(session.reserve_attempt())??);
    }
    fail_attempt(&session, 1)?;
    session.terminate(TerminationReason::LocalClose, None)?;
    check(
        ready(session.closed())?.is_ok(),
        "byte detach prevented terminal completion",
    )
}

#[test]
fn unobserved_reservations_still_reject_another_session_scope() -> TestResult {
    for detached in [false, true] {
        let options = detached.then(minimum_journal);
        let (first, first_journal) = fixture(options.clone(), 8, 8)?;
        let (second, second_journal) = fixture(options, 8, 8)?;
        // Equal numeric identities must not make separately owned reservations interchangeable.
        drop((first_journal, second_journal));
        first.begin_cycle(41, false)?;
        second.begin_cycle(41, false)?;
        let foreign = ready(first.reserve_attempt())??;
        let error = second
            .begin_attempt(second.handshake_attempt(41, 0), foreign)
            .err()
            .ok_or("foreign unobserved reservation was accepted")?;
        check(
            error.kind() == ErrorKind::InvalidConfig,
            "foreign reservation did not retain its ownership error",
        )?;
        fail_attempt(&first, 0)?;
        fail_attempt(&second, 0)?;
    }
    Ok(())
}

#[test]
fn unobserved_success_commit_cannot_be_replayed() -> TestResult {
    for detached in [false, true] {
        let (session, journal) = fixture(detached.then(minimum_journal), 8, 8)?;
        drop(journal);
        session.begin_cycle(41, false)?;
        establish_reserved(&session, 0, ready(session.reserve_attempt())??)?;
        let established = ready(session.wait_connected())??;
        check(
            session.commit_established(41, 0).is_err(),
            "duplicate success commit was accepted without a journal lease",
        )?;
        check(
            ready(session.wait_connected())??.connection_id == established.connection_id,
            "rejected duplicate changed the connected identity",
        )?;
        session.terminate(TerminationReason::LocalClose, None)?;
        let mut history = session.subscribe_events()?;
        for sequence in 1..=4 {
            check(
                history.try_recv()?.sequence == sequence,
                "duplicate commit altered the lifecycle sequence",
            )?;
        }
        check(
            matches!(history.try_recv(), Err(TryReceiveError::Closed)),
            "duplicate commit appended an extra fact",
        )?;
    }
    Ok(())
}

#[test]
fn detached_journal_preserves_terminal_error_source_and_last_error() -> TestResult {
    let (session, journal) = fixture(Some(minimum_journal()), 8, 8)?;
    drop(journal);
    session.begin_cycle(41, false)?;
    establish_reserved(&session, 0, ready(session.reserve_attempt())??)?;
    let physical = NetError::from(std::io::Error::new(
        std::io::ErrorKind::ConnectionReset,
        "retained transport failure",
    ));
    session.terminate(
        TerminationReason::IoFailure,
        Some(ConnectionFailure::new(
            physical.clone(),
            ConnectStage::WebSocketIo,
            None,
            false,
        )),
    )?;
    let terminal = ready(session.closed())?
        .err()
        .ok_or("detached journal lost the business failure")?;
    let last = session.snapshot()?.last_error.ok_or("last_error missing")?;
    for error in [&terminal, &last] {
        check(
            error.kind() == ErrorKind::Io,
            "error kind changed after detach",
        )?;
        check(
            std::ptr::eq(
                error.source().ok_or("observed source missing")?,
                physical.source().ok_or("physical source missing")?,
            ),
            "journal detach changed the retained error source",
        )?;
        check(
            error.context().connection_id.is_some(),
            "terminal connection identity missing",
        )?;
    }
    let mut history = session.subscribe_events()?;
    for _ in 0..3 {
        history.try_recv()?;
    }
    check(
        matches!(history.try_recv()?.kind, ConnectionEventKind::Closed { result: Err(ref error) } if error.kind() == ErrorKind::Io),
        "ordinary history lost detached terminal error",
    )
}

#[test]
fn native_journal_uses_one_event_subscription_slot_and_detach_returns_it() -> TestResult {
    let (session, journal) = fixture(Some(minimum_journal()), 8, 1)?;
    let journal = journal.ok_or("journal missing")?;
    check(
        session.subscribe_events().err().map(|error| error.kind())
            == Some(ErrorKind::SubscriptionLimitReached),
        "journal did not occupy exactly one shared event subscription slot",
    )?;
    journal.unsubscribe();
    let ordinary = session.subscribe_events()?;
    check(
        !session.cancel_token().is_cancelled(),
        "releasing a subscription slot cancelled the session",
    )?;
    drop(ordinary);
    let (unobserved, absent) = fixture(None, 8, 1)?;
    check(absent.is_none(), "journal None created a receiver")?;
    let _ordinary = unobserved.subscribe_events()?;
    check(
        unobserved
            .subscribe_events()
            .err()
            .map(|error| error.kind())
            == Some(ErrorKind::SubscriptionLimitReached),
        "absent journal consumed an event subscription slot",
    )
}

#[test]
fn journal_callback_dispatch_failure_only_terminates_its_subscription() -> TestResult {
    struct RejectDispatch(NetError);
    impl CallbackExecutor for RejectDispatch {
        fn ensure_ready(&self) -> crate::Result<()> {
            Ok(())
        }
        fn submit(&self, _job: Box<dyn FnOnce() + Send>) -> crate::Result<()> {
            Err(self.0.clone())
        }
    }

    let dispatch_error = NetError::with_source(
        ErrorKind::RuntimeUnavailable,
        std::io::Error::other("journal executor rejected the accepted lifecycle fact"),
    )
    .with_stage(ErrorStage::Dispatch);
    let (session, journal) = fixture_with_executor(
        Some(minimum_journal()),
        8,
        8,
        Arc::new(RejectDispatch(dispatch_error.clone())),
    )?;
    let calls = Arc::new(AtomicUsize::new(0));
    let callback_calls = calls.clone();
    let subscription = journal
        .ok_or("journal missing")?
        .into_callback(move |_, _| {
            callback_calls.fetch_add(1, Ordering::SeqCst);
        })?;
    check(
        subscription.is_active(),
        "empty callback conversion was rejected",
    )?;
    session.begin_cycle(41, false)?;
    session.begin_attempt(
        session.handshake_attempt(41, 0),
        ready(session.reserve_attempt())??,
    )?;
    check(
        !subscription.is_active() && calls.load(Ordering::SeqCst) == 0,
        "rejected dispatch did not terminate only the journal callback",
    )?;
    let failure = ready(subscription.close())?
        .err()
        .ok_or("subscription close lost the dispatch failure")?;
    check(
        failure.kind() == ErrorKind::RuntimeUnavailable
            && failure.context().stage == Some(ErrorStage::Dispatch),
        "dispatch failure classification changed",
    )?;
    check(
        std::ptr::eq(
            failure
                .source()
                .ok_or("subscription failure source missing")?,
            dispatch_error
                .source()
                .ok_or("original failure source missing")?,
        ),
        "subscription did not retain the dispatch source",
    )?;
    check(
        !session.cancel_token().is_cancelled(),
        "callback executor failure cancelled the session",
    )?;
    drop(ready(session.reserve_attempt())??);
    session.attempt_failed(
        41,
        0,
        ConnectionFailure::new(NetError::from(ErrorKind::Io), ConnectStage::Tcp, None, true),
        RetryDecision::Scheduled {
            after: Duration::ZERO,
        },
    )?;
    fail_attempt(&session, 1)?;
    session.terminate(TerminationReason::LocalClose, None)?;
    check(
        ready(session.closed())??.reason == TerminationReason::LocalClose,
        "journal dispatch failure replaced the business result",
    )?;
    let mut history = session.subscribe_events()?;
    for sequence in 1..=5 {
        check(
            history.try_recv()?.sequence == sequence,
            "journal dispatch failure interrupted ordinary history",
        )?;
    }
    check(
        matches!(history.try_recv(), Err(TryReceiveError::Closed)),
        "history did not end after isolated dispatch failure",
    )
}