jetstream-extra 0.3.0

Set of utilities and extensions for the JetStream NATS of the async-nats crate
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
// Copyright 2025 Synadia Communications Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

use async_nats::jetstream::{self, stream};
use futures::StreamExt;
use jetstream_extra::batch_fetch::{BatchFetchErrorKind, BatchFetchExt};
use time::OffsetDateTime;

#[tokio::test]
async fn test_batch_fetch_basic() -> Result<(), Box<dyn std::error::Error>> {
    // Start an embedded NATS server
    let server = nats_server::run_server("tests/configs/jetstream.conf");

    // Connect to the server
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    // Create a stream with some test data
    let stream_name = "TEST_STREAM";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish some test messages
    for i in 0..10 {
        context
            .publish(format!("test.{}", i), format!("message {}", i).into())
            .await?
            .await?;
    }

    // Test batch fetch
    let mut messages = context.get_batch(stream_name, 5).send().await?;

    let mut count = 0;
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        println!("Fetched message seq {}: {:?}", msg.sequence, msg.subject);
        count += 1;
    }

    assert_eq!(count, 5, "Should have fetched exactly 5 messages");

    Ok(())
}

#[tokio::test]
async fn test_batch_fetch_with_subject_filter() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_FILTER";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["events.*".to_string()],
            allow_direct: true, // Required for DIRECT.GET API
            ..Default::default()
        })
        .await?;

    // Publish messages to different subjects
    for i in 0..5 {
        context
            .publish("events.user", format!("user message {}", i).into())
            .await?
            .await?;
        context
            .publish("events.system", format!("system message {}", i).into())
            .await?
            .await?;
    }

    // Fetch only user messages
    let mut messages = context
        .get_batch(stream_name, 10)
        .subject("events.user")
        .send()
        .await?;

    let mut count = 0;
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        assert_eq!(msg.subject, "events.user".into());
        count += 1;
    }

    assert_eq!(count, 5, "Should have fetched only user messages");

    Ok(())
}

#[tokio::test]
async fn test_get_last_msgs_for() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_LAST";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["data.*".to_string()],
            allow_direct: true, // Required for DIRECT.GET API
            ..Default::default()
        })
        .await?;

    // Publish multiple messages to different subjects
    for i in 0..3 {
        context
            .publish("data.sensor1", format!("sensor1 reading {}", i).into())
            .await?
            .await?;
        context
            .publish("data.sensor2", format!("sensor2 reading {}", i).into())
            .await?
            .await?;
        context
            .publish("data.sensor3", format!("sensor3 reading {}", i).into())
            .await?
            .await?;
    }

    // Get the last message for each sensor
    let subjects = vec![
        "data.sensor1".to_string(),
        "data.sensor2".to_string(),
        "data.sensor3".to_string(),
    ];

    let mut messages = context
        .get_last_messages_for(stream_name)
        .subjects(subjects)
        .send()
        .await?;

    let mut count = 0;
    let mut subjects_found = std::collections::HashSet::new();

    while let Some(msg) = messages.next().await {
        let msg = msg?;
        subjects_found.insert(msg.subject.to_string());
        count += 1;

        // The last message for each sensor should be reading 2
        let payload = String::from_utf8(msg.payload.to_vec())?;
        assert!(
            payload.ends_with("reading 2"),
            "Should get the last message"
        );
    }

    assert_eq!(
        count, 3,
        "Should have fetched last message for each subject"
    );
    assert_eq!(subjects_found.len(), 3, "Should have all three subjects");

    Ok(())
}

#[tokio::test]
async fn test_batch_fetch_seq_and_subject() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_SEQ_SUBJECT";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish mixed messages
    // Sequence: 1=A, 2=B, 3=A, 4=B, 5=A, 6=B, 7=A, 8=B, 9=A, 10=B
    for i in 0..10 {
        let subject = if i % 2 == 0 { "test.A" } else { "test.B" };
        context
            .publish(subject, format!("message {}", i).into())
            .await?
            .await?;
    }

    // Test: Get 5 messages from sequence 5, subject test.B only
    // Should get B messages at sequences 6, 8, 10 (only 3 messages)
    let mut messages = context
        .get_batch(stream_name, 5)
        .sequence(5)
        .subject("test.B")
        .send()
        .await?;

    let mut sequences = Vec::new();
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        assert_eq!(msg.subject, "test.B".into());
        sequences.push(msg.sequence);
    }

    assert_eq!(
        sequences,
        vec![6, 8, 10],
        "Should get only B messages from seq 5+"
    );

    Ok(())
}

#[tokio::test]
async fn test_batch_fetch_sequence_range() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_SEQ";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["seq.*".to_string()],
            allow_direct: true, // Required for DIRECT.GET API
            ..Default::default()
        })
        .await?;

    // Publish 10 messages
    for i in 0..10 {
        context
            .publish("seq.msg", format!("message {}", i).into())
            .await?
            .await?;
    }

    // Fetch messages starting from sequence 5
    let mut messages = context.get_batch(stream_name, 3).sequence(5).send().await?;

    let mut sequences = Vec::new();
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        sequences.push(msg.sequence);
    }

    assert_eq!(
        sequences,
        vec![5, 6, 7],
        "Should fetch correct sequence range"
    );

    Ok(())
}

#[tokio::test]
async fn test_batch_size_limit() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_SIZE_LIMIT";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Test batch size validation - should fail with > 1000
    let result = context.get_batch(stream_name, 1001).send().await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::BatchSizeTooLarge => {
            // Expected error
            Ok(())
        }
        Err(e) => {
            panic!("Expected BatchSizeTooLarge error, got: {:?}", e);
        }
        Ok(_) => {
            panic!("Expected error for batch size > 1000");
        }
    }
}

#[tokio::test]
async fn test_time_based_fetching() -> Result<(), Box<dyn std::error::Error>> {
    use std::time::{Duration, SystemTime};

    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_TIME";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["time.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish messages with timestamps
    let _before = SystemTime::now();
    tokio::time::sleep(Duration::from_millis(100)).await;

    for i in 0..5 {
        context
            .publish("time.msg", format!("early message {}", i).into())
            .await?
            .await?;
    }

    let middle = OffsetDateTime::now_utc();
    tokio::time::sleep(Duration::from_millis(100)).await;

    for i in 0..5 {
        context
            .publish("time.msg", format!("late message {}", i).into())
            .await?
            .await?;
    }

    // Fetch messages after middle timestamp - should get only late messages
    let mut messages = context
        .get_batch(stream_name, 10)
        .start_time(middle)
        .send()
        .await?;

    let mut count = 0;
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        let payload = String::from_utf8(msg.payload.to_vec())?;
        assert!(
            payload.starts_with("late message"),
            "Should only get late messages"
        );
        count += 1;
    }

    assert_eq!(count, 5, "Should have fetched only late messages");
    Ok(())
}

#[tokio::test]
async fn test_subject_count_limit() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_SUBJECT_LIMIT";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Create more than 1024 subjects
    let mut subjects = Vec::new();
    for i in 0..1025 {
        subjects.push(format!("test.subject{}", i));
    }

    // Test subject count validation - should fail with > 1024
    let result = context
        .get_last_messages_for(stream_name)
        .subjects(subjects)
        .send()
        .await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::TooManySubjects => {
            // Expected error
            Ok(())
        }
        Err(e) => {
            panic!("Expected TooManySubjects error, got: {:?}", e);
        }
        Ok(_) => {
            panic!("Expected error for subject count > 1024");
        }
    }
}

#[tokio::test]
async fn test_invalid_parameters() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_INVALID";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Test: seq = 0 should fail
    let result = context.get_batch(stream_name, 5).sequence(0).send().await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::InvalidOption => {
            // Expected
        }
        _ => panic!("Expected InvalidOption error for seq=0"),
    }

    // Test: max_bytes = 0 should fail
    let result = context.get_batch(stream_name, 5).max_bytes(0).send().await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::InvalidOption => {
            // Expected
        }
        _ => panic!("Expected InvalidOption error for max_bytes=0"),
    }

    // Test: batch = 0 in get_last should fail
    let result = context
        .get_last_messages_for(stream_name)
        .subjects(vec!["test.A".to_string()])
        .batch(0)
        .send()
        .await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::InvalidOption => {
            // Expected
        }
        _ => panic!("Expected InvalidOption error for batch=0 in get_last"),
    }

    Ok(())
}

#[tokio::test]
async fn test_invalid_stream_name() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    // Test empty stream name for get_batch
    let result = context.get_batch("", 5).send().await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::InvalidStreamName => {
            // Expected error
        }
        Err(e) => {
            panic!(
                "Expected InvalidStreamName error for get_batch, got: {:?}",
                e
            );
        }
        Ok(_) => {
            panic!("Expected error for empty stream name in get_batch");
        }
    }

    // Test empty stream name for get_last_messages_for
    let result = context
        .get_last_messages_for("")
        .subjects(vec!["test.subject".to_string()])
        .send()
        .await;

    match result {
        Err(e) if e.kind() == BatchFetchErrorKind::InvalidStreamName => {
            // Expected error
            Ok(())
        }
        Err(e) => {
            panic!(
                "Expected InvalidStreamName error for get_last_messages_for, got: {:?}",
                e
            );
        }
        Ok(_) => {
            panic!("Expected error for empty stream name in get_last_messages_for");
        }
    }
}

#[tokio::test]
async fn test_more_than_available() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_PARTIAL";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish only 5 messages
    for i in 0..5 {
        context
            .publish("test.msg", format!("message {}", i).into())
            .await?
            .await?;
    }

    // Request 10 messages but only 5 available
    let mut messages = context
        .get_batch(stream_name, 10)
        .sequence(1)
        .send()
        .await?;

    let mut count = 0;
    while let Some(msg) = messages.next().await {
        let _msg = msg?;
        count += 1;
    }

    assert_eq!(
        count, 5,
        "Should get only 5 messages even though 10 requested"
    );
    Ok(())
}

#[tokio::test]
async fn test_seq_higher_than_available() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_HIGH_SEQ";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish 5 messages (seq 1-5)
    for i in 0..5 {
        context
            .publish("test.msg", format!("message {}", i).into())
            .await?
            .await?;
    }

    // Request starting from seq 10 (doesn't exist)
    let mut messages = context
        .get_batch(stream_name, 5)
        .sequence(10)
        .send()
        .await?;

    // Should get no messages
    let mut count = 0;
    while let Some(msg) = messages.next().await {
        match msg {
            Ok(_) => count += 1,
            Err(e) if e.kind() == BatchFetchErrorKind::NoMessages => break,
            Err(e) => return Err(e.into()),
        }
    }

    assert_eq!(
        count, 0,
        "Should get no messages when seq is higher than available"
    );
    Ok(())
}

#[tokio::test]
async fn test_get_last_with_up_to_seq() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_UP_TO_SEQ";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["data.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish messages
    // Sequences: 1,2,3 = data.A; 4,5,6 = data.B; 7,8,9 = data.A
    for i in 0..3 {
        context
            .publish("data.A", format!("A{}", i).into())
            .await?
            .await?;
    }
    for i in 0..3 {
        context
            .publish("data.B", format!("B{}", i).into())
            .await?
            .await?;
    }
    for i in 3..6 {
        context
            .publish("data.A", format!("A{}", i).into())
            .await?
            .await?;
    }

    // Get last messages up to sequence 6
    // Should get: data.A at seq 3, data.B at seq 6
    let mut messages = context
        .get_last_messages_for(stream_name)
        .subjects(vec!["data.A".to_string(), "data.B".to_string()])
        .up_to_seq(6)
        .send()
        .await?;

    let mut results = std::collections::HashMap::new();
    while let Some(msg) = messages.next().await {
        let msg = msg?;
        results.insert(msg.subject.to_string(), msg.sequence);
    }

    assert_eq!(
        results.get("data.A"),
        Some(&3),
        "Last data.A up to seq 6 should be seq 3"
    );
    assert_eq!(
        results.get("data.B"),
        Some(&6),
        "Last data.B up to seq 6 should be seq 6"
    );

    Ok(())
}

#[tokio::test]
async fn test_no_messages_match_filter() -> Result<(), Box<dyn std::error::Error>> {
    let server = nats_server::run_server("tests/configs/jetstream.conf");
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    let stream_name = "TEST_STREAM_NO_MATCH";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["data.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    // Publish messages only to data.A
    for i in 0..5 {
        context
            .publish("data.A", format!("msg{}", i).into())
            .await?
            .await?;
    }

    // Try to get last messages for data.B and data.C (don't exist)
    let mut messages = context
        .get_last_messages_for(stream_name)
        .subjects(vec!["data.B".to_string(), "data.C".to_string()])
        .send()
        .await?;

    let mut count = 0;
    while let Some(msg) = messages.next().await {
        match msg {
            Ok(_) => count += 1,
            Err(e) if e.kind() == BatchFetchErrorKind::NoMessages => break,
            Err(e) => return Err(e.into()),
        }
    }

    assert_eq!(count, 0, "Should get no messages when subjects don't match");
    Ok(())
}

#[tokio::test]
async fn test_batch_fetch_unsupported_server() -> Result<(), Box<dyn std::error::Error>> {
    // Start an embedded NATS server (current versions don't support batch get)
    let server = nats_server::run_server("tests/configs/jetstream.conf");

    // Connect to the server
    let client = async_nats::connect(server.client_url()).await?;
    let context = jetstream::new(client);

    // Create a stream
    let stream_name = "TEST_STREAM_UNSUPPORTED";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            ..Default::default()
        })
        .await?;

    // Attempt batch fetch - the error will come when trying to consume the stream
    let mut messages = context.get_batch(stream_name, 5).send().await?;

    // Try to get the first message - should fail with UnsupportedByServer
    match messages.next().await {
        Some(Err(e)) if e.kind() == BatchFetchErrorKind::UnsupportedByServer => {
            // Expected error
            Ok(())
        }
        Some(Err(e)) => {
            panic!("Expected UnsupportedByServer error, got: {:?}", e);
        }
        Some(Ok(_)) => {
            panic!("Expected error but got a message");
        }
        None => {
            panic!("Expected error but stream ended without messages");
        }
    }
}

#[tokio::test]
async fn test_timeout() -> Result<(), Box<dyn std::error::Error>> {
    // Start an embedded NATS server (current versions don't support batch get)
    let server = nats_server::run_server("tests/configs/jetstream.conf");

    // Connect to the server
    let client = async_nats::connect(server.client_url()).await?;
    let mut context = jetstream::new(client);

    // Create a stream
    let stream_name = "TIMEOUT";
    context
        .create_stream(stream::Config {
            name: stream_name.to_string(),
            subjects: vec!["test.*".to_string()],
            allow_direct: true,
            ..Default::default()
        })
        .await?;

    for i in 0..1000 {
        context
            .publish("test.msg", format!("message {}", i).into())
            .await?
            .await?;
    }

    context.set_timeout(std::time::Duration::from_nanos(1));
    let messages = context.get_batch(stream_name, 1000).send().await?;

    let results = messages.collect::<Vec<_>>().await;
    assert!(
        results
            .iter()
            .any(|r| { matches!(r, Err(e) if e.kind() == BatchFetchErrorKind::TimedOut) }),
        "Should have a timeout error"
    );

    Ok(())
}