11 | 11 |
"time"
|
12 | 12 |
)
|
13 | 13 |
|
14 | |
func ensureErrorL(t *testing.T, swr *Simple, key, expectedError string) {
|
15 | |
value, err := swr.Query(key)
|
|
14 |
func ensureErrorL(t *testing.T, querier Querier, key, expectedError string) {
|
|
15 |
value, err := querier.Query(key)
|
16 | 16 |
if value != nil {
|
17 | 17 |
t.Errorf("Actual: %v; Expected: %v", value, nil)
|
18 | 18 |
}
|
|
21 | 21 |
}
|
22 | 22 |
}
|
23 | 23 |
|
24 | |
func ensureValueL(t *testing.T, swr *Simple, key string, expectedValue uint64) {
|
25 | |
value, err := swr.Query(key)
|
|
24 |
func ensureValueL(t *testing.T, querier Querier, key string, expectedValue uint64) {
|
|
25 |
value, err := querier.Query(key)
|
26 | 26 |
if value.(uint64) != expectedValue {
|
27 | 27 |
t.Errorf("Actual: %d; Expected: %d", value, expectedValue)
|
28 | 28 |
}
|
|
36 | 36 |
func TestSimpleSynchronousLookupWhenMiss(t *testing.T) {
|
37 | 37 |
var invoked uint64
|
38 | 38 |
swr, err := NewSimple(&Config{Lookup: func(_ string) (interface{}, error) {
|
39 | |
atomic.AddUint64(&invoked, 1)
|
|
39 |
invoked++
|
40 | 40 |
return uint64(42), nil
|
41 | 41 |
}})
|
42 | 42 |
if err != nil {
|
|
46 | 46 |
|
47 | 47 |
ensureValueL(t, swr, "miss", 42)
|
48 | 48 |
|
49 | |
if actual, expected := atomic.AddUint64(&invoked, 0), uint64(1); actual != expected {
|
|
49 |
if actual, expected := invoked, uint64(1); actual != expected {
|
50 | 50 |
t.Errorf("Actual: %d; Expected: %d", actual, expected)
|
51 | 51 |
}
|
52 | 52 |
}
|
|
78 | 78 |
defer func() { _ = swr.Close() }()
|
79 | 79 |
|
80 | 80 |
// NOTE: storing a value that expires one minute in the future
|
81 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Expiry: time.Now().Add(time.Minute)})
|
82 | |
|
83 | |
ensureValueL(t, swr, "hit", 13)
|
|
81 |
now := time.Now()
|
|
82 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Expiry: now.Add(time.Minute)})
|
|
83 |
|
|
84 |
ensureValueL(t, swr, "hit", 13)
|
|
85 |
}
|
|
86 |
|
|
87 |
func TestSimpleStaleExpireLoadReturnsFalse(t *testing.T) {
|
|
88 |
swr, err := NewSimple(nil)
|
|
89 |
if err != nil {
|
|
90 |
t.Fatal(err)
|
|
91 |
}
|
|
92 |
defer func() { _ = swr.Close() }()
|
|
93 |
|
|
94 |
now := time.Now()
|
|
95 |
swr.Store("expired", &TimedValue{Value: uint64(42), Created: now, Expiry: now.Add(-time.Minute)})
|
|
96 |
|
|
97 |
value, ok := swr.Load("expired")
|
|
98 |
|
|
99 |
if got, want := ok, false; got != want {
|
|
100 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
101 |
}
|
|
102 |
|
|
103 |
if value != nil {
|
|
104 |
t.Errorf("GOT: %v; WANT: %v", value, nil)
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
func TestSimpleStaleExpireLoadTimedValueReturnsExpiredValue(t *testing.T) {
|
|
109 |
swr, err := NewSimple(nil)
|
|
110 |
if err != nil {
|
|
111 |
t.Fatal(err)
|
|
112 |
}
|
|
113 |
defer func() { _ = swr.Close() }()
|
|
114 |
|
|
115 |
now := time.Now()
|
|
116 |
swr.Store("expired", &TimedValue{Value: uint64(42), Created: now, Expiry: now.Add(-time.Minute)})
|
|
117 |
|
|
118 |
tv := swr.LoadTimedValue("expired")
|
|
119 |
|
|
120 |
if got, want := tv.IsExpired(), true; got != want {
|
|
121 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
122 |
}
|
|
123 |
|
|
124 |
if got, want := tv.Value, uint64(42); got != want {
|
|
125 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
126 |
}
|
84 | 127 |
}
|
85 | 128 |
|
86 | 129 |
func TestSimpleNoStaleExpireSynchronousLookupWhenAfterExpire(t *testing.T) {
|
|
95 | 138 |
defer func() { _ = swr.Close() }()
|
96 | 139 |
|
97 | 140 |
// NOTE: storing a value that expired one minute ago
|
98 | |
swr.Store("hit", &TimedValue{Value: uint64(42), Err: nil, Expiry: time.Now().Add(-time.Minute)})
|
|
141 |
now := time.Now()
|
|
142 |
swr.Store("hit", &TimedValue{Value: uint64(42), Err: nil, Created: now, Expiry: now.Add(-time.Minute)})
|
99 | 143 |
|
100 | 144 |
ensureValueL(t, swr, "hit", 42)
|
101 | 145 |
|
|
116 | 160 |
defer func() { _ = swr.Close() }()
|
117 | 161 |
|
118 | 162 |
// NOTE: storing a value that goes stale one minute in the future
|
119 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Stale: time.Now().Add(time.Minute)})
|
|
163 |
now := time.Now()
|
|
164 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Stale: now.Add(time.Minute)})
|
120 | 165 |
|
121 | 166 |
ensureValueL(t, swr, "hit", 13)
|
122 | 167 |
}
|
|
136 | 181 |
defer func() { _ = swr.Close() }()
|
137 | 182 |
|
138 | 183 |
// NOTE: storing a value that went stale one minute ago
|
139 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Stale: time.Now().Add(-time.Minute)})
|
|
184 |
now := time.Now()
|
|
185 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Stale: now.Add(-time.Minute)})
|
140 | 186 |
|
141 | 187 |
wg.Add(1)
|
142 | 188 |
ensureValueL(t, swr, "hit", 13)
|
|
164 | 210 |
defer func() { _ = swr.Close() }()
|
165 | 211 |
|
166 | 212 |
// NOTE: storing a value that goes stale one minute in the future and expires one hour in the future
|
167 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Stale: time.Now().Add(time.Minute), Expiry: time.Now().Add(time.Hour)})
|
|
213 |
now := time.Now()
|
|
214 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Stale: now.Add(time.Minute), Expiry: now.Add(time.Hour)})
|
168 | 215 |
|
169 | 216 |
ensureValueL(t, swr, "hit", 13)
|
170 | 217 |
}
|
|
183 | 230 |
defer func() { _ = swr.Close() }()
|
184 | 231 |
|
185 | 232 |
// NOTE: storing a value that went stale one minute ago and expires one minute in the future
|
186 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Stale: time.Now().Add(-time.Minute), Expiry: time.Now().Add(time.Minute)})
|
|
233 |
now := time.Now()
|
|
234 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Stale: now.Add(-time.Minute), Expiry: now.Add(time.Minute)})
|
187 | 235 |
|
188 | 236 |
// expect to receive the old value back immediately, then expect lookup to be asynchronously invoked
|
189 | 237 |
wg.Add(1)
|
|
211 | 259 |
defer func() { _ = swr.Close() }()
|
212 | 260 |
|
213 | 261 |
// NOTE: storing a value that went stale one hour ago and expired one minute ago
|
214 | |
swr.Store("hit", &TimedValue{Value: uint64(42), Err: nil, Stale: time.Now().Add(-time.Hour), Expiry: time.Now().Add(-time.Minute)})
|
|
262 |
now := time.Now()
|
|
263 |
swr.Store("hit", &TimedValue{Value: uint64(42), Err: nil, Created: now, Stale: now.Add(-time.Hour), Expiry: now.Add(-time.Minute)})
|
215 | 264 |
|
216 | 265 |
ensureValueL(t, swr, "hit", 42)
|
217 | 266 |
|
|
234 | 283 |
defer func() { _ = swr.Close() }()
|
235 | 284 |
|
236 | 285 |
// NOTE: storing a value that went stale one minute ago
|
237 | |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Stale: time.Now().Add(-time.Minute)})
|
|
286 |
now := time.Now()
|
|
287 |
swr.Store("hit", &TimedValue{Value: uint64(13), Err: nil, Created: now, Stale: now.Add(-time.Minute)})
|
238 | 288 |
|
239 | 289 |
wg.Add(1)
|
240 | 290 |
ensureValueL(t, swr, "hit", 13)
|
|
265 | 315 |
defer func() { _ = swr.Close() }()
|
266 | 316 |
|
267 | 317 |
// NOTE: storing a value that went stale one minute ago
|
268 | |
swr.Store("hit", &TimedValue{Value: nil, Err: errors.New("original error"), Stale: time.Now().Add(-time.Minute)})
|
|
318 |
now := time.Now()
|
|
319 |
swr.Store("hit", &TimedValue{Value: nil, Err: errors.New("original error"), Created: now, Stale: now.Add(-time.Minute)})
|
269 | 320 |
|
270 | 321 |
wg.Add(1)
|
271 | 322 |
ensureErrorL(t, swr, "hit", "new error")
|
|
291 | 342 |
defer func() { _ = swr.Close() }()
|
292 | 343 |
|
293 | 344 |
// NOTE: storing a value is already stale, but will expire during the fetch
|
294 | |
swr.Store("hit", &TimedValue{Value: nil, Err: errors.New("original error"), Stale: time.Now().Add(-time.Hour), Expiry: time.Now().Add(5 * time.Millisecond)})
|
|
345 |
now := time.Now()
|
|
346 |
swr.Store("hit", &TimedValue{Value: nil, Err: errors.New("original error"), Created: now, Stale: now.Add(-time.Hour), Expiry: now.Add(5 * time.Millisecond)})
|
295 | 347 |
|
296 | 348 |
wg.Add(1)
|
297 | 349 |
ensureErrorL(t, swr, "hit", "original error")
|
|
316 | 368 |
defer func() { _ = swr.Close() }()
|
317 | 369 |
|
318 | 370 |
swr.Store("no expiry", "shall not expire")
|
319 | |
swr.Store("stale value", TimedValue{Value: "stale value", Stale: time.Now().Add(-time.Minute)})
|
320 | |
swr.Store("expired value", TimedValue{Value: "expired value", Expiry: time.Now().Add(-time.Minute)})
|
|
371 |
swr.Store("expired value", TimedValue{Value: "expired value", Created: time.Now(), Expiry: time.Now().Add(-time.Minute)})
|
|
372 |
swr.Store("stale value", TimedValue{Value: "stale value", Created: time.Now(), Stale: time.Now().Add(-time.Minute)})
|
|
373 |
swr.Store("will update expiry", "soon to be expired")
|
|
374 |
|
|
375 |
swr.Store("will update stale", TimedValue{Value: "stale value", Created: time.Now(), Stale: time.Now().Add(-time.Minute)})
|
|
376 |
// make sure already stale
|
|
377 |
if got, want := swr.LoadTimedValue("will update stale").IsStale(), true; got != want {
|
|
378 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
379 |
}
|
321 | 380 |
|
322 | 381 |
called := make(map[string]struct{})
|
323 | 382 |
swr.Range(func(key string, value *TimedValue) {
|
324 | 383 |
called[key] = struct{}{}
|
325 | 384 |
swr.Store(strconv.Itoa(rand.Intn(50)), "make sure we can invoke methods that require locking")
|
|
385 |
switch key {
|
|
386 |
case "will update stale":
|
|
387 |
value.Stale = time.Now().Add(time.Minute)
|
|
388 |
case "will update expiry":
|
|
389 |
value.Expiry = time.Now().Add(-time.Minute)
|
|
390 |
}
|
326 | 391 |
})
|
327 | 392 |
|
328 | 393 |
if _, ok := called["no expiry"]; !ok {
|
|
333 | 398 |
}
|
334 | 399 |
if _, ok := called["expired value"]; ok {
|
335 | 400 |
t.Errorf("Actual: %#v; Expected: %#v", ok, false)
|
|
401 |
}
|
|
402 |
if _, ok := called["will update stale"]; !ok {
|
|
403 |
t.Errorf("Actual: %#v; Expected: %#v", ok, true)
|
|
404 |
}
|
|
405 |
if got, want := swr.LoadTimedValue("will update stale").IsStale(), false; got != want {
|
|
406 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
407 |
}
|
|
408 |
if got, want := swr.LoadTimedValue("will update expiry").IsExpired(), true; got != want {
|
|
409 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
410 |
}
|
|
411 |
|
|
412 |
swr.Store("ensure range released top level lock", struct{}{})
|
|
413 |
}
|
|
414 |
|
|
415 |
func TestSimpleRangeBreak(t *testing.T) {
|
|
416 |
swr, err := NewSimple(nil)
|
|
417 |
if err != nil {
|
|
418 |
t.Fatal(err)
|
|
419 |
}
|
|
420 |
defer func() { _ = swr.Close() }()
|
|
421 |
|
|
422 |
swr.Store("alpha", 1)
|
|
423 |
swr.Store("bravo", 2)
|
|
424 |
swr.Store("charlie", 3)
|
|
425 |
swr.Store("delta", 4)
|
|
426 |
|
|
427 |
called := make(map[string]struct{})
|
|
428 |
terminated := swr.RangeBreak(func(key string, value *TimedValue) bool {
|
|
429 |
called[key] = struct{}{}
|
|
430 |
if key == "charlie" {
|
|
431 |
return true
|
|
432 |
}
|
|
433 |
return false
|
|
434 |
})
|
|
435 |
|
|
436 |
if got, want := terminated, true; got != want {
|
|
437 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
438 |
}
|
|
439 |
if _, ok := called["charlie"]; !ok {
|
|
440 |
t.Errorf("Actual: %#v; Expected: %#v", ok, true)
|
336 | 441 |
}
|
337 | 442 |
|
338 | 443 |
swr.Store("ensure range released top level lock", struct{}{})
|
|
383 | 488 |
t.Errorf("Actual: %s; Expected: %s", actual, expected)
|
384 | 489 |
}
|
385 | 490 |
}
|
|
491 |
|
|
492 |
func TestStats(t *testing.T) {
|
|
493 |
t.Run("query", func(t *testing.T) {
|
|
494 |
var haveLookupFail bool
|
|
495 |
|
|
496 |
swr, err := NewSimple(&Config{
|
|
497 |
Lookup: func(key string) (interface{}, error) {
|
|
498 |
if haveLookupFail {
|
|
499 |
return nil, errors.New("lookup failure")
|
|
500 |
}
|
|
501 |
time.Sleep(10 * time.Millisecond)
|
|
502 |
return key, nil
|
|
503 |
},
|
|
504 |
})
|
|
505 |
if err != nil {
|
|
506 |
t.Fatal(err)
|
|
507 |
}
|
|
508 |
|
|
509 |
// Get stats before cache methods invoked.
|
|
510 |
stats := swr.Stats()
|
|
511 |
if got, want := stats.Count, int64(0); got != want {
|
|
512 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
513 |
}
|
|
514 |
if got, want := stats.Creates, int64(0); got != want {
|
|
515 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
516 |
}
|
|
517 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
518 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
519 |
}
|
|
520 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
521 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
522 |
}
|
|
523 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
524 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
525 |
}
|
|
526 |
if got, want := stats.Queries, int64(0); got != want {
|
|
527 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
528 |
}
|
|
529 |
if got, want := stats.Hits, int64(0); got != want {
|
|
530 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
531 |
}
|
|
532 |
if got, want := stats.Misses, int64(0); got != want {
|
|
533 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
534 |
}
|
|
535 |
if got, want := stats.Stales, int64(0); got != want {
|
|
536 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
537 |
}
|
|
538 |
if got, want := stats.Stores, int64(0); got != want {
|
|
539 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
540 |
}
|
|
541 |
if got, want := stats.Updates, int64(0); got != want {
|
|
542 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
543 |
}
|
|
544 |
|
|
545 |
// Invoke Query with key not yet in cache.
|
|
546 |
_, err = swr.Query("foo")
|
|
547 |
if got, want := err, error(nil); got != want {
|
|
548 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
549 |
}
|
|
550 |
|
|
551 |
// Get stats after new key-value pair added.
|
|
552 |
stats = swr.Stats()
|
|
553 |
if got, want := stats.Count, int64(1); got != want {
|
|
554 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
555 |
}
|
|
556 |
if got, want := stats.Creates, int64(1); got != want {
|
|
557 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
558 |
}
|
|
559 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
560 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
561 |
}
|
|
562 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
563 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
564 |
}
|
|
565 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
566 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
567 |
}
|
|
568 |
if got, want := stats.Queries, int64(1); got != want {
|
|
569 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
570 |
}
|
|
571 |
if got, want := stats.Hits, int64(0); got != want {
|
|
572 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
573 |
}
|
|
574 |
if got, want := stats.Misses, int64(1); got != want {
|
|
575 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
576 |
}
|
|
577 |
if got, want := stats.Stales, int64(0); got != want {
|
|
578 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
579 |
}
|
|
580 |
if got, want := stats.Stores, int64(0); got != want {
|
|
581 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
582 |
}
|
|
583 |
if got, want := stats.Updates, int64(0); got != want {
|
|
584 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
585 |
}
|
|
586 |
|
|
587 |
// Invoke Query with key already in cache.
|
|
588 |
_, err = swr.Query("foo")
|
|
589 |
if got, want := err, error(nil); got != want {
|
|
590 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
591 |
}
|
|
592 |
|
|
593 |
// Get stats after new key-value pair added.
|
|
594 |
stats = swr.Stats()
|
|
595 |
if got, want := stats.Count, int64(1); got != want {
|
|
596 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
597 |
}
|
|
598 |
if got, want := stats.Creates, int64(0); got != want {
|
|
599 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
600 |
}
|
|
601 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
602 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
603 |
}
|
|
604 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
605 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
606 |
}
|
|
607 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
608 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
609 |
}
|
|
610 |
if got, want := stats.Queries, int64(1); got != want {
|
|
611 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
612 |
}
|
|
613 |
if got, want := stats.Hits, int64(1); got != want {
|
|
614 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
615 |
}
|
|
616 |
if got, want := stats.Misses, int64(0); got != want {
|
|
617 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
618 |
}
|
|
619 |
if got, want := stats.Stales, int64(0); got != want {
|
|
620 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
621 |
}
|
|
622 |
if got, want := stats.Stores, int64(0); got != want {
|
|
623 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
624 |
}
|
|
625 |
if got, want := stats.Updates, int64(0); got != want {
|
|
626 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
627 |
}
|
|
628 |
|
|
629 |
// Invoke Query with key already in cache.
|
|
630 |
haveLookupFail = true
|
|
631 |
_, err = swr.Query("bar")
|
|
632 |
if err == nil || !strings.Contains(err.Error(), "lookup failure") {
|
|
633 |
t.Errorf("GOT: %v; WANT: %v", err, "lookup failure")
|
|
634 |
}
|
|
635 |
|
|
636 |
// Get stats after new key-value pair added.
|
|
637 |
stats = swr.Stats()
|
|
638 |
if got, want := stats.Count, int64(2); got != want {
|
|
639 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
640 |
}
|
|
641 |
if got, want := stats.Creates, int64(1); got != want {
|
|
642 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
643 |
}
|
|
644 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
645 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
646 |
}
|
|
647 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
648 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
649 |
}
|
|
650 |
if got, want := stats.LookupErrors, int64(1); got != want {
|
|
651 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
652 |
}
|
|
653 |
if got, want := stats.Queries, int64(1); got != want {
|
|
654 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
655 |
}
|
|
656 |
if got, want := stats.Hits, int64(0); got != want {
|
|
657 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
658 |
}
|
|
659 |
if got, want := stats.Misses, int64(1); got != want {
|
|
660 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
661 |
}
|
|
662 |
if got, want := stats.Stales, int64(0); got != want {
|
|
663 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
664 |
}
|
|
665 |
if got, want := stats.Stores, int64(0); got != want {
|
|
666 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
667 |
}
|
|
668 |
if got, want := stats.Updates, int64(0); got != want {
|
|
669 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
670 |
}
|
|
671 |
})
|
|
672 |
|
|
673 |
t.Run("load", func(t *testing.T) {
|
|
674 |
t.Run("stale", func(t *testing.T) {
|
|
675 |
swr, err := NewSimple(nil)
|
|
676 |
if err != nil {
|
|
677 |
t.Fatal(err)
|
|
678 |
}
|
|
679 |
swr.Store("foo", TimedValue{
|
|
680 |
Value: "foo",
|
|
681 |
Stale: time.Now().Add(-time.Second),
|
|
682 |
})
|
|
683 |
_, ok := swr.Load("foo")
|
|
684 |
if got, want := ok, true; got != want {
|
|
685 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
686 |
}
|
|
687 |
|
|
688 |
stats := swr.Stats()
|
|
689 |
|
|
690 |
if got, want := stats.Count, int64(1); got != want {
|
|
691 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
692 |
}
|
|
693 |
if got, want := stats.Creates, int64(1); got != want {
|
|
694 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
695 |
}
|
|
696 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
697 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
698 |
}
|
|
699 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
700 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
701 |
}
|
|
702 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
703 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
704 |
}
|
|
705 |
if got, want := stats.Queries, int64(1); got != want {
|
|
706 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
707 |
}
|
|
708 |
if got, want := stats.Hits, int64(0); got != want {
|
|
709 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
710 |
}
|
|
711 |
if got, want := stats.Misses, int64(0); got != want {
|
|
712 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
713 |
}
|
|
714 |
if got, want := stats.Stales, int64(1); got != want {
|
|
715 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
716 |
}
|
|
717 |
if got, want := stats.Stores, int64(1); got != want {
|
|
718 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
719 |
}
|
|
720 |
if got, want := stats.Updates, int64(0); got != want {
|
|
721 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
722 |
}
|
|
723 |
})
|
|
724 |
|
|
725 |
t.Run("expired", func(t *testing.T) {
|
|
726 |
swr, err := NewSimple(nil)
|
|
727 |
if err != nil {
|
|
728 |
t.Fatal(err)
|
|
729 |
}
|
|
730 |
swr.Store("foo", TimedValue{
|
|
731 |
Value: "foo",
|
|
732 |
Expiry: time.Now().Add(-time.Second),
|
|
733 |
})
|
|
734 |
_, ok := swr.Load("foo")
|
|
735 |
if got, want := ok, false; got != want {
|
|
736 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
737 |
}
|
|
738 |
|
|
739 |
stats := swr.Stats()
|
|
740 |
|
|
741 |
if got, want := stats.Count, int64(1); got != want {
|
|
742 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
743 |
}
|
|
744 |
if got, want := stats.Creates, int64(1); got != want {
|
|
745 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
746 |
}
|
|
747 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
748 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
749 |
}
|
|
750 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
751 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
752 |
}
|
|
753 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
754 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
755 |
}
|
|
756 |
if got, want := stats.Queries, int64(1); got != want {
|
|
757 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
758 |
}
|
|
759 |
if got, want := stats.Hits, int64(0); got != want {
|
|
760 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
761 |
}
|
|
762 |
if got, want := stats.Misses, int64(1); got != want {
|
|
763 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
764 |
}
|
|
765 |
if got, want := stats.Stales, int64(0); got != want {
|
|
766 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
767 |
}
|
|
768 |
if got, want := stats.Stores, int64(1); got != want {
|
|
769 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
770 |
}
|
|
771 |
if got, want := stats.Updates, int64(0); got != want {
|
|
772 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
773 |
}
|
|
774 |
})
|
|
775 |
})
|
|
776 |
|
|
777 |
t.Run("load-timed-value", func(t *testing.T) {
|
|
778 |
t.Run("stale", func(t *testing.T) {
|
|
779 |
swr, err := NewSimple(nil)
|
|
780 |
if err != nil {
|
|
781 |
t.Fatal(err)
|
|
782 |
}
|
|
783 |
swr.Store("foo", TimedValue{
|
|
784 |
Value: "foo",
|
|
785 |
Stale: time.Now().Add(-time.Second),
|
|
786 |
Expiry: time.Now().Add(time.Second),
|
|
787 |
})
|
|
788 |
tv := swr.LoadTimedValue("foo")
|
|
789 |
if got, want := tv.IsStale(), true; got != want {
|
|
790 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
791 |
}
|
|
792 |
if got, want := tv.IsExpired(), false; got != want {
|
|
793 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
794 |
}
|
|
795 |
|
|
796 |
stats := swr.Stats()
|
|
797 |
|
|
798 |
if got, want := stats.Count, int64(1); got != want {
|
|
799 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
800 |
}
|
|
801 |
if got, want := stats.Creates, int64(1); got != want {
|
|
802 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
803 |
}
|
|
804 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
805 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
806 |
}
|
|
807 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
808 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
809 |
}
|
|
810 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
811 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
812 |
}
|
|
813 |
if got, want := stats.Queries, int64(1); got != want {
|
|
814 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
815 |
}
|
|
816 |
if got, want := stats.Hits, int64(0); got != want {
|
|
817 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
818 |
}
|
|
819 |
if got, want := stats.Misses, int64(0); got != want {
|
|
820 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
821 |
}
|
|
822 |
if got, want := stats.Stales, int64(1); got != want {
|
|
823 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
824 |
}
|
|
825 |
if got, want := stats.Stores, int64(1); got != want {
|
|
826 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
827 |
}
|
|
828 |
if got, want := stats.Updates, int64(0); got != want {
|
|
829 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
830 |
}
|
|
831 |
})
|
|
832 |
|
|
833 |
t.Run("expired", func(t *testing.T) {
|
|
834 |
swr, err := NewSimple(nil)
|
|
835 |
if err != nil {
|
|
836 |
t.Fatal(err)
|
|
837 |
}
|
|
838 |
swr.Store("foo", TimedValue{
|
|
839 |
Value: "foo",
|
|
840 |
Stale: time.Now().Add(-time.Second),
|
|
841 |
Expiry: time.Now().Add(-time.Second),
|
|
842 |
})
|
|
843 |
tv := swr.LoadTimedValue("foo")
|
|
844 |
if got, want := tv.IsStale(), true; got != want {
|
|
845 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
846 |
}
|
|
847 |
if got, want := tv.IsExpired(), true; got != want {
|
|
848 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
849 |
}
|
|
850 |
|
|
851 |
stats := swr.Stats()
|
|
852 |
|
|
853 |
if got, want := stats.Count, int64(1); got != want {
|
|
854 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
855 |
}
|
|
856 |
if got, want := stats.Creates, int64(1); got != want {
|
|
857 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
858 |
}
|
|
859 |
if got, want := stats.Deletes, int64(0); got != want {
|
|
860 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
861 |
}
|
|
862 |
if got, want := stats.Evictions, int64(0); got != want {
|
|
863 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
864 |
}
|
|
865 |
if got, want := stats.LookupErrors, int64(0); got != want {
|
|
866 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
867 |
}
|
|
868 |
if got, want := stats.Queries, int64(1); got != want {
|
|
869 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
870 |
}
|
|
871 |
if got, want := stats.Hits, int64(0); got != want {
|
|
872 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
873 |
}
|
|
874 |
if got, want := stats.Misses, int64(1); got != want {
|
|
875 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
876 |
}
|
|
877 |
if got, want := stats.Stales, int64(0); got != want {
|
|
878 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
879 |
}
|
|
880 |
if got, want := stats.Stores, int64(1); got != want {
|
|
881 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
882 |
}
|
|
883 |
if got, want := stats.Updates, int64(0); got != want {
|
|
884 |
t.Errorf("GOT: %v; WANT: %v", got, want)
|
|
885 |
}
|
|
886 |
})
|
|
887 |
})
|
|
888 |
}
|