-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachestat_test.go
executable file
·135 lines (112 loc) · 2.4 KB
/
cachestat_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
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
package fscache_test
import (
"github.com/Kovensky/go-fscache"
"math/rand"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestCache_Stat(T *testing.T) {
cd, err := fscache.NewCacheDir(".testdir")
if err != nil {
T.Fatal(err)
return
}
err = cd.Touch("test", "stat")
if err != nil {
T.Fatal(err)
return
}
stat, err := cd.Stat("test", "stat")
if err != nil {
T.Fatal(err)
return
}
T.Log("IsDir:", stat.IsDir())
T.Log("ModTime:", stat.ModTime())
T.Log("Name: " + stat.Name())
T.Log("Size:", stat.Size(), "bytes")
if stat.IsDir() || stat.ModTime().IsZero() || stat.Name() != "stat" || stat.Size() != 0 {
T.Error("Stat returned unexpected data")
}
}
func TestCache_Touch(T *testing.T) {
cd, err := fscache.NewCacheDir(".testdir")
if err != nil {
T.Fatal(err)
return
}
err = cd.Touch("test", "touch", "file")
if err != nil {
T.Fatal(err)
return
}
stat, err := cd.Stat("test", "touch")
if err != nil {
T.Fatal(err)
return
}
if !stat.IsDir() {
T.Error("Expected touch to be a dir, file found")
}
// can be anything -- just make a nonblank file
size, err := cd.Set(stat.ModTime(), "test", "touch", "subdir", "file")
if err != nil {
T.Fatal(err)
return
}
stat, err = cd.Stat("test", "touch", "subdir", "file")
if err != nil {
T.Fatal(err)
return
}
if stat.Size() != size {
panic("File modified outside of test")
}
T.Log("Waiting 5ms")
time.Sleep(5 * time.Millisecond)
err = cd.Touch("test", "touch", "subdir", "file")
if err != nil {
T.Fatal(err)
}
stat2, err := cd.Stat("test", "touch", "subdir", "file")
if stat.Size() != stat2.Size() {
T.Errorf("Touch modified the size")
}
if !stat2.ModTime().After(stat.ModTime()) {
T.Errorf("Touch did not update timestamp (FAT filesystem?)")
}
}
func TestCache_Chtime(T *testing.T) {
cd, err := fscache.NewCacheDir(".testdir")
if err != nil {
T.Fatal(err)
return
}
err = cd.Touch("test", "chtime")
if err != nil {
T.Fatal(err)
return
}
stat, err := cd.Stat("test", "chtime")
if err != nil {
T.Fatal(err)
return
}
newTime := stat.ModTime().Add(-1 * 7 * 24 * time.Hour)
err = cd.Chtime(newTime, "test", "chtime")
if err != nil {
T.Fatal(err)
return
}
stat, err = cd.Stat("test", "chtime")
if err != nil {
T.Fatal(err)
return
}
if mtime := stat.ModTime(); !mtime.Equal(newTime) {
T.Errorf("Expected mtime %q, got mtime %q", newTime, mtime)
}
}