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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use super::*;
use crate::subscription::event::{event_channel, EventPublisher, EventReceiver, EventSeed};
use futures::{executor::block_on, task::noop_waker_ref};
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::Wake;

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

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

#[derive(Default)]
struct ManualExecutor {
    jobs: Mutex<VecDeque<Box<dyn FnOnce() + Send>>>,
}
impl CallbackExecutor for ManualExecutor {
    fn ensure_ready(&self) -> Result<()> {
        Ok(())
    }
    fn submit(&self, job: Box<dyn FnOnce() + Send>) -> Result<()> {
        self.jobs
            .lock()
            .map_err(NetError::from_poison)?
            .push_back(job);
        Ok(())
    }
}
impl ManualExecutor {
    fn drain(&self) -> TestResult {
        loop {
            let job = self.jobs.lock().map_err(|_| "executor lock")?.pop_front();
            match job {
                Some(job) => job(),
                None => return Ok(()),
            }
        }
    }
    fn len(&self) -> TestResult<usize> {
        Ok(self.jobs.lock().map_err(|_| "executor lock")?.len())
    }
}
fn channel<T>(
    items: usize,
    bytes: usize,
    policy: EventOverflow,
) -> TestResult<(EventPublisher<T>, EventReceiver<T>, Arc<ManualExecutor>)> {
    let executor = Arc::new(ManualExecutor::default());
    let (publisher, receiver) = event_channel(
        EventQueueLimit {
            max_items: items,
            max_bytes: bytes,
        },
        policy,
        executor.clone(),
        None,
    )?;
    Ok((publisher, receiver, executor))
}
fn seed<T>(
    entries: impl IntoIterator<Item = (T, usize)>,
    lagged: u64,
    closed: bool,
    failure: Option<NetError>,
) -> EventSeed<T> {
    EventSeed {
        entries: entries.into_iter().collect(),
        lagged,
        source_closed: closed,
        failure,
    }
}

#[test]
fn reversed_dispatch_preserves_committed_order_and_defers_callback_submission() -> TestResult {
    let (publisher, receiver, executor) = channel(3, 3, EventOverflow::DropOldest)?;
    let seen = Arc::new(Mutex::new(Vec::new()));
    let capture = seen.clone();
    let subscription = receiver.into_callback(move |_, value| {
        if let (Ok(value), Ok(mut seen)) = (value, capture.lock()) {
            seen.push(value);
        }
    })?;
    let outer = Mutex::new(());
    let guard = outer.lock().map_err(|_| "outer lock")?;
    let first = publisher.prepare_try_publish(1_u8, 1);
    let second = publisher.prepare_try_publish(2, 1);
    let closed = publisher.prepare_finish();
    first.result()?;
    second.result()?;
    closed.result()?;
    check(
        executor.len()? == 0,
        "prepare submitted a callback under the outer lock",
    )?;
    drop(guard);
    closed.dispatch()?;
    second.dispatch()?;
    first.dispatch()?;
    check(
        executor.len()? == 1,
        "reversed dispatch duplicated the runner",
    )?;
    executor.drain()?;
    check(
        *seen.lock().map_err(|_| "seen lock")? == vec![1, 2],
        "dispatch reordered facts",
    )?;
    block_on(subscription.close())?;
    check(
        !publisher.is_active(),
        "closed callback registration stayed active",
    )
}

struct DropProbe {
    outer: Arc<Mutex<()>>,
    registration: Weak<EventRegistration<DropProbe>>,
    drops: Arc<AtomicUsize>,
    blocked: Arc<AtomicUsize>,
}
impl Drop for DropProbe {
    fn drop(&mut self) {
        if self.outer.try_lock().is_err() {
            self.blocked.fetch_add(1, Ordering::SeqCst);
        }
        if self
            .registration
            .upgrade()
            .is_some_and(|registration| registration.state.try_lock().is_err())
        {
            self.blocked.fetch_add(1, Ordering::SeqCst);
        }
        self.drops.fetch_add(1, Ordering::SeqCst);
    }
}
fn probe(
    receiver: &EventReceiver<DropProbe>,
    outer: &Arc<Mutex<()>>,
    drops: &Arc<AtomicUsize>,
    blocked: &Arc<AtomicUsize>,
) -> DropProbe {
    DropProbe {
        outer: outer.clone(),
        registration: Arc::downgrade(&receiver.registration),
        drops: drops.clone(),
        blocked: blocked.clone(),
    }
}

#[test]
fn prepare_retains_evicted_rejected_and_failed_reservation_values_until_dispatch() -> TestResult {
    let (publisher, receiver, _) = channel(1, 1, EventOverflow::DropOldest)?;
    let outer = Arc::new(Mutex::new(()));
    let drops = Arc::new(AtomicUsize::new(0));
    let blocked = Arc::new(AtomicUsize::new(0));
    publisher.try_publish(probe(&receiver, &outer, &drops, &blocked), 1)?;
    let guard = outer.lock().map_err(|_| "outer lock")?;
    let replacement = publisher.prepare_try_publish(probe(&receiver, &outer, &drops, &blocked), 1);
    replacement.result()?;
    let oversized = publisher.prepare_try_publish(probe(&receiver, &outer, &drops, &blocked), 2);
    check(
        matches!(oversized.result(), Err(error) if error.kind() == ErrorKind::ItemTooLarge),
        "oversized input accepted",
    )?;
    lock(&receiver.registration.state).fail_retirement_reserve = true;
    let failed = publisher.prepare_try_publish(probe(&receiver, &outer, &drops, &blocked), 1);
    check(
        matches!(failed.result(), Err(error) if error.kind() == ErrorKind::ResourceExhausted),
        "reservation injection was ignored",
    )?;
    check(
        drops.load(Ordering::SeqCst) == 0,
        "prepare retired user input",
    )?;
    drop(guard);
    replacement.dispatch()?;
    check(
        oversized.dispatch().is_err(),
        "dispatch lost admission error",
    )?;
    check(
        failed.dispatch().is_err(),
        "dispatch lost allocation failure",
    )?;
    check(
        drops.load(Ordering::SeqCst) == 3,
        "publication retained retired inputs",
    )?;
    check(receiver.unsubscribe(), "receiver did not unsubscribe")?;
    check(
        drops.load(Ordering::SeqCst) == 4,
        "failed prepare changed queued ownership",
    )?;
    check(
        blocked.load(Ordering::SeqCst) == 0,
        "user destructor ran under a lock",
    )
}

#[test]
fn atomic_seed_delivers_gap_then_entries_then_failure_then_eof() -> TestResult {
    let (publisher, mut receiver, _) = channel(3, 5, EventOverflow::DropOldest)?;
    let publication = publisher.prepare_seed(seed(
        [(7_u8, 2), (8, 3)],
        6,
        true,
        Some(NetError::from(ErrorKind::Io)),
    ));
    publication.result()?;
    publication.dispatch()?;
    check(
        matches!(
            receiver.try_recv(),
            Err(TryReceiveError::Lagged { skipped: 6 })
        ),
        "seed gap missing",
    )?;
    check(
        receiver.try_recv()? == 7 && receiver.try_recv()? == 8,
        "seed entries reordered",
    )?;
    check(
        matches!(receiver.try_recv(), Err(TryReceiveError::Failed(error)) if error.kind() == ErrorKind::Io),
        "seed terminal failure missing",
    )?;
    check(
        matches!(receiver.try_recv(), Err(TryReceiveError::Closed)),
        "seed terminal EOF missing",
    )?;
    check(
        !publisher.is_active(),
        "seed EOF did not retire registration",
    )
}

#[test]
fn seed_rejects_receivers_after_empty_poll_drain_or_prior_empty_seed() -> TestResult {
    let (publisher, mut receiver, _) = channel::<u8>(2, 2, EventOverflow::DropOldest)?;
    check(
        matches!(receiver.try_recv(), Err(TryReceiveError::Empty)),
        "fresh queue was not empty",
    )?;
    check(
        matches!(publisher.prepare_seed(seed([(1, 1)], 0, false, None)).dispatch(), Err(error) if error.kind() == ErrorKind::InvalidInput),
        "empty receive allowed seed reset",
    )?;
    publisher.try_publish(2, 1)?;
    check(receiver.try_recv()? == 2, "rejected seed changed queue")?;
    check(
        publisher
            .prepare_seed(seed([(3, 1)], 0, false, None))
            .dispatch()
            .is_err(),
        "drained receiver allowed seed reset",
    )?;
    let (fresh, _receiver, _) = channel::<u8>(1, 1, EventOverflow::DropOldest)?;
    fresh.prepare_seed(seed([], 0, false, None)).dispatch()?;
    check(
        matches!(fresh.prepare_seed(seed([], 0, false, None)).dispatch(), Err(error) if error.kind() == ErrorKind::InvalidInput),
        "empty seed was reusable",
    )
}

#[test]
fn skip_clears_pending_prefix_counts_exact_loss_and_preserves_capacity() -> TestResult {
    let (publisher, mut receiver, _) = channel(2, 2, EventOverflow::DropOldest)?;
    publisher
        .prepare_seed(seed([(1_u8, 1), (2, 1)], 3, false, None))
        .dispatch()?;
    let capacity = lock(&receiver.registration.state).queue.capacity();
    publisher.prepare_skip(2).dispatch()?;
    check(
        lock(&receiver.registration.state).queue.capacity() == capacity,
        "skip discarded reserved queue capacity",
    )?;
    publisher.try_publish(5, 2)?;
    check(
        matches!(
            receiver.try_recv(),
            Err(TryReceiveError::Lagged { skipped: 7 })
        ),
        "skip did not count old gap, queue and explicit loss",
    )?;
    check(receiver.try_recv()? == 5, "post-gap event missing")?;
    check(
        matches!(receiver.try_recv(), Err(TryReceiveError::Empty)),
        "skipped prefix survived",
    )
}

#[test]
fn skip_rejects_other_policies_and_count_or_allocation_failures_are_transactional() -> TestResult {
    for policy in [EventOverflow::Wait, EventOverflow::Disconnect] {
        let (publisher, mut receiver, _) = channel(1, 1, policy)?;
        publisher.try_publish(9_u8, 1)?;
        check(
            matches!(publisher.prepare_skip(1).dispatch(), Err(error) if error.kind() == ErrorKind::InvalidInput),
            "skip accepted a non-lossy policy",
        )?;
        check(
            receiver.try_recv()? == 9,
            "rejected skip changed reliable queue",
        )?;
    }
    let (publisher, mut receiver, _) = channel(1, 1, EventOverflow::DropOldest)?;
    publisher
        .prepare_seed(seed([(8_u8, 1)], u64::MAX, false, None))
        .dispatch()?;
    check(
        matches!(publisher.prepare_skip(1).dispatch(), Err(error) if error.kind() == ErrorKind::ResourceExhausted),
        "lag count wrapped",
    )?;
    check(
        matches!(
            receiver.try_recv(),
            Err(TryReceiveError::Lagged { skipped: u64::MAX })
        ),
        "count failure changed previous lag",
    )?;
    lock(&receiver.registration.state).fail_retirement_reserve = true;
    check(
        matches!(publisher.prepare_skip(1).dispatch(), Err(error) if error.kind() == ErrorKind::ResourceExhausted),
        "skip ignored allocation failure",
    )?;
    check(
        receiver.try_recv()? == 8,
        "failed skip evicted original queue",
    )
}

struct WakeProbe {
    outer: Arc<Mutex<()>>,
    calls: AtomicUsize,
    blocked: AtomicUsize,
}
impl Wake for WakeProbe {
    fn wake(self: Arc<Self>) {
        self.wake_by_ref();
    }
    fn wake_by_ref(self: &Arc<Self>) {
        if self.outer.try_lock().is_err() {
            self.blocked.fetch_add(1, Ordering::SeqCst);
        }
        self.calls.fetch_add(1, Ordering::SeqCst);
    }
}
#[test]
fn prepare_finish_defers_receive_wake_until_outer_lock_is_released() -> TestResult {
    let (publisher, mut receiver, _) = channel::<u8>(1, 1, EventOverflow::Wait)?;
    let outer = Arc::new(Mutex::new(()));
    let wake = Arc::new(WakeProbe {
        outer: outer.clone(),
        calls: AtomicUsize::new(0),
        blocked: AtomicUsize::new(0),
    });
    let waker = Waker::from(wake.clone());
    let mut cx = Context::from_waker(&waker);
    let mut receiving = Box::pin(receiver.recv());
    check(
        receiving.as_mut().poll(&mut cx).is_pending(),
        "empty receiver completed",
    )?;
    let guard = outer.lock().map_err(|_| "outer lock")?;
    let publication = publisher.prepare_finish();
    check(
        wake.calls.load(Ordering::SeqCst) == 0,
        "prepare woke receiver",
    )?;
    drop(guard);
    publication.dispatch()?;
    check(
        wake.calls.load(Ordering::SeqCst) == 1 && wake.blocked.load(Ordering::SeqCst) == 0,
        "receive wake held outer lock",
    )?;
    check(
        matches!(receiving.as_mut().poll(&mut cx), Poll::Ready(Ok(None))),
        "finish did not close pending receive",
    )
}

#[test]
fn prepared_wait_rejection_keeps_existing_async_capacity_and_cancel_safety() -> TestResult {
    let (publisher, mut receiver, _) = channel(1, 1, EventOverflow::Wait)?;
    publisher.prepare_try_publish(1_u8, 1).dispatch()?;
    check(
        matches!(publisher.prepare_try_publish(2, 1).dispatch(), Err(error) if error.kind() == ErrorKind::QueueFull),
        "prepared Wait rejection changed classification",
    )?;
    let mut cx = Context::from_waker(noop_waker_ref());
    let mut pending = Box::pin(publisher.publish(3, 1));
    check(
        pending.as_mut().poll(&mut cx).is_pending(),
        "full async publisher did not wait",
    )?;
    drop(pending);
    check(
        receiver.try_recv()? == 1,
        "cancelled publisher overwrote queued item",
    )?;
    publisher.prepare_try_publish(4, 1).dispatch()?;
    check(
        receiver.try_recv()? == 4,
        "cancelled publisher stranded capacity",
    )
}

#[test]
fn failed_seed_retains_every_input_until_dispatch_and_does_not_consume_freshness() -> TestResult {
    let (publisher, receiver, _) = channel(2, 1, EventOverflow::DropOldest)?;
    let outer = Arc::new(Mutex::new(()));
    let drops = Arc::new(AtomicUsize::new(0));
    let blocked = Arc::new(AtomicUsize::new(0));
    let guard = outer.lock().map_err(|_| "outer lock")?;
    let publication = publisher.prepare_seed(seed(
        [
            (probe(&receiver, &outer, &drops, &blocked), 1),
            (probe(&receiver, &outer, &drops, &blocked), 1),
        ],
        4,
        false,
        None,
    ));
    check(
        matches!(publication.result(), Err(error) if error.kind() == ErrorKind::QueueFull),
        "aggregate seed byte overflow accepted",
    )?;
    check(
        drops.load(Ordering::SeqCst) == 0,
        "rejected seed dropped inputs under outer lock",
    )?;
    drop(guard);
    check(publication.dispatch().is_err(), "seed admission error lost")?;
    check(
        drops.load(Ordering::SeqCst) == 2 && blocked.load(Ordering::SeqCst) == 0,
        "seed cleanup held a lock or retained input",
    )?;
    publisher
        .prepare_seed(seed(
            [(probe(&receiver, &outer, &drops, &blocked), 1)],
            0,
            false,
            None,
        ))
        .dispatch()?;
    check(
        receiver.unsubscribe(),
        "seeded receiver did not unsubscribe",
    )?;
    check(
        drops.load(Ordering::SeqCst) == 3,
        "valid seed did not own its event",
    )
}

#[derive(Debug)]
struct ErrorDropProbe {
    outer: Arc<Mutex<()>>,
    registration: Weak<EventRegistration<u8>>,
    drops: Arc<AtomicUsize>,
    blocked: Arc<AtomicUsize>,
}
impl fmt::Display for ErrorDropProbe {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("error drop probe")
    }
}
impl std::error::Error for ErrorDropProbe {}
impl Drop for ErrorDropProbe {
    fn drop(&mut self) {
        if self.outer.try_lock().is_err() {
            self.blocked.fetch_add(1, Ordering::SeqCst);
        }
        if self
            .registration
            .upgrade()
            .is_some_and(|registration| registration.state.try_lock().is_err())
        {
            self.blocked.fetch_add(1, Ordering::SeqCst);
        }
        self.drops.fetch_add(1, Ordering::SeqCst);
    }
}

#[test]
fn redundant_failure_keeps_last_user_error_source_until_dispatch() -> TestResult {
    let (publisher, _receiver, _) = channel::<u8>(1, 1, EventOverflow::DropOldest)?;
    publisher.prepare_finish().dispatch()?;
    let outer = Arc::new(Mutex::new(()));
    let drops = Arc::new(AtomicUsize::new(0));
    let blocked = Arc::new(AtomicUsize::new(0));
    let source = ErrorDropProbe {
        outer: outer.clone(),
        registration: Arc::downgrade(&_receiver.registration),
        drops: drops.clone(),
        blocked: blocked.clone(),
    };
    let guard = outer.lock().map_err(|_| "outer lock")?;
    let publication = publisher.prepare_fail(NetError::with_source(ErrorKind::Io, source));
    publication.result()?;
    check(
        drops.load(Ordering::SeqCst) == 0,
        "redundant failure released source in prepare",
    )?;
    drop(guard);
    publication.dispatch()?;
    check(
        drops.load(Ordering::SeqCst) == 1 && blocked.load(Ordering::SeqCst) == 0,
        "failure source was retained or destroyed under a lock",
    )
}

#[test]
fn seed_validation_and_callback_conversion_preserve_the_initial_cursor() -> TestResult {
    for (entries, closed, failure, expected) in [
        (vec![(1_u8, 1), (2, 1)], false, None, ErrorKind::QueueFull),
        (vec![(1, 3)], false, None, ErrorKind::ItemTooLarge),
        (
            vec![],
            false,
            Some(NetError::from(ErrorKind::Io)),
            ErrorKind::InvalidInput,
        ),
    ] {
        let (publisher, _receiver, _) = channel(1, 2, EventOverflow::DropOldest)?;
        check(
            matches!(publisher.prepare_seed(seed(entries, 0, closed, failure)).dispatch(), Err(error) if error.kind() == expected),
            "seed validation classification changed",
        )?;
    }
    let (publisher, receiver, executor) = channel(2, 2, EventOverflow::DropOldest)?;
    let original_id = receiver.id();
    publisher
        .prepare_seed(seed(
            [(5_u8, 1), (6, 1)],
            4,
            true,
            Some(NetError::from(ErrorKind::Io)),
        ))
        .dispatch()?;
    let seen = Arc::new(Mutex::new(Vec::new()));
    let capture = seen.clone();
    let subscription = receiver.into_callback(move |context, item| {
        let value = match item {
            Ok(value) => (context.id(), u64::from(value)),
            Err(ReceiveError::Lagged { skipped }) => (context.id(), skipped),
            Err(ReceiveError::Failed(_)) => (context.id(), 0),
        };
        if let Ok(mut seen) = capture.lock() {
            seen.push(value);
        }
    })?;
    check(
        subscription.id() == original_id,
        "seed conversion changed subscription identity",
    )?;
    executor.drain()?;
    check(
        *seen.lock().map_err(|_| "seen lock")?
            == vec![
                (original_id, 4),
                (original_id, 5),
                (original_id, 6),
                (original_id, 0),
            ],
        "callback did not consume the seed cursor exactly once",
    )?;
    check(
        matches!(block_on(subscription.close()), Err(error) if error.kind() == ErrorKind::Io),
        "seed error was lost from close",
    )
}