Repository navigation
Expand file tree
/
Copy pathsimplefs_test.go
More file actions
154 lines (120 loc) · 3.41 KB
/
Copy pathsimplefs_test.go
File metadata and controls
154 lines (120 loc) · 3.41 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package simplefs_test
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/darkweak/storages/core"
"github.com/darkweak/storages/simplefs"
"go.uber.org/zap"
)
const (
byteKey = "MyByteKey"
nonExistentKey = "NonExistentKey"
baseValue = "My first data"
)
func getSimplefsInstance() (core.Storer, error) {
return simplefs.Factory(core.CacheProvider{}, zap.NewNop().Sugar(), 0)
}
func TestCreateDefaultStore(t *testing.T) {
_, err := getSimplefsInstance()
if err != nil {
t.Fatal(err)
}
}
func TestSimplefsConnectionFactory(t *testing.T) {
instance, err := getSimplefsInstance()
if nil != err {
t.Error("Shouldn't have panic")
}
if nil == instance {
t.Error("Simplefs should be instanciated")
}
}
func TestIShouldBeAbleToReadAndWriteDataInSimplefs(t *testing.T) {
client, _ := getSimplefsInstance()
_ = client.Set("Test", []byte(baseValue), time.Duration(20)*time.Second)
time.Sleep(1 * time.Second)
res := client.Get("Test")
if len(res) == 0 {
t.Errorf("Key %s should exist", baseValue)
}
if baseValue != string(res) {
t.Errorf("%s not corresponding to %s", string(res), baseValue)
}
}
func TestSimplefs_GetRequestInCache(t *testing.T) {
client, _ := getSimplefsInstance()
res := client.Get(nonExistentKey)
if 0 < len(res) {
t.Errorf("Key %s should not exist", nonExistentKey)
}
}
func TestSimplefs_GetSetRequestInCache_OneByte(t *testing.T) {
client, _ := getSimplefsInstance()
_ = client.Set(byteKey, []byte("A"), time.Duration(20)*time.Second)
time.Sleep(1 * time.Second)
res := client.Get(byteKey)
if len(res) == 0 {
t.Errorf("Key %s should exist", byteKey)
}
if string(res) != "A" {
t.Errorf("%s not corresponding to %v", res, 65)
}
}
func TestSimplefs_SetRequestInCache_TTL(t *testing.T) {
key := "MyEmptyKey"
client, _ := getSimplefsInstance()
value := []byte("Hello world")
_ = client.Set(key, value, time.Duration(20)*time.Second)
time.Sleep(1 * time.Second)
newValue := client.Get(key)
if len(newValue) != len(value) {
t.Errorf("Key %s should be equals to %s, %s provided", key, value, newValue)
}
}
func TestSimplefs_SetRequestInCache_Negative_TTL(t *testing.T) {
client, _ := getSimplefsInstance()
value := []byte("New value")
_ = client.Set(byteKey, value, -1)
time.Sleep(1 * time.Second)
_ = client.Set(byteKey, value, time.Duration(20)*time.Second)
time.Sleep(1 * time.Second)
newValue := client.Get(byteKey)
if len(newValue) != len(value) {
t.Errorf("Key %s should be equals to %s, %s provided", byteKey, value, newValue)
}
}
func TestSimplefs_DeleteRequestInCache(t *testing.T) {
client, _ := getSimplefsInstance()
client.Delete(byteKey)
time.Sleep(1 * time.Second)
if 0 < len(client.Get(byteKey)) {
t.Errorf("Key %s should not exist", byteKey)
}
}
func TestSimplefs_Init(t *testing.T) {
client, _ := getSimplefsInstance()
err := client.Init()
if nil != err {
t.Error("Impossible to init Simplefs provider")
}
}
func TestSimplefs_EvictAfterXSeconds(t *testing.T) {
client, _ := getSimplefsInstance()
_ = client.Init()
for i := range 10 {
key := fmt.Sprintf("Test_%d", i)
_ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", time.Second, key)
time.Sleep(100 * time.Millisecond)
}
res := client.Get("Test_0")
if len(res) != 0 {
t.Errorf("Key %s should be evicted", "Test_0")
}
res = client.Get("Test_9")
if len(res) == 0 {
t.Errorf("Key %s should exist", "Test_9")
}
time.Sleep(3 * time.Second)
}