-
Notifications
You must be signed in to change notification settings - Fork 9
/
peanutcache_test.go
48 lines (42 loc) · 1.07 KB
/
peanutcache_test.go
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
// Copyright 2021 Peanutzhen. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package peanutcache
import (
"fmt"
"log"
"testing"
)
func TestGet(t *testing.T) {
mysql := map[string]string{
"Tom": "630",
"Jack": "589",
"Sam": "567",
}
loadCounts := make(map[string]int, len(mysql))
g := NewGroup("scores", 2<<10, RetrieverFunc(
func(key string) ([]byte, error) {
log.Println("[Mysql] search key", key)
if v, ok := mysql[key]; ok {
if _, ok := loadCounts[key]; !ok {
loadCounts[key] = 0
}
loadCounts[key]++
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))
for k, v := range mysql {
if view, err := g.Get(k); err != nil || view.String() != v {
t.Fatalf("failed to get value of %s", k)
}
if _, err := g.Get(k); err != nil || loadCounts[k] > 1 {
t.Fatalf("cache %s miss", k)
}
}
if view, err := g.Get("unknown"); err == nil {
t.Fatalf("the value of unknow should be empty, but %s got", view)
} else {
log.Println(err)
}
}