distkit 0.4.0

A toolkit of distributed systems primitives for Rust, backed by Redis
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
use crate::{CounterComparator, counter::CounterTrait};

use super::common::{key, make_strict_counter};

/// A key that has never been written returns 0.
#[tokio::test]
async fn get_returns_zero_for_unknown_key() {
    let counter = make_strict_counter("test_get_zero").await;
    let result = counter.get(&key("absent")).await.unwrap();
    assert_eq!(result, 0);
}

/// inc by a positive amount returns the new total and the value is persisted.
#[tokio::test]
async fn inc_positive_returns_new_total() {
    let counter = make_strict_counter("test_inc_positive").await;
    let k = key("hits");

    let after_first = counter.inc(&k, 5).await.unwrap();
    assert_eq!(after_first, 5);

    let stored = counter.get(&k).await.unwrap();
    assert_eq!(stored, 5);
}

/// inc accumulates across multiple calls.
#[tokio::test]
async fn inc_accumulates() {
    let counter = make_strict_counter("test_inc_accumulates").await;
    let k = key("visits");

    counter.inc(&k, 3).await.unwrap();
    counter.inc(&k, 7).await.unwrap();
    let total = counter.inc(&k, 10).await.unwrap();

    assert_eq!(total, 20);
}

/// dec by a positive amount subtracts from the counter.
#[tokio::test]
async fn dec_positive_subtracts() {
    let counter = make_strict_counter("test_dec_positive").await;
    let k = key("stock");

    counter.inc(&k, 100).await.unwrap();
    let after_dec = counter.dec(&k, 30).await.unwrap();
    assert_eq!(after_dec, 70);
}

/// dec can drive a counter below zero (no floor).
#[tokio::test]
async fn dec_below_zero() {
    let counter = make_strict_counter("test_dec_below_zero").await;
    let k = key("balance");

    counter.inc(&k, 10).await.unwrap();
    let result = counter.dec(&k, 25).await.unwrap();
    assert_eq!(result, -15);

    let stored = counter.get(&k).await.unwrap();
    assert_eq!(stored, -15);
}

/// Chained inc and dec operations produce the correct running total.
#[tokio::test]
async fn chained_inc_and_dec() {
    let counter = make_strict_counter("test_chained").await;
    let k = key("score");

    counter.inc(&k, 10).await.unwrap(); // 10
    counter.dec(&k, 3).await.unwrap(); // 7
    counter.inc(&k, 5).await.unwrap(); // 12
    counter.dec(&k, 2).await.unwrap(); // 10

    let total = counter.get(&k).await.unwrap();
    assert_eq!(total, 10);
}

/// Two distinct member keys on the same counter are independent.
#[tokio::test]
async fn independent_keys_do_not_interfere() {
    let counter = make_strict_counter("test_independent").await;
    let k_a = key("alpha");
    let k_b = key("beta");

    counter.inc(&k_a, 42).await.unwrap();
    counter.inc(&k_b, 7).await.unwrap();

    assert_eq!(counter.get(&k_a).await.unwrap(), 42);
    assert_eq!(counter.get(&k_b).await.unwrap(), 7);
}

/// Counters with different prefixes do not share state even for the same member key.
#[tokio::test]
async fn different_prefixes_are_isolated() {
    let counter_a = make_strict_counter("test_prefix_a").await;
    let counter_b = make_strict_counter("test_prefix_b").await;
    let k = key("shared");

    counter_a.inc(&k, 100).await.unwrap();
    counter_b.inc(&k, 1).await.unwrap();

    assert_eq!(counter_a.get(&k).await.unwrap(), 100);
    assert_eq!(counter_b.get(&k).await.unwrap(), 1);
}

/// inc by zero returns the current value unchanged and does not error.
#[tokio::test]
async fn inc_by_zero_is_noop() {
    let counter = make_strict_counter("test_inc_zero").await;
    let k = key("noop");

    counter.inc(&k, 10).await.unwrap();
    let result = counter.inc(&k, 0).await.unwrap();

    assert_eq!(result, 10);
    assert_eq!(counter.get(&k).await.unwrap(), 10);
}

/// dec by zero is a no-op (dec calls inc with negated count).
#[tokio::test]
async fn dec_by_zero_is_noop() {
    let counter = make_strict_counter("test_dec_zero").await;
    let k = key("still");

    counter.inc(&k, 55).await.unwrap();
    let result = counter.dec(&k, 0).await.unwrap();

    assert_eq!(result, 55);
}

/// inc handles large i64 values without error.
#[tokio::test]
async fn inc_large_value() {
    let counter = make_strict_counter("test_large_value").await;
    let k = key("big");

    let large: i64 = 1_000_000_000_000i64;
    let result = counter.inc(&k, large).await.unwrap();
    assert_eq!(result, large);
}

/// get immediately after inc returns the same value inc returned.
#[tokio::test]
async fn get_matches_last_inc_return_value() {
    let counter = make_strict_counter("test_get_matches_inc").await;
    let k = key("counter");

    let inc_result = counter.inc(&k, 17).await.unwrap();
    let get_result = counter.get(&k).await.unwrap();

    assert_eq!(inc_result, get_result);
}

// ---------------------------------------------------------------------------
// set
// ---------------------------------------------------------------------------

/// set returns the value it was given.
#[tokio::test]
async fn set_returns_the_value_that_was_set() {
    let counter = make_strict_counter("test_set_return").await;
    let k = key("val");

    let result = counter.set(&k, 99).await.unwrap();
    assert_eq!(result, 99);
}

/// set overwrites an existing counter value.
#[tokio::test]
async fn set_overrides_existing_value() {
    let counter = make_strict_counter("test_set_override").await;
    let k = key("val");

    counter.inc(&k, 10).await.unwrap();
    counter.set(&k, 50).await.unwrap();

    assert_eq!(counter.get(&k).await.unwrap(), 50);
}

/// set to zero is a valid reset.
#[tokio::test]
async fn set_zero_resets_counter() {
    let counter = make_strict_counter("test_set_zero").await;
    let k = key("val");

    counter.inc(&k, 20).await.unwrap();
    counter.set(&k, 0).await.unwrap();

    assert_eq!(counter.get(&k).await.unwrap(), 0);
}

/// set accepts negative values.
#[tokio::test]
async fn set_negative_value() {
    let counter = make_strict_counter("test_set_negative").await;
    let k = key("val");

    counter.set(&k, -5).await.unwrap();

    assert_eq!(counter.get(&k).await.unwrap(), -5);
}

/// set works on a key that was never written before.
#[tokio::test]
async fn set_on_new_key() {
    let counter = make_strict_counter("test_set_new").await;
    let k = key("fresh");

    counter.set(&k, 77).await.unwrap();

    assert_eq!(counter.get(&k).await.unwrap(), 77);
}

// ---------------------------------------------------------------------------
// del
// ---------------------------------------------------------------------------

/// del returns the value the counter held before deletion.
#[tokio::test]
async fn del_returns_value_before_deletion() {
    let counter = make_strict_counter("test_del_return").await;
    let k = key("val");

    counter.inc(&k, 42).await.unwrap();
    let returned = counter.del(&k).await.unwrap();

    assert_eq!(returned, 42);
}

/// del on a key that was never set returns 0.
#[tokio::test]
async fn del_on_unknown_key_returns_zero() {
    let counter = make_strict_counter("test_del_unknown").await;

    let returned = counter.del(&key("ghost")).await.unwrap();
    assert_eq!(returned, 0);
}

/// after del, get returns 0.
#[tokio::test]
async fn del_removes_the_key() {
    let counter = make_strict_counter("test_del_removes").await;
    let k = key("val");

    counter.inc(&k, 10).await.unwrap();
    counter.del(&k).await.unwrap();

    assert_eq!(counter.get(&k).await.unwrap(), 0);
}

/// del only removes the targeted key; sibling keys are unaffected.
#[tokio::test]
async fn del_does_not_affect_sibling_keys() {
    let counter = make_strict_counter("test_del_sibling").await;
    let k_a = key("a");
    let k_b = key("b");

    counter.inc(&k_a, 5).await.unwrap();
    counter.inc(&k_b, 20).await.unwrap();
    counter.del(&k_a).await.unwrap();

    assert_eq!(counter.get(&k_b).await.unwrap(), 20);
}

// ---------------------------------------------------------------------------
// clear
// ---------------------------------------------------------------------------

/// clear causes all member keys to return 0 afterwards.
#[tokio::test]
async fn clear_removes_all_member_keys() {
    let counter = make_strict_counter("test_clear_all").await;
    let k_a = key("x");
    let k_b = key("y");
    let k_c = key("z");

    counter.inc(&k_a, 1).await.unwrap();
    counter.inc(&k_b, 2).await.unwrap();
    counter.inc(&k_c, 3).await.unwrap();

    counter.clear().await.unwrap();

    assert_eq!(counter.get(&k_a).await.unwrap(), 0);
    assert_eq!(counter.get(&k_b).await.unwrap(), 0);
    assert_eq!(counter.get(&k_c).await.unwrap(), 0);
}

/// clear on a prefix with no written keys does not error.
#[tokio::test]
async fn clear_on_empty_prefix_does_not_error() {
    let counter = make_strict_counter("test_clear_empty").await;
    counter.clear().await.unwrap();
}

/// clear on one prefix does not affect a counter with a different prefix.
#[tokio::test]
async fn clear_does_not_affect_other_prefixes() {
    let counter_a = make_strict_counter("test_clear_isolation_a").await;
    let counter_b = make_strict_counter("test_clear_isolation_b").await;
    let k = key("val");

    counter_a.inc(&k, 10).await.unwrap();
    counter_b.inc(&k, 99).await.unwrap();

    counter_a.clear().await.unwrap();

    assert_eq!(counter_b.get(&k).await.unwrap(), 99);
}

#[tokio::test]
async fn inc_if_uses_all_comparators() {
    let cases = [
        ("eq", CounterComparator::Eq(10), true),
        ("lt", CounterComparator::Lt(11), true),
        ("gt", CounterComparator::Gt(10), false),
        ("ne", CounterComparator::Ne(9), true),
        ("nil", CounterComparator::Nil, true),
    ];

    for (suffix, comparator, should_apply) in cases {
        let counter = make_strict_counter(&format!("test_inc_if_{suffix}")).await;
        let k = key("conditional");
        counter.set(&k, 10).await.unwrap();

        let result = counter.inc_if(&k, comparator, 2).await.unwrap();
        let expected = if should_apply { (12, 10) } else { (10, 10) };

        assert_eq!(result, expected);
        assert_eq!(counter.get(&k).await.unwrap(), expected.0);
    }
}

#[tokio::test]
async fn inc_all_empty_and_inc_all_if_empty_return_empty() {
    let counter = make_strict_counter("test_inc_all_empty").await;
    assert_eq!(counter.inc_all(&[]).await.unwrap(), vec![]);
    assert_eq!(counter.inc_all_if(&[]).await.unwrap(), vec![]);
}

#[tokio::test]
async fn inc_all_supports_duplicate_keys_sequentially() {
    let counter = make_strict_counter("test_inc_all_duplicates").await;
    let k = key("hits");

    let results = counter.inc_all(&[(&k, 1), (&k, 2)]).await.unwrap();

    assert_eq!(results, vec![(&k, 1), (&k, 3)]);
    assert_eq!(counter.get(&k).await.unwrap(), 3);
}

#[tokio::test]
async fn inc_all_if_supports_partial_success_missing_keys_and_duplicates() {
    let counter = make_strict_counter("test_inc_all_if_ordered").await;
    let k1 = key("a");
    let k2 = key("b");

    counter.set(&k1, 0).await.unwrap();
    counter.set(&k2, 10).await.unwrap();

    let results = counter
        .inc_all_if(&[
            (&k1, CounterComparator::Eq(0), 1),
            (&k1, CounterComparator::Eq(1), 2),
            (&k2, CounterComparator::Gt(20), 5),
            (&k2, CounterComparator::Nil, 3),
        ])
        .await
        .unwrap();

    assert_eq!(
        results,
        vec![(&k1, 1, 0), (&k1, 3, 1), (&k2, 10, 10), (&k2, 13, 10)]
    );
    assert_eq!(counter.get(&k1).await.unwrap(), 3);
    assert_eq!(counter.get(&k2).await.unwrap(), 13);
}

#[tokio::test]
async fn set_if_uses_all_comparators() {
    let cases = [
        ("eq", CounterComparator::Eq(10), true),
        ("lt", CounterComparator::Lt(11), true),
        ("gt", CounterComparator::Gt(10), false),
        ("ne", CounterComparator::Ne(9), true),
        ("nil", CounterComparator::Nil, true),
    ];

    for (suffix, comparator, should_apply) in cases {
        let counter = make_strict_counter(&format!("test_set_if_{suffix}")).await;
        let k = key("conditional");
        counter.set(&k, 10).await.unwrap();

        let result = counter.set_if(&k, comparator, 99).await.unwrap();
        let expected = if should_apply { (99, 10) } else { (10, 10) };

        assert_eq!(result, expected);
        assert_eq!(counter.get(&k).await.unwrap(), expected.0);
    }
}

#[tokio::test]
async fn set_all_if_supports_partial_success_and_missing_keys() {
    let counter = make_strict_counter("test_set_all_if_partial").await;
    let k1 = key("a");
    let k2 = key("b");
    let k3 = key("c");

    counter.set(&k1, 10).await.unwrap();
    counter.set(&k2, 20).await.unwrap();

    let results = counter
        .set_all_if(&[
            (&k3, CounterComparator::Nil, 30),
            (&k1, CounterComparator::Gt(5), 11),
            (&k2, CounterComparator::Lt(10), 99),
        ])
        .await
        .unwrap();

    assert_eq!(results, vec![(&k3, 30, 0), (&k1, 11, 10), (&k2, 20, 20)]);
    assert_eq!(counter.get(&k1).await.unwrap(), 11);
    assert_eq!(counter.get(&k2).await.unwrap(), 20);
    assert_eq!(counter.get(&k3).await.unwrap(), 30);
}