-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfile_test.go
69 lines (51 loc) · 1.46 KB
/
file_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
package file
import (
"io/ioutil"
"testing"
"time"
)
const (
testKey = "foo"
testValue = "bar"
)
func TestFile(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
c := New(dir)
if err := c.Save(testKey, testValue, 1*time.Nanosecond); err != nil {
t.Errorf("save fail: expected nil, got %v", err)
}
if _, err := c.Fetch(testKey); err == nil {
t.Errorf("fetch fail: expected an error, got %v", err)
}
_ = c.Save(testKey, testValue, 10*time.Second)
if res, _ := c.Fetch(testKey); res != testValue {
t.Errorf("fetch fail, wrong value: expected %s, got %s", testValue, res)
}
_ = c.Save(testKey, testValue, 0)
if !c.Contains(testKey) {
t.Errorf("contains failed: the key %s should be exist", testKey)
}
_ = c.Save("bar", testValue, 0)
if values := c.FetchMulti([]string{testKey, "bar"}); len(values) == 0 {
t.Errorf("fetch multi failed: expected %d, got %d", 2, len(values))
}
if err := c.Flush(); err != nil {
t.Errorf("flush failed: expected nil, got %v", err)
}
if c.Contains(testKey) {
t.Errorf("contains failed: the key %s should not be exist", testKey)
}
c = New("./test/")
if err := c.Save(testKey, testValue, 0); err == nil {
t.Errorf("save failed: expected an error, got %v", err)
}
if _, err := c.Fetch(testKey); err == nil {
t.Errorf("fetch failed: expected and error, got %v", err)
}
if err := c.Flush(); err == nil {
t.Errorf("flush failed: expected an error, got %v", err)
}
}