durable-streams-server 0.2.0

Durable Streams protocol server in Rust, built with axum and tokio
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
mod common;

use common::{spawn_test_server, test_client, unique_stream_name};

/// Validates spec: 03-read-modes.md#catch-up-mode
///
/// Verifies that GET returns 200 with data and headers.
#[tokio::test]
async fn test_read_returns_200() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream and append data
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("Hello, world!")
        .send()
        .await
        .unwrap();

    // Read data
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    assert_eq!(response.status(), 200, "Expected 200 OK");

    // Check headers
    let content_type = response
        .headers()
        .get("content-type")
        .expect("Missing Content-Type header")
        .to_str()
        .unwrap();
    assert_eq!(content_type, "text/plain");

    let next_offset = response
        .headers()
        .get("Stream-Next-Offset")
        .expect("Missing Stream-Next-Offset header")
        .to_str()
        .unwrap();
    assert_eq!(next_offset, "0000000000000001_000000000000000d");

    let up_to_date = response
        .headers()
        .get("Stream-Up-To-Date")
        .expect("Missing Stream-Up-To-Date header")
        .to_str()
        .unwrap();
    assert_eq!(up_to_date, "true");

    let etag = response
        .headers()
        .get("etag")
        .expect("Missing ETag header")
        .to_str()
        .unwrap();
    assert_eq!(etag, "\"-1:0000000000000001_000000000000000d\"");

    let cache_control = response
        .headers()
        .get("cache-control")
        .expect("Missing Cache-Control header")
        .to_str()
        .unwrap();
    assert_eq!(cache_control, "no-store");

    // Check body
    let body = response.text().await.unwrap();
    assert_eq!(body, "Hello, world!");
}

/// Validates spec: 03-read-modes.md#catch-up-mode
///
/// Verifies that reading empty stream returns empty body.
#[tokio::test]
async fn test_read_empty_stream() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create empty stream
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    // Read empty stream
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    assert_eq!(response.status(), 200);

    let body = response.text().await.unwrap();
    assert!(body.is_empty(), "Empty stream should return empty body");
}

/// Validates spec: 03-read-modes.md#catch-up-mode
///
/// Verifies that multiple messages are concatenated.
#[tokio::test]
async fn test_read_multiple_messages_concatenated() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    // Append multiple messages
    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("first")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("second")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("third")
        .send()
        .await
        .unwrap();

    // Read all messages
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let body = response.text().await.unwrap();
    assert_eq!(body, "firstsecondthird", "Messages should be concatenated");
}

/// Validates spec: 03-read-modes.md#offset-sentinels
///
/// Verifies that offset=-1 reads from start.
#[tokio::test]
async fn test_read_from_start_sentinel() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create and append
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("data")
        .send()
        .await
        .unwrap();

    // Read with explicit -1 sentinel
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}?offset=-1"))
        .send()
        .await
        .unwrap();

    let body = response.text().await.unwrap();
    assert_eq!(body, "data");
}

/// Validates spec: 03-read-modes.md#offset-sentinels
///
/// Verifies that offset=now reads from tail (empty).
#[tokio::test]
async fn test_read_from_now_sentinel() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create and append
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("data")
        .send()
        .await
        .unwrap();

    // Read from tail (now)
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}?offset=now"))
        .send()
        .await
        .unwrap();

    let up_to_date = response
        .headers()
        .get("Stream-Up-To-Date")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();

    let body = response.text().await.unwrap();
    assert!(
        body.is_empty(),
        "Reading from 'now' should return empty body"
    );
    assert_eq!(up_to_date, "true");
}

/// Validates spec: 03-read-modes.md#resumable-reads
///
/// Verifies that reads can resume from specific offset.
#[tokio::test]
async fn test_resumable_reads() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream and append messages
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("first")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("second")
        .send()
        .await
        .unwrap();

    // First read - get all data
    let response1 = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let body1 = response1.text().await.unwrap();
    assert_eq!(body1, "firstsecond");

    // Append more data
    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("third")
        .send()
        .await
        .unwrap();

    // Resume from offset after "second" (offset 0000000000000002_000000000000000b)
    let response2 = client
        .get(format!(
            "{base_url}/v1/stream/{stream_name}?offset=0000000000000002_000000000000000b"
        ))
        .send()
        .await
        .unwrap();

    let body2 = response2.text().await.unwrap();
    assert_eq!(
        body2, "third",
        "Should only return new data after resume offset"
    );
}

/// Validates spec: 03-read-modes.md#read-your-writes-consistency
///
/// Verifies that data is immediately readable after append.
#[tokio::test]
async fn test_read_your_writes() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    // Append data
    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("immediate")
        .send()
        .await
        .unwrap();

    // Immediately read - should see the data
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let body = response.text().await.unwrap();
    assert_eq!(body, "immediate", "Read-your-writes must be consistent");
}

/// Validates spec: 03-read-modes.md#stream-closed-semantics
///
/// Verifies that Stream-Closed header is present when at tail of closed stream.
#[tokio::test]
async fn test_read_closed_stream_at_tail() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    // Append and close
    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .header("Stream-Closed", "true")
        .body("final")
        .send()
        .await
        .unwrap();

    // Read from start - should get all data and see Stream-Closed
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let closed = response
        .headers()
        .get("Stream-Closed")
        .expect("Missing Stream-Closed header")
        .to_str()
        .unwrap()
        .to_string();

    let etag = response
        .headers()
        .get("etag")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();

    let body = response.text().await.unwrap();
    assert_eq!(body, "final");
    assert_eq!(closed, "true");
    assert!(
        etag.ends_with(":c\""),
        "ETag should have :c suffix for closed stream at tail"
    );
}

/// Validates spec: 03-read-modes.md#etag-and-caching
///
/// Verifies that If-None-Match returns 304 when `ETag` matches.
#[tokio::test]
async fn test_if_none_match_returns_304() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream and append
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("data")
        .send()
        .await
        .unwrap();

    // First read - get ETag
    let response1 = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let etag = response1.headers().get("etag").unwrap().to_str().unwrap();

    // Second read with If-None-Match
    let response2 = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .header("If-None-Match", etag)
        .send()
        .await
        .unwrap();

    assert_eq!(response2.status(), 304, "Expected 304 Not Modified");

    // Should still have Stream-Next-Offset and Stream-Up-To-Date
    assert!(response2.headers().get("Stream-Next-Offset").is_some());
    assert!(response2.headers().get("Stream-Up-To-Date").is_some());
}

/// Validates spec: 03-read-modes.md#catch-up-mode
///
/// Verifies that reading non-existent stream returns 404.
#[tokio::test]
async fn test_read_nonexistent_stream_returns_404() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    assert_eq!(response.status(), 404, "Expected 404 Not Found");
}

/// Validates spec: 03-read-modes.md#catch-up-mode
///
/// Verifies that invalid offset format returns 400.
#[tokio::test]
async fn test_invalid_offset_returns_400() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    // Try to read with invalid offset
    let response = client
        .get(format!("{base_url}/v1/stream/{stream_name}?offset=invalid"))
        .send()
        .await
        .unwrap();

    assert_eq!(response.status(), 400, "Expected 400 Bad Request");
}

/// Regression test: Verifies response headers match body snapshot under concurrent appends
///
/// This test prevents reintroduction of a bug where calling `storage.head()` after
/// `storage.read()` could return offsets from a newer snapshot than the body,
/// breaking resumable reads when a concurrent append lands between the calls.
#[tokio::test]
async fn test_response_headers_match_body_snapshot() {
    let (base_url, _port) = spawn_test_server().await;
    let client = test_client();
    let stream_name = unique_stream_name();

    // Create stream and append initial data
    client
        .put(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .send()
        .await
        .unwrap();

    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("message1")
        .send()
        .await
        .unwrap();

    // Read and capture response headers
    let response1 = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let next_offset1 = response1
        .headers()
        .get("Stream-Next-Offset")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();
    let etag1 = response1
        .headers()
        .get("etag")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();

    let body1 = response1.text().await.unwrap();
    assert_eq!(body1, "message1");

    // Verify next_offset points after message1
    assert_eq!(next_offset1, "0000000000000001_0000000000000008");

    // Verify ETag reflects the range we actually read
    assert_eq!(etag1, "\"-1:0000000000000001_0000000000000008\"");

    // Append more data (simulating concurrent producer)
    client
        .post(format!("{base_url}/v1/stream/{stream_name}"))
        .header("Content-Type", "text/plain")
        .body("message2")
        .send()
        .await
        .unwrap();

    // Read again from start
    let response2 = client
        .get(format!("{base_url}/v1/stream/{stream_name}"))
        .send()
        .await
        .unwrap();

    let next_offset2 = response2
        .headers()
        .get("Stream-Next-Offset")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();
    let etag2 = response2
        .headers()
        .get("etag")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();

    let body2 = response2.text().await.unwrap();
    assert_eq!(body2, "message1message2");

    // Verify next_offset now points after message2
    assert_eq!(next_offset2, "0000000000000002_0000000000000010");

    // Verify new ETag reflects both messages
    assert_eq!(etag2, "\"-1:0000000000000002_0000000000000010\"");

    // Critical: Resume from first read's next_offset should give us only message2
    let response3 = client
        .get(format!(
            "{base_url}/v1/stream/{stream_name}?offset={next_offset1}"
        ))
        .send()
        .await
        .unwrap();

    let body3 = response3.text().await.unwrap();
    assert_eq!(
        body3, "message2",
        "Resuming from first read's next_offset should return only new data, \
         proving the offset was consistent with the first read's body"
    );
}