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
/*!
`ConcurrentCachedAsyncExt`: the deduplicated short aliases over `ConcurrentCachedAsync`.
Every alias is exercised against a sharded store and checked to agree with the
`async_cache_`-prefixed method it delegates to. The aliases keep the `async_` prefix rather than
being bare `get`/`set`, because the stores that implement `ConcurrentCachedAsync` also implement
the synchronous `ConcurrentCached`, whose own alias trait `ConcurrentCachedExt` is blanket
implemented and lives in the same prelude; a bare name would be a second applicable candidate at
the call site. The final tests pin that both alias traits coexist -- through the prelude glob and
through a generic bound naming both -- which is the property the prefix buys.
The trait aliases only genuinely asynchronous operations. Size and metric introspection stays on
`ConcurrentCacheBase` (`cache_size`, `cache_is_empty`, `cache_hits`, ...), which is callable on
an async store with no extension trait imported, so there is nothing here to alias.
No Redis server required.
*/
#![cfg(feature = "async_core")]
use std::sync::atomic::{AtomicUsize, Ordering};
use cached::{ConcurrentCachedAsync, ConcurrentCachedAsyncExt, ShardedUnboundCache};
use futures::executor::block_on;
fn store() -> ShardedUnboundCache<String, u32> {
ShardedUnboundCache::builder()
.shards(2)
.build()
.expect("build ShardedUnboundCache")
}
fn key(k: &str) -> String {
k.to_string()
}
/// `async_set` returns the displaced value and `async_get` reads back what was written; both
/// agree with the `async_cache_`-prefixed methods they delegate to.
#[test]
fn async_get_and_async_set_round_trip() {
let cache = store();
block_on(async {
assert_eq!(cache.async_set(key("a"), 1).await.unwrap(), None);
assert_eq!(cache.async_get(&key("a")).await.unwrap(), Some(1));
// The alias reports the previous value on overwrite, exactly like async_cache_set.
assert_eq!(cache.async_set(key("a"), 2).await.unwrap(), Some(1));
assert_eq!(cache.async_cache_get(&key("a")).await.unwrap(), Some(2));
// A miss is None, not an error.
assert_eq!(cache.async_get(&key("absent")).await.unwrap(), None);
});
}
/// `async_remove` returns the removed value; `async_remove_entry` returns the stored key with it.
#[test]
fn async_remove_and_async_remove_entry_return_the_displaced_entry() {
let cache = store();
block_on(async {
cache.async_set(key("a"), 1).await.unwrap();
assert_eq!(cache.async_remove(&key("a")).await.unwrap(), Some(1));
assert_eq!(cache.async_remove(&key("a")).await.unwrap(), None);
cache.async_set(key("b"), 2).await.unwrap();
assert_eq!(
cache.async_remove_entry(&key("b")).await.unwrap(),
Some((key("b"), 2))
);
assert_eq!(cache.async_remove_entry(&key("b")).await.unwrap(), None);
});
}
/// `async_delete` reports whether an entry was physically removed, without returning the value.
#[test]
fn async_delete_reports_whether_an_entry_was_removed() {
let cache = store();
block_on(async {
cache.async_set(key("a"), 1).await.unwrap();
assert!(cache.async_delete(&key("a")).await.unwrap());
assert!(!cache.async_delete(&key("a")).await.unwrap());
assert_eq!(cache.async_get(&key("a")).await.unwrap(), None);
});
}
/// `async_contains` reports presence and agrees with `async_cache_contains`.
#[test]
fn async_contains_reports_presence() {
let cache = store();
block_on(async {
assert!(!cache.async_contains(&key("a")).await.unwrap());
cache.async_set(key("a"), 1).await.unwrap();
assert!(cache.async_contains(&key("a")).await.unwrap());
assert_eq!(
cache.async_contains(&key("a")).await.unwrap(),
cache.async_cache_contains(&key("a")).await.unwrap()
);
});
}
/// `async_clear` empties the store while preserving metrics; `async_reset` zeroes the metrics
/// too. The distinction is the reason both aliases exist. Metrics are read through
/// `ConcurrentCacheBase`, which the trait deliberately does not alias.
#[test]
fn async_clear_preserves_metrics_and_async_reset_zeroes_them() {
use cached::ConcurrentCacheBase;
let cache = store();
block_on(async {
cache.async_set(key("a"), 1).await.unwrap();
cache.async_get(&key("a")).await.unwrap(); // one hit
cache.async_get(&key("zzz")).await.unwrap(); // one miss
assert_eq!(cache.cache_hits(), Some(1));
assert_eq!(cache.cache_misses(), Some(1));
cache.async_clear().await.unwrap();
assert_eq!(cache.cache_size().unwrap(), Some(0));
assert_eq!(
(cache.cache_hits(), cache.cache_misses()),
(Some(1), Some(1)),
"async_clear must keep the counters"
);
cache.async_set(key("b"), 2).await.unwrap();
cache.async_reset().await.unwrap();
assert_eq!(cache.cache_size().unwrap(), Some(0));
assert_eq!(
(cache.cache_hits(), cache.cache_misses()),
(Some(0), Some(0)),
"async_reset must zero the counters"
);
});
}
/// `async_get_or_set_with` runs the initializer exactly once for a key: the first call is a miss
/// that awaits the closure and stores the result, the second is a hit served from the store.
/// Counting closure invocations proves the alias reaches the store rather than recomputing.
#[test]
fn async_get_or_set_with_runs_the_initializer_only_on_a_miss() {
use cached::ConcurrentCacheBase;
let cache = store();
let calls = AtomicUsize::new(0);
block_on(async {
let first = cache
.async_get_or_set_with(key("a"), || async {
calls.fetch_add(1, Ordering::SeqCst);
42
})
.await
.unwrap();
assert_eq!(first, 42);
assert_eq!(calls.load(Ordering::SeqCst), 1);
// The value really landed in the store, not just in the return value.
assert_eq!(cache.async_get(&key("a")).await.unwrap(), Some(42));
assert_eq!(cache.cache_size().unwrap(), Some(1));
let second = cache
.async_get_or_set_with(key("a"), || async {
calls.fetch_add(1, Ordering::SeqCst);
99
})
.await
.unwrap();
assert_eq!(second, 42, "the hit must return the stored value");
assert_eq!(
calls.load(Ordering::SeqCst),
1,
"the initializer must not run on a hit"
);
// Same observable behaviour as the method it delegates to.
let via_core = cache
.async_cache_get_or_set_with(key("a"), || async { 7 })
.await
.unwrap();
assert_eq!(via_core, 42);
});
}
/// `async_try_get_or_set_with` keeps the two error channels separate: an `Err` from the closure
/// arrives in the inner `Result` and stores nothing, while a later `Ok` is cached and then
/// served on the next call without re-running the closure.
#[test]
fn async_try_get_or_set_with_stores_only_on_success() {
use cached::ConcurrentCacheBase;
let cache = store();
let calls = AtomicUsize::new(0);
block_on(async {
let failed: Result<u32, &str> = cache
.async_try_get_or_set_with(key("a"), || async {
calls.fetch_add(1, Ordering::SeqCst);
Err("boom")
})
.await
.expect("the store itself must not error");
assert_eq!(failed, Err("boom"));
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(
cache.cache_size().unwrap(),
Some(0),
"an Err initializer must store nothing"
);
assert_eq!(cache.async_get(&key("a")).await.unwrap(), None);
let ok: Result<u32, &str> = cache
.async_try_get_or_set_with(key("a"), || async {
calls.fetch_add(1, Ordering::SeqCst);
Ok(42)
})
.await
.unwrap();
assert_eq!(ok, Ok(42));
assert_eq!(calls.load(Ordering::SeqCst), 2);
assert_eq!(cache.async_get(&key("a")).await.unwrap(), Some(42));
let cached_hit: Result<u32, &str> = cache
.async_try_get_or_set_with(key("a"), || async {
calls.fetch_add(1, Ordering::SeqCst);
Ok(99)
})
.await
.unwrap();
assert_eq!(cached_hit, Ok(42));
assert_eq!(
calls.load(Ordering::SeqCst),
2,
"the initializer must not run on a hit"
);
});
}
/// Every alias is reachable from `cached::prelude::*` with no per-trait import, on a store that
/// also implements the synchronous `ConcurrentCached` (and therefore also picks up
/// `ConcurrentCachedExt` from the same glob). If the async aliases were named bare `get`/`set`
/// this module would not compile.
#[test]
fn the_aliases_resolve_through_the_prelude_glob_alongside_the_sync_aliases() {
use cached::prelude::*;
let cache = store();
block_on(async {
// Async aliases (ConcurrentCachedAsyncExt).
assert_eq!(cache.async_set(key("a"), 1).await.unwrap(), None);
assert_eq!(cache.async_get(&key("a")).await.unwrap(), Some(1));
// Sync aliases on the same store, in the same scope (ConcurrentCachedExt), reached
// through fully-qualified syntax because the concrete type's inherent `get`/`set` take
// call-site priority.
assert_eq!(
ConcurrentCachedExt::get(&cache, &key("a")).unwrap(),
Some(1)
);
assert_eq!(ConcurrentCachedExt::set(&cache, key("b"), 2).unwrap(), None);
assert_eq!(cache.async_remove(&key("b")).await.unwrap(), Some(2));
});
}
/// A generic bound naming both alias traits at once resolves every method unambiguously.
/// Inherent methods do not apply in generic code, so this is the strictest form of the
/// no-collision property.
#[test]
fn both_alias_traits_can_be_named_in_one_generic_bound() {
async fn exercise<C>(cache: &C)
where
C: cached::ConcurrentCachedExt<String, u32> + ConcurrentCachedAsyncExt<String, u32> + Sync,
{
// Sync alias, then async alias, on the same generic value.
assert_eq!(cache.set(key("g"), 7).unwrap(), None);
assert_eq!(cache.async_get(&key("g")).await.unwrap(), Some(7));
assert_eq!(cache.async_remove(&key("g")).await.unwrap(), Some(7));
assert!(!cache.contains(&key("g")).unwrap());
assert!(!cache.async_contains(&key("g")).await.unwrap());
// The get-or-set pair is unambiguous under both bounds too, in both spellings.
assert_eq!(cache.get_or_set_with(key("g"), || 1).unwrap(), 1);
assert_eq!(
cache
.async_get_or_set_with(key("g"), || async { 2 })
.await
.unwrap(),
1,
"the sync alias already stored the value"
);
let inner: Result<u32, ()> = cache
.async_try_get_or_set_with(key("h"), || async { Ok(3) })
.await
.unwrap();
assert_eq!(inner, Ok(3));
}
let cache = store();
block_on(exercise(&cache));
}