nautilus-network 0.61.0

Network communication machinery for the Nautilus trading engine
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Turmoil integration tests for the `SocketClient`.
//!
//! These tests use turmoil's network simulation to test the actual production
//! `SocketClient` code under various network conditions.

#![cfg(feature = "turmoil")]

use std::{
    sync::{
        Arc, Mutex,
        atomic::{AtomicUsize, Ordering},
    },
    time::Duration,
};

use nautilus_network::socket::{SocketClient, SocketConfig};
use rstest::{fixture, rstest};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_tungstenite::tungstenite::stream::Mode;
use turmoil::net;

mod common;

use common::turmoil::{seeded_builder, seeded_builder_with_duration, stressed_builder};

// 2-second budget in simulated time, covering reconnect timings across these tests.
const POLL_ITERS: u32 = 200;
const POLL_STEP: Duration = Duration::from_millis(10);
const BASIC_CONNECT_SEED: u64 = 0x51C0_0001;
const RECONNECTION_SEED: u64 = 0x51C0_0002;
const NETWORK_PARTITION_SEED: u64 = 0x51C0_0003;
const CLOSE_DURING_RECONNECT_SEED: u64 = 0x51C0_0004;
const CLOSE_DURING_BACKOFF_SEED: u64 = 0x51C0_0005;
const UNSTABLE_RECONNECT_SEED: u64 = 0x51C0_0006;
const STABLE_RECONNECT_SEED: u64 = 0x51C0_0007;
const UNSTABLE_RECONNECT_DELAY_MS: u64 = 750;

async fn wait_for<F>(mut condition: F) -> bool
where
    F: FnMut() -> bool,
{
    for _ in 0..POLL_ITERS {
        if condition() {
            return true;
        }
        tokio::time::sleep(POLL_STEP).await;
    }
    false
}

type ReceivedMessages = Arc<Mutex<Vec<String>>>;

fn attach_message_capture(config: &mut SocketConfig, received: &ReceivedMessages) {
    let received = Arc::clone(received);
    config.message_handler = Some(Arc::new(move |data: &[u8]| {
        received
            .lock()
            .expect("mutex poisoned")
            .push(String::from_utf8_lossy(data).to_string());
    }));
}

fn captured_messages(received: &ReceivedMessages) -> Vec<String> {
    received.lock().expect("mutex poisoned").clone()
}

async fn echo_once_then_drop_server() -> Result<(), Box<dyn std::error::Error>> {
    let listener = net::TcpListener::bind("0.0.0.0:8080").await?;

    loop {
        let (mut stream, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buffer = vec![0; 1024];
            if let Ok(n) = stream.read(&mut buffer).await
                && n > 0
            {
                if !buffer.starts_with(b"close\r\n") {
                    let _ = stream.write_all(&buffer[..n]).await;
                }
                let _ = stream.shutdown().await;
            }
        });
    }
}

async fn drop_each_connection_server(
    accepted: Arc<AtomicUsize>,
) -> Result<(), Box<dyn std::error::Error>> {
    let listener = net::TcpListener::bind("0.0.0.0:8080").await?;

    loop {
        let (stream, _) = listener.accept().await?;
        accepted.fetch_add(1, Ordering::SeqCst);
        drop(stream);
    }
}

async fn hold_stable_reconnect_server(
    accepted: Arc<AtomicUsize>,
) -> Result<(), Box<dyn std::error::Error>> {
    let listener = net::TcpListener::bind("0.0.0.0:8080").await?;

    loop {
        let (stream, _) = listener.accept().await?;
        let connection = accepted.fetch_add(1, Ordering::SeqCst);

        match connection {
            0 => drop(stream),
            1 => {
                tokio::time::sleep(Duration::from_secs(11)).await;
                drop(stream);
            }
            _ => {
                let _held_stream = stream;
                std::future::pending::<()>().await;
            }
        }
    }
}

/// Default test socket configuration.
#[fixture]
fn socket_config() -> SocketConfig {
    SocketConfig {
        url: "server:8080".to_string(),
        mode: Mode::Plain,
        suffix: b"\r\n".to_vec(),
        message_handler: None,
        heartbeat: None,
        reconnect_timeout_ms: Some(2_000),
        reconnect_delay_initial_ms: Some(50),
        reconnect_delay_max_ms: Some(500),
        reconnect_backoff_factor: Some(1.5),
        reconnect_jitter_ms: Some(10),
        connection_max_retries: None,
        reconnect_max_attempts: None,
        idle_timeout_ms: None,
        certs_dir: None,
    }
}

/// Echo server for testing.
async fn echo_server() -> Result<(), Box<dyn std::error::Error>> {
    let listener = net::TcpListener::bind("0.0.0.0:8080").await?;

    loop {
        if let Ok((mut stream, _)) = listener.accept().await {
            tokio::spawn(async move {
                let mut buffer = vec![0; 1024];

                loop {
                    match stream.read(&mut buffer).await {
                        Ok(0) | Err(_) => break,
                        Ok(n) => {
                            // Check for termination message
                            if buffer.starts_with(b"close\r\n") {
                                let _ = stream.shutdown().await;
                                break;
                            }
                            // Echo back the data
                            if stream.write_all(&buffer[..n]).await.is_err() {
                                break;
                            }
                        }
                    }
                }
            });
        }
    }
}

#[rstest]
fn test_turmoil_real_socket_basic_connect(socket_config: SocketConfig) {
    let mut socket_config = socket_config;
    let received = Arc::new(Mutex::new(Vec::new()));
    attach_message_capture(&mut socket_config, &received);

    let mut sim = seeded_builder(BASIC_CONNECT_SEED).build();

    sim.host("server", echo_server);

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        // Verify client is active
        assert!(client.is_active(), "Client should be active after connect");

        client
            .send_bytes(b"hello".to_vec())
            .await
            .expect("Should send data");
        assert!(
            wait_for(|| captured_messages(&received) == ["hello"]).await,
            "Client should receive echoed hello"
        );

        client
            .send_bytes(b"close".to_vec())
            .await
            .expect("Should send close");

        client.close().await;
        assert!(client.is_closed(), "Client should be closed");

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_real_socket_reconnection(mut socket_config: SocketConfig) {
    socket_config.reconnect_timeout_ms = Some(5_000);
    socket_config.reconnect_delay_initial_ms = Some(100);
    let received = Arc::new(Mutex::new(Vec::new()));
    attach_message_capture(&mut socket_config, &received);

    let mut sim = seeded_builder(RECONNECTION_SEED).build();

    // Server that accepts one connection, closes it, then accepts another
    sim.host("server", || async {
        let listener = net::TcpListener::bind("0.0.0.0:8080").await?;

        // Accept first connection
        if let Ok((mut stream, _)) = listener.accept().await {
            let mut buffer = vec![0; 1024];
            let _ = stream.read(&mut buffer).await;
            let _ = stream.write_all(b"first\r\n").await;
            drop(stream);
        }

        // Accept second connection and run echo loop
        if let Ok((mut stream, _)) = listener.accept().await {
            let mut buffer = vec![0; 1024];
            loop {
                match stream.read(&mut buffer).await {
                    Ok(0) | Err(_) => break,
                    Ok(n) => {
                        if buffer.starts_with(b"close\r\n") {
                            break;
                        }

                        if stream.write_all(&buffer[..n]).await.is_err() {
                            break;
                        }
                    }
                }
            }
        }

        Ok::<(), Box<dyn std::error::Error>>(())
    });

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        client
            .send_bytes(b"first_msg".to_vec())
            .await
            .expect("Should send first message");
        assert!(
            wait_for(|| captured_messages(&received) == ["first"]).await,
            "Client should receive first message before reconnect"
        );

        // Server closes after echoing; wait for the client to cycle through
        // reconnection and return to an active state before the next send.
        assert!(
            wait_for(|| client.is_reconnecting() || !client.is_active()).await,
            "Client should observe server disconnect"
        );
        assert!(
            wait_for(|| client.is_active()).await,
            "Client should reconnect after server close"
        );

        client
            .send_bytes(b"second_msg".to_vec())
            .await
            .expect("Should send second message after reconnect");
        assert!(
            wait_for(|| captured_messages(&received) == ["first", "second_msg"]).await,
            "Client should receive post-reconnect echo"
        );

        client.send_bytes(b"close".to_vec()).await.ok();
        client.close().await;

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_socket_unstable_reconnects_exhaust_attempts(mut socket_config: SocketConfig) {
    socket_config.reconnect_delay_initial_ms = Some(UNSTABLE_RECONNECT_DELAY_MS);
    socket_config.reconnect_delay_max_ms = Some(UNSTABLE_RECONNECT_DELAY_MS);
    socket_config.reconnect_backoff_factor = Some(1.0);
    socket_config.reconnect_jitter_ms = Some(0);
    socket_config.reconnect_max_attempts = Some(3);

    let accepted = Arc::new(AtomicUsize::new(0));
    let server_accepted = Arc::clone(&accepted);
    let mut builder =
        seeded_builder_with_duration(UNSTABLE_RECONNECT_SEED, Duration::from_secs(10));
    builder.min_message_latency(Duration::ZERO);
    builder.max_message_latency(Duration::ZERO);
    let mut sim = builder.build();

    sim.host("server", move || {
        drop_each_connection_server(Arc::clone(&server_accepted))
    });

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Initial socket connection should succeed");
        let started_at = tokio::time::Instant::now();

        assert!(
            wait_for(|| client.is_closed()).await,
            "Rapidly dropped reconnects should exhaust the attempt limit"
        );
        assert!(
            started_at.elapsed() >= Duration::from_millis(UNSTABLE_RECONNECT_DELAY_MS),
            "Rapidly dropped reconnects should retain the backoff progression"
        );
        assert_eq!(
            accepted.load(Ordering::SeqCst),
            4,
            "Server should accept the initial connection and three reconnect attempts"
        );

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_socket_stable_reconnect_resets_attempts(mut socket_config: SocketConfig) {
    socket_config.reconnect_delay_initial_ms = Some(50);
    socket_config.reconnect_delay_max_ms = Some(200);
    socket_config.reconnect_backoff_factor = Some(2.0);
    socket_config.reconnect_jitter_ms = Some(0);
    socket_config.reconnect_max_attempts = Some(1);

    let accepted = Arc::new(AtomicUsize::new(0));
    let server_accepted = Arc::clone(&accepted);
    let mut sim =
        seeded_builder_with_duration(STABLE_RECONNECT_SEED, Duration::from_secs(20)).build();

    sim.host("server", move || {
        hold_stable_reconnect_server(Arc::clone(&server_accepted))
    });

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Initial socket connection should succeed");

        tokio::time::sleep(Duration::from_secs(13)).await;

        assert!(
            wait_for(|| accepted.load(Ordering::SeqCst) >= 3).await,
            "A stable reconnect should reset the attempt limit for the next drop"
        );
        assert!(
            client.is_active(),
            "Client should remain active on the connection after the reset"
        );

        client.close().await;
        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_real_socket_network_partition(mut socket_config: SocketConfig) {
    socket_config.reconnect_timeout_ms = Some(3_000);
    let received = Arc::new(Mutex::new(Vec::new()));
    attach_message_capture(&mut socket_config, &received);

    let mut sim = seeded_builder(NETWORK_PARTITION_SEED).build();

    sim.host("server", echo_server);

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        client
            .send_bytes(b"before_partition".to_vec())
            .await
            .expect("Should send before partition");
        assert!(
            wait_for(|| captured_messages(&received) == ["before_partition"]).await,
            "Client should receive echoed before_partition"
        );

        turmoil::partition("client", "server");
        tokio::time::sleep(Duration::from_millis(200)).await;
        turmoil::repair("client", "server");

        // Either the connection survived the partition or reconnect restored it;
        // poll until the client is active again before sending.
        assert!(
            wait_for(|| client.is_active()).await,
            "Client should be active after partition repair"
        );

        client
            .send_bytes(b"after_partition".to_vec())
            .await
            .expect("Should send after partition repair");
        assert!(
            wait_for(|| {
                captured_messages(&received) == ["before_partition", "after_partition"]
            })
            .await,
            "Client should receive echoed after_partition"
        );

        client.send_bytes(b"close".to_vec()).await.ok();
        client.close().await;

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_real_socket_close_during_reconnect(mut socket_config: SocketConfig) {
    socket_config.reconnect_timeout_ms = Some(5_000);
    socket_config.reconnect_delay_initial_ms = Some(100);

    let mut sim = seeded_builder(CLOSE_DURING_RECONNECT_SEED).build();

    sim.host("server", echo_server);

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        assert!(client.is_active(), "Client should be active after connect");

        turmoil::partition("client", "server");
        tokio::time::sleep(Duration::from_millis(200)).await;

        client.close().await;

        assert!(
            client.is_closed(),
            "Client should be closed after close during reconnect"
        );
        assert!(
            !client.is_active(),
            "Client should not be active after close"
        );

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
fn test_turmoil_real_socket_disconnect_during_backoff(mut socket_config: SocketConfig) {
    socket_config.reconnect_timeout_ms = Some(1_000);
    socket_config.reconnect_delay_initial_ms = Some(10_000); // Long backoff
    socket_config.reconnect_delay_max_ms = Some(10_000);
    socket_config.reconnect_backoff_factor = Some(1.0);
    socket_config.reconnect_jitter_ms = Some(0);

    let mut sim =
        seeded_builder_with_duration(CLOSE_DURING_BACKOFF_SEED, Duration::from_secs(30)).build();

    sim.host("server", echo_server);

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        assert!(client.is_active());

        // Partition to force reconnect
        turmoil::partition("client", "server");
        tokio::time::sleep(Duration::from_millis(300)).await;

        // Client should be reconnecting; reconnect attempt fails, enters 10s backoff
        tokio::time::sleep(Duration::from_millis(1_500)).await;

        let start = tokio::time::Instant::now();
        client.close().await;
        let elapsed = start.elapsed();

        assert!(client.is_closed(), "Client should be closed");
        assert!(
            elapsed < Duration::from_secs(3),
            "Close should interrupt backoff, took {elapsed:?}"
        );

        Ok(())
    });

    sim.run().unwrap();
}

#[rstest]
#[case::seed_a(0x51C0_1001)]
#[case::seed_b(0x51C0_1002)]
#[case::seed_c(0x51C0_1003)]
fn test_turmoil_socket_repeated_drops_preserve_message_order(
    mut socket_config: SocketConfig,
    #[case] seed: u64,
) {
    socket_config.reconnect_timeout_ms = Some(5_000);
    socket_config.reconnect_delay_initial_ms = Some(25);
    socket_config.reconnect_delay_max_ms = Some(100);
    socket_config.reconnect_backoff_factor = Some(1.0);
    socket_config.reconnect_jitter_ms = Some(0);
    let received = Arc::new(Mutex::new(Vec::new()));
    attach_message_capture(&mut socket_config, &received);

    let mut sim = stressed_builder(seed, Duration::from_secs(20)).build();

    sim.host("server", echo_once_then_drop_server);

    sim.client("client", async move {
        let client = SocketClient::connect(socket_config, None, None, None)
            .await
            .expect("Should connect");

        let expected = (0..6)
            .map(|i| format!("drop-reconnect-{i}"))
            .collect::<Vec<_>>();

        for (index, msg) in expected.iter().enumerate() {
            client
                .send_bytes(msg.as_bytes().to_vec())
                .await
                .expect("Should enqueue message");

            assert!(
                wait_for(|| captured_messages(&received).len() == index + 1).await,
                "Client should receive echoed message {index}"
            );

            if index + 1 < expected.len() {
                assert!(
                    wait_for(|| client.is_reconnecting() || !client.is_active()).await,
                    "Client should observe drop after message {index}"
                );
                assert!(
                    wait_for(|| client.is_active()).await,
                    "Client should reconnect after message {index}"
                );
            }
        }

        assert_eq!(
            captured_messages(&received),
            expected,
            "Repeated reconnects should preserve message order"
        );

        client.close().await;
        assert!(client.is_closed(), "Client should close after scenario");

        Ok(())
    });

    sim.run().unwrap();
}