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
use super::*;
use crate::error::ErrorStage;
use std::collections::VecDeque;
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll, Wake, Waker};

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 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);
    }
}
impl CountWake {
    fn count(&self) -> usize {
        self.0.load(Ordering::SeqCst)
    }
}

#[derive(Default)]
struct ManualExecutor(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<()> {
        let mut jobs = self.0.lock().map_err(NetError::from_poison)?;
        jobs.try_reserve(1)
            .map_err(|error| NetError::with_source(ErrorKind::ResourceExhausted, error))?;
        jobs.push_back(job);
        Ok(())
    }
}
impl ManualExecutor {
    fn run_one(&self) -> TestResult {
        let job = self
            .0
            .lock()
            .map_err(NetError::from_poison)?
            .pop_front()
            .ok_or("callback job missing")?;
        job();
        Ok(())
    }
}

fn channel<T>(
    executor: Arc<dyn CallbackExecutor>,
) -> TestResult<(EventPublisher<T>, EventReceiver<T>)> {
    Ok(event_channel(
        EventQueueLimit {
            max_items: 2,
            max_bytes: 64,
        },
        EventOverflow::Wait,
        executor,
        None,
    )?)
}

#[test]
fn receiver_exit_wakes_every_registered_waiter_without_cancelled_waiter_stealing_notification(
) -> TestResult {
    let (publisher, receiver) = channel::<u8>(Arc::new(ManualExecutor::default()))?;
    let first_count = Arc::new(CountWake::default());
    let cancelled_count = Arc::new(CountWake::default());
    let last_count = Arc::new(CountWake::default());
    let first_waker = Waker::from(first_count.clone());
    let cancelled_waker = Waker::from(cancelled_count.clone());
    let last_waker = Waker::from(last_count.clone());
    let mut first = Box::pin(publisher.receiver_closed());
    let mut cancelled = Box::pin(publisher.receiver_closed());
    let mut last = Box::pin(publisher.receiver_closed());
    check(
        first
            .as_mut()
            .poll(&mut Context::from_waker(&first_waker))
            .is_pending(),
        "first waiter completed before detach",
    )?;
    check(
        cancelled
            .as_mut()
            .poll(&mut Context::from_waker(&cancelled_waker))
            .is_pending(),
        "middle waiter completed before detach",
    )?;
    check(
        last.as_mut()
            .poll(&mut Context::from_waker(&last_waker))
            .is_pending(),
        "last waiter completed before detach",
    )?;
    drop(cancelled);
    check(
        receiver.unsubscribe(),
        "first unsubscribe did not revoke the receiver",
    )?;
    check(
        first_count.count() == 1 && last_count.count() == 1,
        "detach failed to wake both live waiters exactly once",
    )?;
    check(
        cancelled_count.count() == 0,
        "cancelled waiter retained a detach notification",
    )?;
    check(
        first
            .as_mut()
            .poll(&mut Context::from_waker(&first_waker))
            .is_ready(),
        "first wake did not make producer ready",
    )?;
    check(
        last.as_mut()
            .poll(&mut Context::from_waker(&last_waker))
            .is_ready(),
        "last wake did not make producer ready",
    )?;
    let mut late = Box::pin(publisher.receiver_closed());
    check(
        late.as_mut()
            .poll(&mut Context::from_waker(&first_waker))
            .is_ready(),
        "late producer missed permanent detach",
    )?;
    check(
        !receiver.unsubscribe(),
        "repeated unsubscribe changed the subscription again",
    )?;
    check(
        first_count.count() == 1 && last_count.count() == 1,
        "idempotent unsubscribe notified already completed waiters",
    )
}

#[test]
fn data_capacity_changes_and_source_finish_do_not_wake_receiver_exit_waiters() -> TestResult {
    let (publisher, mut receiver) = channel::<u8>(Arc::new(ManualExecutor::default()))?;
    let count = Arc::new(CountWake::default());
    let waker = Waker::from(count.clone());
    let mut cx = Context::from_waker(&waker);
    let mut detached = Box::pin(publisher.receiver_closed());
    check(
        detached.as_mut().poll(&mut cx).is_pending(),
        "empty live receiver was detached",
    )?;
    publisher.try_publish(1, 1)?;
    publisher.try_publish(2, 1)?;
    check(receiver.try_recv()? == 1, "first buffered item was changed")?;
    publisher.finish()?;
    check(
        count.count() == 0,
        "data or source-finish notification leaked into receiver_closed",
    )?;
    check(
        detached.as_mut().poll(&mut cx).is_pending(),
        "finish detached a receiver with a buffered prefix",
    )?;
    check(
        receiver.try_recv()? == 2,
        "source finish lost the buffered suffix",
    )?;
    check(
        count.count() == 0,
        "last item delivery detached before consumer EOF",
    )?;
    check(
        matches!(
            receiver.try_recv(),
            Err(crate::error::TryReceiveError::Closed)
        ),
        "source finish did not produce EOF",
    )?;
    check(
        count.count() == 1,
        "consumer EOF failed to wake its producer",
    )?;
    check(
        detached.as_mut().poll(&mut cx).is_ready(),
        "producer did not observe consumed EOF",
    )
}

struct UnavailableExecutor;
impl CallbackExecutor for UnavailableExecutor {
    fn ensure_ready(&self) -> Result<()> {
        Err(NetError::from(ErrorKind::RuntimeUnavailable).with_stage(ErrorStage::Dispatch))
    }
    fn submit(&self, _job: Box<dyn FnOnce() + Send>) -> Result<()> {
        Err(NetError::from(ErrorKind::RuntimeUnavailable).with_stage(ErrorStage::Dispatch))
    }
}

#[test]
fn failed_callback_conversion_detaches_and_releases_the_unconsumed_entry() -> TestResult {
    let (publisher, receiver) = channel::<u8>(Arc::new(UnavailableExecutor))?;
    let items = Arc::new(Semaphore::new(1));
    publisher
        .prepare_try_publish_with_resources(
            7,
            1,
            EventResources {
                items: Some(items.clone().try_acquire_owned()?),
                ..EventResources::default()
            },
        )
        .dispatch()?;
    let count = Arc::new(CountWake::default());
    let waker = Waker::from(count.clone());
    let mut cx = Context::from_waker(&waker);
    let mut detached = Box::pin(publisher.receiver_closed());
    check(
        detached.as_mut().poll(&mut cx).is_pending(),
        "consumer detached before conversion",
    )?;
    let result = receiver.into_callback(|_, _| {});
    check(
        matches!(result, Err(ref error) if error.kind() == ErrorKind::RuntimeUnavailable),
        "callback startup failure was hidden",
    )?;
    check(
        items.available_permits() == 1,
        "failed conversion retained queued resource",
    )?;
    check(
        count.count() == 1,
        "failed conversion did not wake the waiting producer",
    )?;
    check(
        detached.as_mut().poll(&mut cx).is_ready(),
        "producer remained attached after failed conversion",
    )
}

#[test]
fn running_callback_fault_detaches_before_close_and_retires_unread_resources() -> TestResult {
    let executor = Arc::new(ManualExecutor::default());
    let (publisher, receiver) = channel::<u8>(executor.clone())?;
    let registration = receiver.registration.clone();
    let items = Arc::new(Semaphore::new(2));
    for value in [1, 2] {
        publisher
            .prepare_try_publish_with_resources(
                value,
                1,
                EventResources {
                    items: Some(items.clone().try_acquire_owned()?),
                    ..EventResources::default()
                },
            )
            .dispatch()?;
    }
    let count = Arc::new(CountWake::default());
    let waker = Waker::from(count.clone());
    let mut cx = Context::from_waker(&waker);
    let mut detached = Box::pin(publisher.receiver_closed());
    check(
        detached.as_mut().poll(&mut cx).is_pending(),
        "producer detached before callback execution",
    )?;
    let calls = Arc::new(AtomicUsize::new(0));
    let observed = calls.clone();
    let subscription = receiver.into_callback(move |_, _| {
        observed.fetch_add(1, Ordering::SeqCst);
        // Inject the caught-unwind outcome inside an acquired callback without
        // constructing a real panic path in the test process.
        registration.fail_callback_for_test();
    })?;
    executor.run_one()?;
    check(
        calls.load(Ordering::SeqCst) == 1,
        "faulted callback consumed a later queued event",
    )?;
    check(
        items.available_permits() == 2,
        "callback fault retained queued resource",
    )?;
    check(
        count.count() == 1 && detached.as_mut().poll(&mut cx).is_ready(),
        "callback fault did not release the producer",
    )?;
    let mut closing = Box::pin(subscription.close());
    check(
        matches!(closing.as_mut().poll(&mut cx), Poll::Ready(Err(ref error)) if error.kind() == ErrorKind::CallbackPanicked),
        "close lost the acquired callback failure",
    )
}

#[derive(Debug, Eq, PartialEq)]
enum ReservationOutcome {
    Detached,
    Reserved,
}

#[test]
fn detach_interrupts_a_partial_count_and_byte_reservation_without_cancelling_its_owner(
) -> TestResult {
    let (publisher, receiver) = channel::<u8>(Arc::new(ManualExecutor::default()))?;
    let items = Arc::new(Semaphore::new(3));
    let bytes = Arc::new(Semaphore::new(3));
    publisher
        .prepare_try_publish_with_resources(
            1,
            3,
            EventResources {
                bytes: Some(bytes.clone().try_acquire_many_owned(3)?),
                ..EventResources::default()
            },
        )
        .dispatch()?;
    let (owner_cancel, mut owner_for_reservation) = tokio::sync::watch::channel(false);
    let items_for_reservation = items.clone();
    let bytes_for_reservation = bytes.clone();
    let mut reservation = Box::pin(async {
        let partial = items_for_reservation
            .acquire_many_owned(3)
            .await
            .map_err(|_| NetError::from(ErrorKind::Closed))?;
        let outcome = tokio::select! {
            biased;
            _ = owner_for_reservation.changed() => Err(NetError::from(ErrorKind::Cancelled)),
            _ = publisher.receiver_closed() => Ok(ReservationOutcome::Detached),
            acquired = bytes_for_reservation.acquire_many_owned(3) => {
                let acquired = acquired.map_err(|_| NetError::from(ErrorKind::Closed))?;
                drop(acquired);
                Ok(ReservationOutcome::Reserved)
            }
        };
        drop(partial);
        outcome
    });
    let count = Arc::new(CountWake::default());
    let waker = Waker::from(count.clone());
    let mut cx = Context::from_waker(&waker);
    check(
        reservation.as_mut().poll(&mut cx).is_pending(),
        "reservation did not wait on exhausted bytes",
    )?;
    check(
        items.available_permits() == 0,
        "reservation did not own its partial count permits",
    )?;
    drop(receiver);
    check(
        count.count() > 0,
        "receiver Drop did not wake the partial reservation",
    )?;
    check(
        matches!(
            reservation.as_mut().poll(&mut cx),
            Poll::Ready(Ok(ReservationOutcome::Detached))
        ),
        "observation detach became owner cancellation or a fresh reservation",
    )?;
    check(
        items.available_permits() == 3 && bytes.available_permits() == 3,
        "detached reservation leaked count or bytes",
    )?;
    let owner_state = owner_cancel.subscribe();
    let owner_is_active = !*::tokio::sync::watch::Receiver::borrow(&owner_state);
    check(
        owner_is_active,
        "receiver Drop cancelled the independent owner",
    )
}

#[test]
fn receiver_drop_rejects_all_pending_resource_publications_and_returns_their_permits() -> TestResult
{
    let (publisher, receiver) = channel::<u8>(Arc::new(ManualExecutor::default()))?;
    publisher.try_publish(1, 1)?;
    publisher.try_publish(2, 1)?;
    let items = Arc::new(Semaphore::new(2));
    let mut first = Box::pin(publisher.publish_with_resources(
        3,
        1,
        EventResources {
            items: Some(items.clone().try_acquire_owned()?),
            ..EventResources::default()
        },
    ));
    let mut second = Box::pin(publisher.publish_with_resources(
        4,
        1,
        EventResources {
            items: Some(items.clone().try_acquire_owned()?),
            ..EventResources::default()
        },
    ));
    let first_count = Arc::new(CountWake::default());
    let second_count = Arc::new(CountWake::default());
    let first_waker = Waker::from(first_count.clone());
    let second_waker = Waker::from(second_count.clone());
    let mut first_cx = Context::from_waker(&first_waker);
    let mut second_cx = Context::from_waker(&second_waker);
    check(
        first.as_mut().poll(&mut first_cx).is_pending(),
        "first publish bypassed queue capacity",
    )?;
    check(
        second.as_mut().poll(&mut second_cx).is_pending(),
        "second publish bypassed queue capacity",
    )?;
    drop(receiver);
    check(
        first_count.count() > 0 && second_count.count() > 0,
        "receiver Drop failed to wake every blocked publisher",
    )?;
    check(
        matches!(first.as_mut().poll(&mut first_cx), Poll::Ready(Err(ref error)) if error.kind() == ErrorKind::Closed),
        "first pending publish did not report detached receiver",
    )?;
    check(
        matches!(second.as_mut().poll(&mut second_cx), Poll::Ready(Err(ref error)) if error.kind() == ErrorKind::Closed),
        "second pending publish did not report detached receiver",
    )?;
    check(
        items.available_permits() == 2,
        "detached pending publications retained resources",
    )
}