daaki-imap 0.2.0

An IMAP4rev1/IMAP4rev2 async client library
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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::time::Instant;

use tokio::sync::mpsc;

use super::*;
use crate::connection::typed_event::TypedEvent;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Creates a `DriverEventSink` with the given channel capacity and hard cap.
fn make_sink(channel_cap: usize, hard_cap: usize) -> (DriverEventSink, mpsc::Receiver<TypedEvent>) {
    let (tx, rx) = mpsc::channel(channel_cap);
    let sink = DriverEventSink::new(tx, Some(hard_cap));
    (sink, rx)
}

/// A resyncable event (EXISTS) tagged with a sequence number for identification.
fn resyncable_event(n: u32) -> TypedEvent {
    TypedEvent::Exists(n)
}

/// A critical event (Alert) for testing buffering behavior.
fn critical_event(msg: &str) -> TypedEvent {
    TypedEvent::Alert(msg.to_owned())
}

/// A requeryable event (`CapabilityChange`) for testing drop behavior.
fn requeryable_event() -> TypedEvent {
    TypedEvent::CapabilityChange(vec![])
}

/// Drains all available events from the receiver into a Vec.
fn drain_rx(rx: &mut mpsc::Receiver<TypedEvent>) -> Vec<TypedEvent> {
    let mut events = Vec::new();
    while let Ok(ev) = rx.try_recv() {
        events.push(ev);
    }
    events
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn basic_emit_with_capacity() {
    let (mut sink, mut rx) = make_sink(8, 16);
    sink.emit(resyncable_event(1)).unwrap();
    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    assert!(matches!(events[0], TypedEvent::Exists(1)));
}

#[test]
fn resyncable_dropped_when_full() {
    let (mut sink, _rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    // This one should be dropped (resyncable, channel full).
    sink.emit(resyncable_event(2)).unwrap();
    // No error — just silently dropped.
}

#[test]
fn requeryable_dropped_when_full() {
    let (mut sink, _rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Requeryable should be dropped.
    sink.emit(requeryable_event()).unwrap();
}

#[test]
fn critical_buffered_when_full() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Critical event should be buffered in pending.
    sink.emit(critical_event("alert1")).unwrap();
    assert_eq!(sink.pending.len(), 1);

    // Drain the channel to make room.
    drain_rx(&mut rx);
    // Now drain_pending should flush the buffered critical event.
    sink.drain_pending_nonblocking().unwrap();
    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    assert!(matches!(&events[0], TypedEvent::Alert(msg) if msg == "alert1"));
}

#[test]
fn overflow_marker_emitted_after_drops_and_drain() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop 3 resyncable events.
    sink.emit(resyncable_event(2)).unwrap();
    sink.emit(resyncable_event(3)).unwrap();
    sink.emit(resyncable_event(4)).unwrap();

    // Drain the channel to make room, then call drain_pending to emit marker.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 3);
        }
        other => panic!("expected QueueOverflow, got {other:?}"),
    }
}

#[test]
fn cumulative_counts_mixed_drops() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop 2 resyncable + 1 requeryable.
    sink.emit(resyncable_event(2)).unwrap();
    sink.emit(requeryable_event()).unwrap();
    sink.emit(resyncable_event(3)).unwrap();

    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 3); // 2 resyncable + 1 requeryable
        }
        other => panic!("expected QueueOverflow, got {other:?}"),
    }
}

#[test]
fn since_timestamp_is_from_first_drop() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();

    let before = Instant::now();
    sink.emit(resyncable_event(2)).unwrap(); // first drop
    let after_first = Instant::now();

    // Second drop — should NOT update `since`.
    sink.emit(resyncable_event(3)).unwrap();

    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { since, .. } => {
            assert!(*since >= before);
            assert!(*since <= after_first);
        }
        other => panic!("expected QueueOverflow, got {other:?}"),
    }
}

#[test]
fn no_marker_when_no_drops() {
    let (mut sink, mut rx) = make_sink(8, 16);
    sink.emit(resyncable_event(1)).unwrap();
    sink.emit(resyncable_event(2)).unwrap();

    // Drain channel and call drain_pending — should be a no-op for marker.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert!(events.is_empty(), "no marker expected, got {events:?}");
}

#[test]
fn stats_reset_after_marker_delivered() {
    let (mut sink, mut rx) = make_sink(2, 16);

    // --- Window 1: drop 2 events ---
    // Fill the channel.
    sink.emit(resyncable_event(1)).unwrap();
    sink.emit(resyncable_event(2)).unwrap();
    // These are dropped.
    sink.emit(resyncable_event(10)).unwrap();
    sink.emit(resyncable_event(11)).unwrap();

    // Drain and flush marker.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();
    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 2);
        }
        other => panic!("expected QueueOverflow window 1, got {other:?}"),
    }

    // --- Window 2: drop 1 event ---
    sink.emit(resyncable_event(20)).unwrap();
    sink.emit(resyncable_event(21)).unwrap();
    // This one is dropped.
    sink.emit(resyncable_event(22)).unwrap();

    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();
    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 1);
        }
        other => panic!("expected QueueOverflow window 2, got {other:?}"),
    }
}

#[test]
fn critical_events_drain_before_marker() {
    let (mut sink, mut rx) = make_sink(2, 16);
    // Fill channel (cap=2).
    sink.emit(resyncable_event(1)).unwrap();
    sink.emit(resyncable_event(99)).unwrap();
    // Buffer a critical event.
    sink.emit(critical_event("alert-first")).unwrap();
    // Drop a resyncable event.
    sink.emit(resyncable_event(2)).unwrap();

    // Clear channel — now drain should send critical first, then marker.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    // Should have critical event first, then the overflow marker.
    assert!(
        events.len() >= 2,
        "expected at least 2 events, got {events:?}"
    );
    assert!(
        matches!(&events[0], TypedEvent::Alert(msg) if msg == "alert-first"),
        "first event should be alert, got {:?}",
        events[0]
    );
    assert!(
        matches!(
            &events[1],
            TypedEvent::QueueOverflow {
                dropped_count: 1,
                ..
            }
        ),
        "second event should be QueueOverflow with count=1, got {:?}",
        events[1]
    );
}

#[test]
fn marker_does_not_amplify_under_sustained_backpressure() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop some events.
    sink.emit(resyncable_event(2)).unwrap();
    sink.emit(resyncable_event(3)).unwrap();

    // Call drain_pending multiple times while channel is still full.
    // The marker cannot be sent, stats should be preserved.
    sink.drain_pending_nonblocking().unwrap();
    sink.drain_pending_nonblocking().unwrap();
    sink.drain_pending_nonblocking().unwrap();

    // Now free the channel and drain.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    // Exactly one marker, not three.
    let markers: Vec<_> = events
        .iter()
        .filter(|e| matches!(e, TypedEvent::QueueOverflow { .. }))
        .collect();
    assert_eq!(
        markers.len(),
        1,
        "expected exactly 1 marker, got {markers:?}"
    );
    match markers[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 2);
        }
        _ => unreachable!(),
    }
}

#[test]
fn hard_cap_fires_for_critical_overflow() {
    let (mut sink, _rx) = make_sink(1, 2);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Buffer 2 critical events (hard_cap = 2).
    sink.emit(critical_event("c1")).unwrap();
    sink.emit(critical_event("c2")).unwrap();
    // Third critical should hit hard cap.
    let result = sink.emit(critical_event("c3"));
    assert!(
        matches!(result, Err(EventOverflow::HardCap)),
        "expected HardCap, got {result:?}"
    );
}

#[test]
fn caller_gone_when_receiver_dropped() {
    let (mut sink, rx) = make_sink(1, 16);
    drop(rx);
    let result = sink.emit(resyncable_event(1));
    assert!(
        matches!(result, Err(EventOverflow::CallerGone)),
        "expected CallerGone, got {result:?}"
    );
}

#[test]
fn caller_gone_during_drain_pending() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Buffer a critical event.
    sink.emit(critical_event("c1")).unwrap();

    // Drain channel, then drop receiver.
    drain_rx(&mut rx);
    drop(rx);

    // drain_pending should detect CallerGone when trying to flush pending.
    let result = sink.drain_pending_nonblocking();
    assert!(
        matches!(result, Err(EventOverflow::CallerGone)),
        "expected CallerGone, got {result:?}"
    );
}

#[test]
fn marker_retried_on_next_drain_after_full() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop an event to create stats.
    sink.emit(resyncable_event(2)).unwrap();

    // drain_pending with channel still full — marker can't be sent, stats preserved.
    sink.drain_pending_nonblocking().unwrap();
    assert!(
        sink.drop_stats.dropped_count() > 0,
        "stats should be preserved when marker can't be sent"
    );

    // Now free the channel.
    drain_rx(&mut rx);
    // Second drain should deliver the marker.
    sink.drain_pending_nonblocking().unwrap();
    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    assert!(
        matches!(
            &events[0],
            TypedEvent::QueueOverflow {
                dropped_count: 1,
                ..
            }
        ),
        "expected QueueOverflow with count=1, got {:?}",
        events[0]
    );
}

#[test]
fn drops_accumulate_within_one_window() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop 5 events across multiple emit calls.
    for i in 2..=6 {
        sink.emit(resyncable_event(i)).unwrap();
    }

    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 5);
        }
        other => panic!("expected QueueOverflow, got {other:?}"),
    }
}

#[test]
fn no_double_marker_from_drain_then_emit() {
    let (mut sink, mut rx) = make_sink(2, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    sink.emit(resyncable_event(2)).unwrap();
    // Drop one.
    sink.emit(resyncable_event(3)).unwrap();

    // Free one slot (channel cap=2, so one slot opens).
    rx.try_recv().unwrap();
    // drain_pending_nonblocking delivers the marker into the free slot.
    sink.drain_pending_nonblocking().unwrap();
    // Immediately emit another event — should NOT produce a second marker.
    // Free one more slot first.
    rx.try_recv().unwrap();
    sink.emit(resyncable_event(4)).unwrap();

    let events = drain_rx(&mut rx);
    let markers: Vec<_> = events
        .iter()
        .filter(|e| matches!(e, TypedEvent::QueueOverflow { .. }))
        .collect();
    assert_eq!(
        markers.len(),
        1,
        "expected exactly 1 marker total, got {markers:?}"
    );
}

#[test]
fn drain_pending_noop_when_nothing_to_do() {
    let (mut sink, mut rx) = make_sink(8, 16);
    // Nothing pending, no drops.
    sink.drain_pending_nonblocking().unwrap();
    let events = drain_rx(&mut rx);
    assert!(events.is_empty());
}

#[test]
fn caller_gone_during_marker_send() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel and drop an event to create stats.
    sink.emit(resyncable_event(1)).unwrap();
    sink.emit(resyncable_event(2)).unwrap();

    // Drain the channel so pending is empty, then drop the receiver
    // before drain_pending can send the marker.
    drain_rx(&mut rx);
    drop(rx);

    // drain_pending: pending is empty, dropped_count > 0, tries to send
    // marker via try_send — hits Closed, returns CallerGone.
    let result = sink.drain_pending_nonblocking();
    assert!(
        matches!(result, Err(EventOverflow::CallerGone)),
        "expected CallerGone during marker send, got {result:?}"
    );
}

#[test]
fn drops_accumulate_while_marker_is_deferred() {
    let (mut sink, mut rx) = make_sink(1, 16);
    // Fill channel.
    sink.emit(resyncable_event(1)).unwrap();
    // Drop 2 events.
    sink.emit(resyncable_event(2)).unwrap();
    sink.emit(resyncable_event(3)).unwrap();

    // drain_pending: channel still full, marker can't send, stats restored.
    sink.drain_pending_nonblocking().unwrap();
    assert_eq!(
        sink.drop_stats.dropped_count(),
        2,
        "stats should be preserved"
    );

    // Drop 3 more events while the marker is deferred.
    sink.emit(resyncable_event(4)).unwrap();
    sink.emit(resyncable_event(5)).unwrap();
    sink.emit(resyncable_event(6)).unwrap();
    assert_eq!(
        sink.drop_stats.dropped_count(),
        5,
        "new drops should accumulate"
    );

    // Now free the channel and drain.
    drain_rx(&mut rx);
    sink.drain_pending_nonblocking().unwrap();

    let events = drain_rx(&mut rx);
    assert_eq!(events.len(), 1);
    match &events[0] {
        TypedEvent::QueueOverflow { dropped_count, .. } => {
            assert_eq!(*dropped_count, 5, "marker should have the combined count");
        }
        other => panic!("expected QueueOverflow, got {other:?}"),
    }
}