wse 3.2.0

WebSocket engine for reactive user interfaces: topics with message recovery, presence, clustering without a broker, JWT, end-to-end encryption and signed messages.
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! The clients' outboxes on the server: a numbered message is confirmed once
//! the application took it, a repeat is confirmed again and not handed on,
//! a refused one is said, and said again to a client that comes back without
//! having heard, what the server cannot tell is marked, and a client that
//! moves to another node of the cluster is not handed on twice.

use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use tokio::net::TcpListener;
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;

/// What the application took: the type, the number and the mark.
#[derive(Debug, PartialEq)]
struct Taken {
    kind: String,
    s: u64,
    possible_repeat: bool,
}

/// Takes every message, refuses those of type `refuse` (no room); each
/// takes it `pace` (a slow application).
struct App {
    taken: mpsc::UnboundedSender<Taken>,
    connected: mpsc::UnboundedSender<String>,
    pace: Duration,
    /// The first message of type `explode` panics in the handler.
    exploded: std::sync::atomic::AtomicBool,
}

impl wse::Handler for App {
    type Connection = ();

    fn connect(&self, connect: &wse::Connect<'_>) -> Option<()> {
        let _ = self.connected.send((**connect.conn_id).clone());
        Some(())
    }

    fn message(&self, _conn: &mut (), message: wse::Inbound, _size: usize) -> bool {
        let wse::Inbound::Json(value) = message else {
            return true;
        };
        if !self.pace.is_zero() {
            std::thread::sleep(self.pace);
        }
        let kind = value["t"].as_str().unwrap_or_default().to_owned();
        if kind == "explode"
            && !self
                .exploded
                .swap(true, std::sync::atomic::Ordering::SeqCst)
        {
            panic!("the application's handler panics");
        }
        if kind == "refuse" {
            return false;
        }
        let _ = self.taken.send(Taken {
            kind,
            s: value["s"].as_u64().unwrap_or(0),
            possible_repeat: value["possible_repeat"] == true,
        });
        true
    }

    fn signed(&self, _conn: &mut (), _message: serde_json::Value, _size: usize) -> bool {
        false
    }

    fn disconnect(&self, _conn: ()) {}
}

type Client =
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;

struct Node {
    server: wse::Server,
    addr: std::net::SocketAddr,
    taken: mpsc::UnboundedReceiver<Taken>,
    connected: mpsc::UnboundedReceiver<String>,
}

async fn node() -> Node {
    node_with(wse::Config::default()).await
}

async fn node_with(config: wse::Config) -> Node {
    node_paced(config, Duration::ZERO).await
}

async fn node_paced(config: wse::Config, pace: Duration) -> Node {
    let server = wse::Server::new(config).unwrap();
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let (taken_tx, taken) = mpsc::unbounded_channel();
    let (connected_tx, connected) = mpsc::unbounded_channel();
    server
        .start(
            listener,
            App {
                taken: taken_tx,
                connected: connected_tx,
                pace,
                exploded: std::sync::atomic::AtomicBool::new(false),
            },
        )
        .unwrap();
    Node {
        server,
        addr,
        taken,
        connected,
    }
}

impl Node {
    /// A client of `user` with the outbox `outbox`, accepted: the server's
    /// answer to its hello (what was decided, and the node's name).
    async fn client(&mut self, user: &str, outbox: serde_json::Value) -> (Client, u64, String) {
        let (ws, reply) = self.answered(user, outbox).await;
        let acked = reply["acked"].as_u64().unwrap();
        let node = reply["node"].as_str().unwrap().to_owned();
        (ws, acked, node)
    }

    /// A client of `user` with the outbox `outbox`, accepted, and the
    /// server's whole answer to its hello.
    async fn answered(
        &mut self,
        user: &str,
        outbox: serde_json::Value,
    ) -> (Client, serde_json::Value) {
        self.answered_at(user, outbox, "").await
    }

    /// A client of `user` that asks for msgpack (`?format=msgpack`), as a
    /// client sending in it does.
    async fn packed_client(&mut self, user: &str, outbox: serde_json::Value) -> Client {
        self.answered_at(user, outbox, "?format=msgpack").await.0
    }

    async fn answered_at(
        &mut self,
        user: &str,
        outbox: serde_json::Value,
        query: &str,
    ) -> (Client, serde_json::Value) {
        let (mut ws, _) =
            tokio_tungstenite::connect_async(format!("ws://{}/wse{query}", self.addr))
                .await
                .unwrap();
        let hello = serde_json::json!({"t": "client_hello", "p": {"protocol_version": 2, "outbox": outbox}});
        ws.send(Message::Text(hello.to_string().into()))
            .await
            .unwrap();
        let conn_id = tokio::time::timeout(Duration::from_secs(5), self.connected.recv())
            .await
            .unwrap()
            .unwrap();
        assert!(self.server.publisher().accept(&conn_id, user).unwrap());
        let reply = next_of(&mut ws, "outbox").await;
        (ws, reply["p"].clone())
    }

    async fn took(&mut self) -> Taken {
        tokio::time::timeout(Duration::from_secs(5), self.taken.recv())
            .await
            .expect("taken within 5 s")
            .expect("the application alive")
    }

    /// The application took nothing for 300 ms.
    async fn took_nothing(&mut self) {
        let got = tokio::time::timeout(Duration::from_millis(300), self.taken.recv()).await;
        assert!(got.is_err(), "nothing taken: {got:?}");
    }
}

/// The next server message of type `kind`.
async fn next_of(ws: &mut Client, kind: &str) -> serde_json::Value {
    loop {
        let frame = tokio::time::timeout(Duration::from_secs(5), ws.next())
            .await
            .unwrap_or_else(|_| panic!("a {kind} within 5 s"))
            .expect("the connection open")
            .expect("a frame");
        if let Message::Text(text) = frame {
            let value: serde_json::Value = serde_json::from_str(&text).unwrap();
            if value["t"] == kind {
                return value;
            }
        }
    }
}

/// A numbered message, its number first, as a client sends it.
async fn send(ws: &mut Client, kind: &str, s: u64) {
    let message = format!(r#"{{"s":{s},"t":"{kind}","p":{{}},"id":"m{s}"}}"#);
    ws.send(Message::Text(message.into())).await.unwrap();
}

/// Sends message `s` of the client's outbox in msgpack, as a client does:
/// `M:` and a map whose first key is its number.
async fn send_packed(ws: &mut Client, kind: &str, s: u64) {
    fn text(out: &mut Vec<u8>, value: &str) {
        out.push(0xa0 | u8::try_from(value.len()).unwrap());
        out.extend_from_slice(value.as_bytes());
    }
    let mut frame = b"M:".to_vec();
    frame.push(0x84);
    text(&mut frame, "s");
    frame.push(0xcf);
    frame.extend_from_slice(&s.to_be_bytes());
    text(&mut frame, "t");
    text(&mut frame, kind);
    text(&mut frame, "p");
    frame.push(0x80);
    text(&mut frame, "id");
    text(&mut frame, &format!("m{s}"));
    ws.send(Message::Binary(frame.into())).await.unwrap();
}

/// Reads acks until one confirms `s`.
async fn acked(ws: &mut Client, s: u64) {
    loop {
        if next_of(ws, "ack").await["p"]["s"].as_u64() >= Some(s) {
            return;
        }
    }
}

/// Reads until the server closes the connection.
async fn closed(ws: &mut Client) {
    loop {
        let frame = tokio::time::timeout(Duration::from_secs(5), ws.next())
            .await
            .expect("closed within 5 s");
        if matches!(frame, Some(Ok(Message::Close(_))) | None | Some(Err(_))) {
            return;
        }
    }
}

/// More runs of refusals than a producer keeps (1024): refused and taken in
/// turn, so that the first refusal is let go. The highest number sent.
async fn refuse_past_the_bound(node: &mut Node, ws: &mut Client) -> u64 {
    const RUNS: u64 = 1025;
    for n in 0..RUNS {
        send(ws, "refuse", 2 * n + 1).await;
        send(ws, "m", 2 * n + 2).await;
    }
    for n in 0..RUNS {
        assert_eq!(node.took().await, taken("m", 2 * n + 2));
    }
    2 * RUNS
}

fn marked(kind: &str, s: u64) -> Taken {
    Taken {
        kind: kind.to_owned(),
        s,
        possible_repeat: true,
    }
}

fn taken(kind: &str, s: u64) -> Taken {
    Taken {
        kind: kind.to_owned(),
        s,
        possible_repeat: false,
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_message_is_taken_once_and_the_client_hears_of_each() {
    let mut node = node().await;
    let (mut ws, acked_at, _) = node
        .client("ann", serde_json::json!({"producer": "p1"}))
        .await;
    assert_eq!(acked_at, 0);

    send(&mut ws, "a", 1).await;
    send(&mut ws, "b", 2).await;
    assert_eq!(node.took().await, taken("a", 1));
    assert_eq!(node.took().await, taken("b", 2));
    acked(&mut ws, 2).await;

    // A repeat is confirmed again, not handed on.
    send(&mut ws, "b", 2).await;
    acked(&mut ws, 2).await;
    node.took_nothing().await;

    // No room: refused, and said so.
    send(&mut ws, "refuse", 3).await;
    let nack = next_of(&mut ws, "nack").await;
    assert_eq!(
        (nack["p"]["s"].as_u64(), nack["p"]["code"].as_str()),
        (Some(3), Some("busy"))
    );

    // A number skipped on the connection is refused as missing.
    send(&mut ws, "d", 5).await;
    let nack = next_of(&mut ws, "nack").await;
    assert_eq!(
        (nack["p"]["s"].as_u64(), nack["p"]["code"].as_str()),
        (Some(4), Some("missing"))
    );
    assert_eq!(node.took().await, taken("d", 5));
    acked(&mut ws, 5).await;
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_client_back_on_the_same_node_goes_on_from_what_was_taken() {
    let mut node = node().await;
    let (mut ws, _, name) = node
        .client("ann", serde_json::json!({"producer": "p2"}))
        .await;
    for s in 1..=3 {
        send(&mut ws, "m", s).await;
        assert_eq!(node.took().await, taken("m", s));
    }
    acked(&mut ws, 3).await;
    drop(ws);

    let outbox = serde_json::json!({"producer": "p2", "node": name, "written": 3});
    let (mut ws, acked_at, _) = node.client("ann", outbox).await;
    assert_eq!(acked_at, 3, "what was taken");
    // Sent again all the same: a repeat.
    send(&mut ws, "m", 3).await;
    acked(&mut ws, 3).await;
    node.took_nothing().await;
    send(&mut ws, "m", 4).await;
    assert_eq!(node.took().await, taken("m", 4));

    // The same producer of another user is another one.
    let outbox = serde_json::json!({"producer": "p2", "node": name, "written": 3});
    let (_bob, acked_at, _) = node.client("bob", outbox).await;
    assert_eq!(acked_at, 0);
    node.server.stop().await;
}

/// The client left a connection with a message on its way (its bytes held
/// up somewhere) and is back on another: told the message was not taken
/// there, it may let it run out. The old connection's late message is not
/// taken then, nor answered: that connection goes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_message_late_on_a_connection_the_client_left_is_never_taken() {
    let mut node = node().await;
    let (mut old, _, name) = node
        .client("ann", serde_json::json!({"producer": "p7"}))
        .await;
    send(&mut old, "m", 1).await;
    assert_eq!(node.took().await, taken("m", 1));
    acked(&mut old, 1).await;

    // Back on another connection, 2 written on the old one.
    let outbox = serde_json::json!({"producer": "p7", "node": name, "written": 2});
    let (mut new, reply) = node.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 1, "{reply}");
    assert!(
        reply["unsure"].as_u64().unwrap_or(0) <= 1,
        "2 told not taken: {reply}"
    );
    // 2 reaches the server on the old connection only now.
    send(&mut old, "m", 2).await;
    closed(&mut old).await;
    node.took_nothing().await;
    // Sent again on the new one, it is taken, once.
    send(&mut new, "m", 2).await;
    assert_eq!(node.took().await, taken("m", 2));
    acked(&mut new, 2).await;
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn what_the_server_cannot_tell_reaches_the_application_marked() {
    let mut node = node().await;
    // From a node that is gone, with two messages it may have taken.
    let outbox = serde_json::json!({"producer": "p3", "node": "gone", "written": 2});
    let (mut ws, acked_at, _) = node.client("ann", outbox).await;
    assert_eq!(acked_at, 0);
    for s in 1..=3 {
        send(&mut ws, "m", s).await;
    }
    let marked = |s, possible_repeat| Taken {
        kind: "m".to_owned(),
        s,
        possible_repeat,
    };
    assert_eq!(node.took().await, marked(1, true));
    assert_eq!(node.took().await, marked(2, true));
    assert_eq!(node.took().await, marked(3, false));
    acked(&mut ws, 3).await;
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_refusal_the_client_did_not_hear_of_is_told_when_it_comes_back() {
    let mut node = node().await;
    let (mut ws, _, name) = node
        .client("ann", serde_json::json!({"producer": "p6"}))
        .await;
    send(&mut ws, "m", 1).await;
    assert_eq!(node.took().await, taken("m", 1));
    send(&mut ws, "refuse", 2).await;
    send(&mut ws, "m", 3).await;
    assert_eq!(node.took().await, taken("m", 3));
    // Gone before the answers came.
    drop(ws);

    // Back, still waiting to hear of 2 and 3.
    let outbox = serde_json::json!({"producer": "p6", "node": name, "written": 3, "from": 2});
    let (mut ws, reply) = node.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 3);
    assert_eq!(
        reply["refused"],
        serde_json::json!([{"s": 2, "code": "busy"}]),
        "2 was refused, 3 taken"
    );
    // Sent again all the same: refused again, never handed on.
    send(&mut ws, "m", 2).await;
    let nack = next_of(&mut ws, "nack").await;
    assert_eq!(
        (nack["p"]["s"].as_u64(), nack["p"]["code"].as_str()),
        (Some(2), Some("busy"))
    );
    node.took_nothing().await;
    drop(ws);

    // A client that heard of it is not told again.
    let outbox = serde_json::json!({"producer": "p6", "node": name, "written": 3, "from": 4});
    let (_ws, reply) = node.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 3);
    assert!(reply.get("refused").is_none(), "{reply}");
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_msgpack_message_over_the_rate_limit_is_refused_at_once() {
    let mut config = wse::Config::default();
    config.rate_limit_capacity = 2.0;
    config.rate_limit_refill = 0.01;
    let mut node = node_with(config).await;
    let mut ws = node
        .packed_client("ann", serde_json::json!({"producer": "p9"}))
        .await;
    for s in 1..=4 {
        send_packed(&mut ws, "m", s).await;
    }
    // Nothing comes after 4: its refusal does not wait for a later message.
    let mut refused = Vec::new();
    while refused.last() != Some(&4) {
        let nack = next_of(&mut ws, "nack").await;
        assert_eq!(nack["p"]["code"], "rate_limited", "{nack}");
        let to = nack["p"]["s"].as_u64().unwrap();
        let from = nack["p"]["from"].as_u64().unwrap_or(to);
        refused.extend(from..=to);
    }
    let first = refused[0];
    assert_eq!(refused, (first..=4).collect::<Vec<_>>());
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_message_over_the_rate_limit_is_refused_at_once() {
    let mut config = wse::Config::default();
    config.rate_limit_capacity = 2.0;
    config.rate_limit_refill = 0.01;
    let mut node = node_with(config).await;
    let (mut ws, _, _) = node
        .client("ann", serde_json::json!({"producer": "p8"}))
        .await;
    for s in 1..=4 {
        send(&mut ws, "m", s).await;
    }
    // Nothing comes after 4: its refusal does not wait for a later message.
    let mut refused = Vec::new();
    while refused.last() != Some(&4) {
        let nack = next_of(&mut ws, "nack").await;
        assert_eq!(nack["p"]["code"], "rate_limited", "{nack}");
        let to = nack["p"]["s"].as_u64().unwrap();
        let from = nack["p"]["from"].as_u64().unwrap_or(to);
        refused.extend(from..=to);
    }
    // What the bucket held was taken, the rest refused, in order.
    let first = refused[0];
    assert_eq!(refused, (first..=4).collect::<Vec<_>>());
    for s in 1..first {
        assert_eq!(node.took().await, taken("m", s));
    }
    node.took_nothing().await;
    node.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn without_an_outbox_a_number_is_the_applications_own() {
    let mut node = node().await;
    let (mut ws, _) = tokio_tungstenite::connect_async(format!("ws://{}/wse", node.addr))
        .await
        .unwrap();
    let hello = serde_json::json!({"t": "client_hello", "p": {"protocol_version": 2}});
    ws.send(Message::Text(hello.to_string().into()))
        .await
        .unwrap();
    let conn_id = tokio::time::timeout(Duration::from_secs(5), node.connected.recv())
        .await
        .unwrap()
        .unwrap();
    assert!(node.server.publisher().accept(&conn_id, "ann").unwrap());
    send(&mut ws, "score", 3).await;
    send(&mut ws, "score", 3).await;
    // Both taken as they are, and no answer the client did not ask for.
    assert_eq!(node.took().await, taken("score", 3));
    assert_eq!(node.took().await, taken("score", 3));
    let answered = tokio::time::timeout(Duration::from_millis(300), next_of(&mut ws, "ack")).await;
    assert!(answered.is_err(), "no ack: {answered:?}");
    node.server.stop().await;
}

/// Links `a` and `b` (on their WebSocket ports) and waits for the link.
async fn link(a: &Node, b: &Node) {
    let secret = b"a-cluster-secret-of-at-least-32-bytes!".to_vec();
    for (node, peer) in [(a, b), (b, a)] {
        let mut settings = wse::ClusterSettings::default();
        settings.peers = vec![peer.addr.to_string()];
        settings.single_port = true;
        settings.secret = Some(secret.clone());
        node.server.connect_cluster(settings).unwrap();
    }
    for _ in 0..250 {
        if !a.server.publisher().cluster_peers().is_empty()
            && !b.server.publisher().cluster_peers().is_empty()
        {
            return;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    panic!("the nodes did not link");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_client_that_moves_to_another_node_is_not_handed_on_twice() {
    let mut a = node().await;
    let mut b = node().await;
    link(&a, &b).await;

    let (mut on_a, _, name_a) = a.client("ann", serde_json::json!({"producer": "p4"})).await;
    for s in 1..=3 {
        send(&mut on_a, "m", s).await;
        assert_eq!(a.took().await, taken("m", s));
    }
    // 3 was taken on A, its ack not heard: the client goes to B with it.
    let outbox = serde_json::json!({"producer": "p4", "node": name_a, "written": 3});
    let (mut on_b, acked_at, _) = b.client("ann", outbox).await;
    assert_eq!(acked_at, 3, "B asked A");
    send(&mut on_b, "m", 3).await;
    acked(&mut on_b, 3).await;
    b.took_nothing().await;
    send(&mut on_b, "m", 4).await;
    assert_eq!(b.took().await, taken("m", 4));

    // A handed the producer over: its stale connection takes nothing more.
    send(&mut on_a, "m", 5).await;
    a.took_nothing().await;
    a.server.stop().await;
    b.server.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_refusal_goes_with_the_client_to_another_node() {
    let mut a = node().await;
    let mut b = node().await;
    link(&a, &b).await;

    let (mut on_a, _, name_a) = a.client("ann", serde_json::json!({"producer": "p7"})).await;
    send(&mut on_a, "refuse", 1).await;
    send(&mut on_a, "m", 2).await;
    assert_eq!(a.took().await, taken("m", 2));
    // The answers not heard: the client goes to B with both.
    let outbox = serde_json::json!({"producer": "p7", "node": name_a, "written": 2, "from": 1});
    let (_on_b, reply) = b.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 2, "B asked A");
    assert_eq!(
        reply["refused"],
        serde_json::json!([{"s": 1, "code": "busy"}])
    );
    b.took_nothing().await;
    a.server.stop().await;
    b.server.stop().await;
}

/// A drained server answers what it took before it closes: the client
/// hears of every message the application took.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_drain_answers_what_was_taken_before_the_close() {
    let mut node = node().await;
    let (mut ws, _, _) = node
        .client("ann", serde_json::json!({"producer": "p5"}))
        .await;
    for s in 1..=50 {
        send(&mut ws, "m", s).await;
    }
    let mut taken = 0;
    while taken < 50 {
        node.took().await;
        taken += 1;
    }
    let server = node.server;
    let drained = tokio::spawn(async move {
        let _ = server.drain(4300, "draining", Duration::from_secs(5)).await;
        server
    });
    let mut highest = 0;
    loop {
        let frame = tokio::time::timeout(Duration::from_secs(5), ws.next())
            .await
            .expect("a frame within 5 s");
        match frame {
            Some(Ok(Message::Text(text))) => {
                let value: serde_json::Value = serde_json::from_str(&text).unwrap();
                if value["t"] == "ack" {
                    highest = highest.max(value["p"]["s"].as_u64().unwrap());
                }
            }
            Some(Ok(Message::Close(_))) | None | Some(Err(_)) => break,
            Some(Ok(_)) => {}
        }
    }
    assert_eq!(
        highest, 50,
        "every message taken was confirmed before the close"
    );
    drained.await.unwrap().stop().await;
}

/// A drained server stops taking the messages of a client that keeps
/// sending, and answers what it took: the client hears of each one.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_drain_answers_a_client_that_keeps_sending() {
    // An application slower than the client: frames wait for it.
    let mut node = node_paced(wse::Config::default(), Duration::from_micros(20)).await;
    let (ws, _, _) = node
        .client("ann", serde_json::json!({"producer": "p6"}))
        .await;
    let (mut sink, mut stream) = ws.split();
    // It sends with no pause until its connection goes, 64 at a time: the
    // server always has a frame waiting.
    let sender = tokio::spawn(async move {
        for s in 1u64.. {
            let message = format!(r#"{{"s":{s},"t":"m","p":{{}},"id":"m{s}"}}"#);
            let sent = if s % 64 == 0 {
                sink.send(Message::Text(message.into())).await
            } else {
                sink.feed(Message::Text(message.into())).await
            };
            if sent.is_err() {
                return;
            }
        }
    });
    let mut last_taken = 0;
    while last_taken < 200 {
        last_taken = node.took().await.s;
    }
    let server = node.server;
    let drained = tokio::spawn(async move {
        let _ = server.drain(4300, "draining", Duration::from_secs(5)).await;
        server
    });
    let mut highest = 0;
    loop {
        let frame = tokio::time::timeout(Duration::from_secs(5), stream.next())
            .await
            .expect("a frame within 5 s");
        match frame {
            Some(Ok(Message::Text(text))) => {
                let value: serde_json::Value = serde_json::from_str(&text).unwrap();
                if value["t"] == "ack" {
                    highest = highest.max(value["p"]["s"].as_u64().unwrap());
                }
            }
            Some(Ok(Message::Close(_))) | None | Some(Err(_)) => break,
            Some(Ok(_)) => {}
        }
    }
    sender.abort();
    let _ = sender.await;
    drop(stream);
    while let Ok(Some(taken)) =
        tokio::time::timeout(Duration::from_millis(300), node.taken.recv()).await
    {
        last_taken = last_taken.max(taken.s);
    }
    assert!(
        highest >= last_taken,
        "taken up to {last_taken}, confirmed up to {highest}"
    );
    drained.await.unwrap().stop().await;
}

/// A message whose handler panicked is not said taken: the client sends it
/// again on its next connection, and the application gets it marked as a
/// possible repeat.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_message_whose_handler_panicked_comes_again_marked() {
    let mut node = node().await;
    let (mut ws, _, _) = node
        .client("ann", serde_json::json!({"producer": "p7"}))
        .await;
    send(&mut ws, "m", 1).await;
    assert_eq!(node.took().await, taken("m", 1));
    acked(&mut ws, 1).await;
    send(&mut ws, "explode", 2).await;
    // Its handler panicked: the connection closes.
    closed(&mut ws).await;
    let (mut ws, acked_at, _) = node
        .client(
            "ann",
            serde_json::json!({"producer": "p7", "written": 2, "from": 2}),
        )
        .await;
    assert_eq!(acked_at, 1, "2 was never decided");
    send(&mut ws, "explode", 2).await;
    assert_eq!(
        node.took().await,
        Taken {
            kind: "explode".to_owned(),
            s: 2,
            possible_repeat: true,
        }
    );
    acked(&mut ws, 2).await;
    node.server.stop().await;
}

/// A refusal let go for the bound cannot be told from a take: a client that
/// comes back still waiting for it is not told it was taken, sends it
/// again, and the application gets it marked as a possible repeat.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_refusal_let_go_for_the_bound_is_never_told_taken() {
    let mut node = node().await;
    let (mut ws, _, name) = node
        .client("ann", serde_json::json!({"producer": "p10"}))
        .await;
    let last = refuse_past_the_bound(&mut node, &mut ws).await;
    // Gone before the answers came: back, waiting for all of them.
    drop(ws);
    let outbox = serde_json::json!({"producer": "p10", "node": name, "written": last, "from": 1});
    let (mut ws, reply) = node.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 0, "the first one may have been refused");
    send(&mut ws, "m", 1).await;
    assert_eq!(node.took().await, marked("m", 1));
    // One the server still knows of is answered as before.
    send(&mut ws, "m", 3).await;
    let nack = next_of(&mut ws, "nack").await;
    assert_eq!(
        (nack["p"]["s"].as_u64(), nack["p"]["code"].as_str()),
        (Some(3), Some("busy"))
    );
    node.took_nothing().await;
    node.server.stop().await;
}

/// The same on another node: what the first let go goes with the client.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_refusal_let_go_for_the_bound_is_never_told_taken_on_another_node() {
    let mut a = node().await;
    let mut b = node().await;
    link(&a, &b).await;
    let (mut on_a, _, name_a) = a
        .client("ann", serde_json::json!({"producer": "p11"}))
        .await;
    let last = refuse_past_the_bound(&mut a, &mut on_a).await;
    let outbox = serde_json::json!({"producer": "p11", "node": name_a, "written": last, "from": 1});
    let (mut on_b, reply) = b.answered("ann", outbox).await;
    assert_eq!(reply["acked"], 0, "the first one may have been refused");
    send(&mut on_b, "m", 1).await;
    assert_eq!(b.took().await, marked("m", 1));
    send(&mut on_b, "m", 2).await;
    acked(&mut on_b, 2).await;
    b.took_nothing().await;
    a.server.stop().await;
    b.server.stop().await;
}

/// A message whose handler panicked may have done its work: on the node the
/// client moves to, it comes marked as a possible repeat, never as a first
/// delivery.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_message_whose_handler_panicked_comes_marked_on_another_node() {
    let mut a = node().await;
    let mut b = node().await;
    link(&a, &b).await;
    let (mut on_a, _, name_a) = a
        .client("ann", serde_json::json!({"producer": "p12"}))
        .await;
    send(&mut on_a, "m", 1).await;
    assert_eq!(a.took().await, taken("m", 1));
    acked(&mut on_a, 1).await;
    send(&mut on_a, "explode", 2).await;
    closed(&mut on_a).await;
    let outbox = serde_json::json!({"producer": "p12", "node": name_a, "written": 2, "from": 2});
    let (mut on_b, acked_at, _) = b.client("ann", outbox).await;
    assert_eq!(acked_at, 1, "2 was never decided");
    // Sent again; the server knows it by its number.
    send(&mut on_b, "m", 2).await;
    assert_eq!(b.took().await, marked("m", 2));
    acked(&mut on_b, 2).await;
    a.server.stop().await;
    b.server.stop().await;
}

/// What a node that is gone may have taken stays unsure here, whatever
/// connection of the client comes: the first time each of those comes, on a
/// later connection too, it is marked as a possible repeat.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn what_the_server_cannot_tell_stays_marked_on_the_next_connection() {
    let mut node = node().await;
    let outbox = serde_json::json!({"producer": "p13", "node": "gone", "written": 2});
    let (mut ws, acked_at, name) = node.client("ann", outbox).await;
    assert_eq!(acked_at, 0);
    send(&mut ws, "m", 1).await;
    assert_eq!(node.took().await, marked("m", 1));
    acked(&mut ws, 1).await;
    ws.close(None).await.ok();
    // Back here, the node it names now, with 2 still waiting.
    let outbox = serde_json::json!({"producer": "p13", "node": name, "from": 2, "written": 2});
    let (mut ws, acked_at, _) = node.client("ann", outbox).await;
    assert_eq!(acked_at, 1);
    send(&mut ws, "m", 2).await;
    assert_eq!(node.took().await, marked("m", 2));
    send(&mut ws, "m", 3).await;
    assert_eq!(node.took().await, taken("m", 3));
    node.server.stop().await;
}

/// What a node could not tell of a client's outbox goes with it to the node
/// it moves to: there too the first time each of those comes it is marked.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn what_a_node_could_not_tell_goes_with_the_client_to_another_node() {
    let mut a = node().await;
    let mut b = node().await;
    link(&a, &b).await;
    let outbox = serde_json::json!({"producer": "p14", "node": "gone", "written": 3});
    let (mut on_a, _, name_a) = a.client("ann", outbox).await;
    send(&mut on_a, "m", 1).await;
    assert_eq!(a.took().await, marked("m", 1));
    acked(&mut on_a, 1).await;
    on_a.close(None).await.ok();
    let outbox = serde_json::json!({"producer": "p14", "node": name_a, "from": 2, "written": 3});
    let (mut on_b, acked_at, _) = b.client("ann", outbox).await;
    assert_eq!(acked_at, 1, "B asked A");
    for s in 2..=4 {
        send(&mut on_b, "m", s).await;
    }
    assert_eq!(b.took().await, marked("m", 2));
    assert_eq!(b.took().await, marked("m", 3));
    assert_eq!(b.took().await, taken("m", 4));
    a.server.stop().await;
    b.server.stop().await;
}