forked from philchia/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
98 lines (81 loc) · 1.87 KB
/
cache_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
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
package agollo
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestCache(t *testing.T) {
cache := newCache()
cache.set("key", "val")
if val, ok := cache.get("key"); !ok || val != "val" {
t.FailNow()
}
cache.set("key", "val2")
if val, ok := cache.get("key"); !ok || val != "val2" {
t.FailNow()
}
kv := cache.dump()
if len(kv) != 1 || kv["key"] != "val2" {
t.FailNow()
}
cache.delete("key")
if _, ok := cache.get("key"); ok {
t.FailNow()
}
}
func TestCacheDump(t *testing.T) {
var caches = newNamespaceCahce()
defer caches.drain()
caches.mustGetCache("namespace").set("key", "val")
f, err := ioutil.TempFile(".", "agollo")
if err != nil {
t.Error(err)
}
f.Close()
defer os.Remove(f.Name())
if err := caches.dump(f.Name()); err != nil {
t.Error(err)
}
var restore = newNamespaceCahce()
defer restore.drain()
if err := restore.load(f.Name()); err != nil {
t.Error(err)
}
if val, _ := restore.mustGetCache("namespace").get("key"); val != "val" {
t.FailNow()
}
if err := restore.load("null"); err == nil {
t.FailNow()
}
if err := restore.load("./testdata/app.properties"); err == nil {
t.FailNow()
}
}
func TestDecode(t *testing.T) {
// decode
type Client struct {
APP struct {
Key string `mapstructure:"key"`
} `mapstructure:"application"`
Client1 struct {
Name string `mapstructure:"name"`
} `mapstructure:"client.json"`
Client2 struct {
Name string `mapstructure:"name"`
} `mapstructure:"client.yaml"`
}
var mc = newNamespaceCahce()
defer mc.drain()
mc.mustGetCache("application").set("key", "val")
mc.mustGetCache("client.json").set("content", `{"name":"json"}`)
mc.mustGetCache("client.yaml").set("content", "name: yaml")
var c Client
if err := mc.decode(&c); err != nil {
t.Error(err)
}
fmt.Printf("%+v", c)
if c.APP.Key != "val" || c.Client1.Name != "json" || c.Client2.Name != "yaml" {
t.FailNow()
}
}