ntex 4.9.1

Framework for composable network services
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
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{sync::mpsc, thread};

use ntex::rt::{self, Arbiter, Handle, System};
use ntex::time::{Millis, sleep};

/// STATEMENT: Local and remote runtime join handles return task results across threads.
///
/// HOW:
/// - Spawn local and same-thread remote tasks and assert their values.
/// - Submit a task through `Handle` from another thread, join it there, and assert its result and the `Arbiter` debug label.
#[ntex::test]
async fn test_join_handle() {
    let hnd = rt::Handle::current();
    let f: rt::JoinHandle<_> = rt::spawn(async { std::rc::Rc::new("test") });
    assert_eq!(*f.await.unwrap(), "test");
    let local: rt::RemoteJoinHandle<_> = hnd.spawn(async { "local" });
    assert_eq!(local.await.unwrap(), "local");
    let (tx, rx) = oneshot::async_channel();

    let thread = thread::spawn(move || {
        let runner = crate::System::build().build(ntex::rt::DefaultRuntime);
        // submitted to the test's arbiter, joined on this thread
        let remote: rt::RemoteJoinHandle<_> = hnd.spawn(async { "test2" });
        let result = runner.block_on(remote).unwrap();
        let _ = tx.send(result);
    });

    let result = rx.await.unwrap();
    assert_eq!(result, "test2");
    thread.join().unwrap();

    assert!(format!("{:?}", Arbiter::current()).contains("Arbiter"));
}

/// STATEMENT: System and arbiter handles execute tasks in their owning system.
///
/// HOW:
/// - Run a system on another thread and submit detached, boxed, and joined tasks through its handles.
/// - Compare each task's `System::current().id()` with the target system ID.
#[test]
fn test_async() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let runner = crate::System::build().build(ntex::rt::DefaultRuntime);

        let _ = runner.run(move || {
            tx.send(System::current()).unwrap();
            Ok(())
        });
    });
    let s = System::new("test", ntex_net::DefaultRuntime);

    let sys = rx.recv().unwrap();
    let id = sys.id();
    let (tx, rx) = mpsc::channel();
    sys.handle().spawn_detached(async move {
        let _ = tx.send(System::current().id());
    });
    let id2 = rx.recv().unwrap();
    assert_eq!(id, id2);

    let (tx, rx) = mpsc::channel();
    sys.handle().spawn_detached(async move {
        let _ = tx.send(System::current().id());
    });
    let id2 = rx.recv().unwrap();
    assert_eq!(id, id2);

    let (tx, rx) = mpsc::channel();
    sys.arbiter().handle().spawn_detached(async move {
        let _ = tx.send(System::current().id());
    });
    let id2 = rx.recv().unwrap();
    assert_eq!(id, id2);

    let task = sys.handle().spawn(async { System::current().id() });
    let id2 = s.block_on(task).unwrap();
    assert_eq!(id, id2);

    let (tx, rx) = mpsc::channel();
    sys.handle().spawn_detached(Box::pin(async move {
        let _ = tx.send(System::current().id());
    }));
    let id2 = rx.recv().unwrap();
    assert_eq!(id, id2);
}

/// STATEMENT: A custom Tokio runner preserves system context and records arbiter pings.
///
/// HOW:
/// - Implement `Runner` with a current-thread Tokio runtime and run a system with a short ping interval.
/// - Assert submitted tasks observe the owning system ID.
/// - Poll ping records on the system thread until the first ping round, then
///   assert they are nonempty before stopping.
#[cfg(feature = "tokio")]
#[test]
fn test_block_on() {
    let (tx, rx) = mpsc::channel();

    struct Custom;

    impl ntex::rt::Runner for Custom {
        fn block_on(
            &self,
            fut: ntex::rt::BlockFuture,
        ) -> Result<(), Box<dyn std::any::Any + Send>> {
            let rt = ntex::rt::tokio::internal::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();
            ntex::rt::tokio::internal::task::LocalSet::new().block_on(&rt, fut);
            Ok(())
        }
    }

    thread::spawn(move || {
        let runner = crate::System::build().ping_interval(25).build(Custom);

        let _ = runner.run(move || {
            tx.send(System::current()).unwrap();
            Ok(())
        });
    });
    let s = System::new("test", ntex::rt::DefaultRuntime);

    let sys = rx.recv().unwrap();
    let id = sys.id();
    let (tx, rx) = mpsc::channel();
    sys.arbiter().handle().spawn_detached(async move {
        let _ = tx.send(System::current().id());
    });
    let id2 = rx.recv().unwrap();
    assert_eq!(id, id2);

    let task = sys.handle().spawn(async { System::current().id() });
    let id2 = s.block_on(task).unwrap();
    assert_eq!(id, id2);

    let (tx, rx) = mpsc::channel();
    // timers are thread-local, create the sleep on the system thread
    sys.handle().spawn_detached(async move {
        rt::spawn_detached(async move {
            // pings use their own clock, wait for the first round
            let mut recs = Vec::new();
            for _ in 0..200 {
                ntex::time::sleep(std::time::Duration::from_millis(25)).await;
                System::list_arbiter_pings(|_, r| recs.extend(r.iter().copied()));
                if !recs.is_empty() {
                    break;
                }
            }
            let _ = tx.send(recs);
        });
    });
    let recs = rx.recv().unwrap();

    assert!(!recs.is_empty());
    sys.stop();
}

/// STATEMENT: Arbiter-local and runtime typed storage return initialized values.
///
/// HOW:
/// - Create a `System`, initialize an arbiter-local `u64`, and assert it is `64`.
/// - Store `100u32` with `set_item` and retrieve the same value with `get_item`.
#[test]
fn test_arbiter_local_storage() {
    let _s = System::new("test", ntex::rt::DefaultRuntime);
    assert!(Arbiter::get_value(|| 64u64) == 64);

    ntex::rt::set_item(100u32);
    assert_eq!(ntex::rt::get_item::<u32>().unwrap(), 100);
}

/// STATEMENT: Spawned tasks expose completion and return their values through handles.
///
/// HOW:
/// - Spawn a sleeping task and check `is_finished` before and after awaiting it.
/// - Spawn through the current `Handle` and assert the joined result is `1`.
#[test]
fn test_spawn_api() {
    System::new("test", ntex::rt::DefaultRuntime).block_on(async {
        let mut hnd = ntex::rt::spawn(async {
            sleep(Millis(25)).await;
        });
        assert!(!hnd.is_finished());
        let _ = (&mut hnd).await;
        assert!(hnd.is_finished());

        let hnd = crate::Handle::current();
        let task = hnd.spawn(async move { 1 });
        let res = task.await.unwrap();
        assert_eq!(res, 1);
    });
}

static CB_COUNTER: AtomicUsize = AtomicUsize::new(0);

fn counting_callbacks() -> ntex::rt::TaskCallbacks {
    ntex::rt::TaskCallbacks {
        before: || {
            CB_COUNTER.fetch_add(1, Ordering::Relaxed);
            Some(CB_COUNTER.as_ptr().cast_const().cast())
        },
        enter: |ptr| {
            CB_COUNTER.fetch_add(1, Ordering::Relaxed);
            ptr
        },
        exit: |_| {
            CB_COUNTER.fetch_add(1, Ordering::Relaxed);
        },
        after: |_| {
            CB_COUNTER.fetch_add(1, Ordering::Relaxed);
        },
    }
}

/// STATEMENT: Task callbacks set with the `task_callbacks` test argument run for spawned work.
///
/// HOW:
/// - Run the test system with counting callbacks passed through `#[ntex::test]`.
/// - Join a sleeping task, assert completion, and verify `before`, `enter`,
///   `exit`, and `after` increased the counter.
#[ntex::test(task_callbacks = counting_callbacks)]
async fn test_spawn_cb() {
    let start = CB_COUNTER.load(Ordering::Relaxed);
    let mut hnd = ntex::rt::spawn(async {
        sleep(Millis(25)).await;
    });
    let _ = (&mut hnd).await;
    assert!(hnd.is_finished());

    // `before` and `after` once, `enter` and `exit` for each of at least two polls
    assert!(CB_COUNTER.load(Ordering::Relaxed) - start >= 6);
}

/// STATEMENT: `Arbiter::on_shutdown` callbacks, including nested registrations, run while the arbiter is still running.
///
/// HOW:
/// - Register a shutdown callback that registers another callback on a new arbiter.
/// - Stop the arbiter, assert the nested callback observes `is_running()`, and join the arbiter.
#[test]
fn test_on_shutdown_arbiter() {
    System::new("test", ntex::rt::DefaultRuntime).block_on(async {
        let (tx, rx) = oneshot::channel();
        let mut arb = Arbiter::new();
        let registered = arb.handle().spawn(async move {
            Arbiter::on_shutdown(move || {
                Arbiter::on_shutdown(move || {
                    let _ = tx.send(Arbiter::current().is_running());
                });
            });
        });
        registered.await.unwrap();

        arb.stop();
        assert!(
            rx.await.unwrap(),
            "callback ran after the event loop exited"
        );
        arb.join().unwrap();
    });
}

/// STATEMENT: System shutdown callbacks run on explicit stop and on event-loop exit without a stop request.
///
/// HOW:
/// - Register a counting callback in a system that calls `System::stop` and assert one invocation.
/// - Register another in `block_on`, assert it has not run inside the loop, and assert one additional invocation on exit.
#[test]
fn test_on_shutdown_system() {
    thread::spawn(|| {
        let ran = std::rc::Rc::new(std::cell::Cell::new(0));

        let ran2 = ran.clone();
        System::build()
            .build(ntex::rt::DefaultRuntime)
            .run(move || {
                Arbiter::on_shutdown(move || ran2.set(ran2.get() + 1));
                System::current().stop();
                Ok(())
            })
            .unwrap();
        assert_eq!(ran.get(), 1);

        let ran2 = ran.clone();
        System::new("test", ntex::rt::DefaultRuntime).block_on(async move {
            let ran3 = ran2.clone();
            Arbiter::on_shutdown(move || ran3.set(ran3.get() + 1));
            sleep(Millis(10)).await;
            assert_eq!(ran2.get(), 1, "callback ran before shutdown");
        });
        assert_eq!(ran.get(), 2);
    })
    .join()
    .unwrap();
}

/// STATEMENT: System-scoped typed storage is shared across arbiters.
///
/// HOW:
/// - Initialize a `usize` to `10` in the current system.
/// - Read it from a new arbiter using an initializer returning `11` and assert the existing value `10` is retained.
#[ntex::test]
async fn system_storage() {
    let val = System::current().get_value::<usize>(|| 10);
    assert_eq!(val, 10);

    let arb = Arbiter::new();
    let task = arb
        .handle()
        .spawn(async { System::current().get_value::<usize>(|| 11) });
    let val2 = task.await.unwrap();
    assert_eq!(val2, 10);
}

/// STATEMENT: The io-uring backend drains input and observes EOF after an idle peer closes.
///
/// HOW:
/// - Use small read buffers and backpressure while a server sends two delayed copies of `DATA` and closes.
/// - Receive with `BytesCodec` under a five-second timeout and assert EOF arrives after exactly twice the payload length.
#[cfg(all(target_os = "linux", feature = "neon-uring"))]
#[ntex::test]
async fn idle_disconnect_uring() {
    use std::sync::{Arc, Mutex};

    use ntex::io::{Io, IoConfig};
    use ntex::util::{BytePageSize, Bytes};
    use ntex::{SharedCfg, codec::BytesCodec, connect::Connect};
    use ntex::{time::Millis, time::sleep, time::timeout};

    const DATA: &[u8] = b"Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World \
                          Hello World Hello World Hello World Hello World Hello World";

    let (tx, rx) = ::oneshot::channel();
    let tx = Arc::new(Mutex::new(Some(tx)));

    let server = ntex::server::test_server(async move |_| {
        let tx = tx.clone();
        ntex::fn_service(move |io: Io<_>| {
            tx.lock().unwrap().take().unwrap().send(()).unwrap();

            async move {
                io.encode_slice(DATA).unwrap();
                sleep(Millis(250)).await;
                io.encode_bytes(Bytes::from_static(DATA)).unwrap();
                sleep(Millis(250)).await;
                io.close();
                Ok::<_, ()>(())
            }
        })
    });

    let cfg = SharedCfg::new("NEON-URING")
        .add(
            IoConfig::new()
                .set_read_size(BytePageSize::Size4, BytePageSize::Size4)
                .set_read_backpressure(BytePageSize::Size4.capacity()),
        )
        .build();

    let msg = Connect::new(server.addr());
    let io = ntex::connect::connect_with(msg, &cfg).await.unwrap();
    rx.await.unwrap();

    // Server close is a FIN, which is indistinguishable from a half-close, so
    // it is observed as eof once all input is read
    let mut total = 0;
    let res = timeout(Millis(5000), async {
        while let Ok(Some(item)) = io.recv(&BytesCodec).await {
            total += item.len();
        }
    })
    .await;
    assert!(res.is_ok(), "eof is not observed");
    assert_eq!(total, DATA.len() * 2);
}

/// STATEMENT: Arbiter latency callbacks report stalls and supply the spawn location when available.
///
/// HOW:
/// - Run a deliberately blocking arbiter once without a callback, then repeat with `System::set_latency_callback` installed.
/// - Assert the callback ran and, when a location is reported, that its file is this test file.
#[cfg(any(target_os = "linux", windows))]
#[test]
fn test_log_backtrace() {
    use std::{sync::Arc, sync::atomic::AtomicBool, time::Duration};

    fn run(tx: oneshot::Sender<()>) {
        thread::spawn(move || {
            crate::System::build()
                .signals(true)
                .ping_interval(1000)
                .ping_threshold(500)
                .build(ntex::rt::DefaultRuntime)
                .block_on(async move {
                    let (tx, rx) = oneshot::channel();
                    Arbiter::with_name("ttttt".to_string())
                        .handle()
                        .spawn(async {
                            thread::sleep(Duration::from_secs(2));
                            let _ = tx.send(());
                        });

                    let _ = rx.await;
                });
            let _ = tx.send(());
        });
    }

    let (tx, rx) = oneshot::channel();
    run(tx);
    let _ = rx.recv();

    let tag = Arc::new(AtomicBool::new(false));
    let tag2 = tag.clone();
    let task = Arc::new(std::sync::Mutex::new(None));
    let task2 = task.clone();
    System::set_latency_callback(move |_, loc| {
        *task2.lock().unwrap() = loc;
        tag2.store(true, Ordering::Relaxed);
    });

    let (tx, rx) = oneshot::channel();
    run(tx);
    let _ = rx.recv();
    assert!(tag.load(Ordering::Relaxed));
    // neon reports the spawn location, tokio and compio report none
    if let Some(loc) = *task.lock().unwrap() {
        assert_eq!(loc.file(), file!());
    }
}