-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathfile_test.go
More file actions
181 lines (158 loc) · 4.61 KB
/
file_test.go
File metadata and controls
181 lines (158 loc) · 4.61 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package file_test
import (
"io/ioutil"
"log"
"os"
"testing"
"github.com/philippgille/gokv"
"github.com/philippgille/gokv/encoding"
"github.com/philippgille/gokv/file"
"github.com/philippgille/gokv/test"
)
// TestStore tests if reading from, writing to and deleting from the store works properly.
// A struct is used as value. See TestTypes() for a test that is simpler but tests all types.
func TestStore(t *testing.T) {
// Test with JSON
t.Run("JSON", func(t *testing.T) {
store, path := createStore(t, encoding.JSON)
defer cleanUp(store, path)
test.TestStore(store, t)
})
// Test with gob
t.Run("gob", func(t *testing.T) {
store, path := createStore(t, encoding.Gob)
defer cleanUp(store, path)
test.TestStore(store, t)
})
}
// TestTypes tests if setting and getting values works with all Go types.
func TestTypes(t *testing.T) {
// Test with JSON
t.Run("JSON", func(t *testing.T) {
store, path := createStore(t, encoding.JSON)
defer cleanUp(store, path)
test.TestTypes(store, t)
})
// Test with gob
t.Run("gob", func(t *testing.T) {
store, path := createStore(t, encoding.Gob)
defer cleanUp(store, path)
test.TestTypes(store, t)
})
}
// TestStoreConcurrent launches a bunch of goroutines that concurrently work with one store.
// The store is Go map with manual locking via sync.RWMutex, so testing this is important.
func TestStoreConcurrent(t *testing.T) {
store, path := createStore(t, encoding.JSON)
defer cleanUp(store, path)
goroutineCount := 1000
test.TestConcurrentInteractions(t, goroutineCount, store)
}
// TestErrors tests some error cases.
func TestErrors(t *testing.T) {
// Test empty key
store, path := createStore(t, encoding.JSON)
defer cleanUp(store, path)
err := store.Set("", "bar")
if err == nil {
t.Error("Expected an error")
}
_, err = store.Get("", new(string))
if err == nil {
t.Error("Expected an error")
}
err = store.Delete("")
if err == nil {
t.Error("Expected an error")
}
}
// TestNil tests the behaviour when passing nil or pointers to nil values to some methods.
func TestNil(t *testing.T) {
// Test setting nil
t.Run("set nil with JSON marshalling", func(t *testing.T) {
store, path := createStore(t, encoding.JSON)
defer cleanUp(store, path)
err := store.Set("foo", nil)
if err == nil {
t.Error("Expected an error")
}
})
t.Run("set nil with Gob marshalling", func(t *testing.T) {
store, path := createStore(t, encoding.Gob)
defer cleanUp(store, path)
err := store.Set("foo", nil)
if err == nil {
t.Error("Expected an error")
}
})
// Test passing nil or pointer to nil value for retrieval
createTest := func(codec encoding.Codec) func(t *testing.T) {
return func(t *testing.T) {
store, path := createStore(t, codec)
defer cleanUp(store, path)
// Prep
err := store.Set("foo", test.Foo{Bar: "baz"})
if err != nil {
t.Error(err)
}
_, err = store.Get("foo", nil) // actually nil
if err == nil {
t.Error("An error was expected")
}
var i any // actually nil
_, err = store.Get("foo", i)
if err == nil {
t.Error("An error was expected")
}
var valPtr *test.Foo // nil value
_, err = store.Get("foo", valPtr)
if err == nil {
t.Error("An error was expected")
}
}
}
t.Run("get with nil / nil value parameter", createTest(encoding.JSON))
t.Run("get with nil / nil value parameter", createTest(encoding.Gob))
}
// TestClose tests if the close method returns any errors.
func TestClose(t *testing.T) {
store, path := createStore(t, encoding.JSON)
defer os.RemoveAll(path)
err := store.Close()
if err != nil {
t.Error(err)
}
}
func createStore(t *testing.T, codec encoding.Codec) (file.Store, string) {
path := generateRandomTempDBpath(t)
options := file.Options{
Directory: path,
// Setting no FileNameEnding leads to all Codecs writing ".json" files,
// but that doesn't matter to the functionality of gokv.
Codec: codec,
}
store, err := file.NewStore(options)
if err != nil {
t.Fatal(err)
}
return store, path
}
func generateRandomTempDBpath(t *testing.T) string {
path, err := ioutil.TempDir(os.TempDir(), "gokv")
if err != nil {
t.Fatalf("Generating random DB path failed: %v", err)
}
return path
}
// cleanUp cleans up the store (deletes the files that have been created during a test).
// If an error occurs the test is NOT marked as failed.
func cleanUp(store gokv.Store, path string) {
err := store.Close()
if err != nil {
log.Printf("Error during cleaning up after a test (during closing the store): %v\n", err)
}
err = os.RemoveAll(path)
if err != nil {
log.Printf("Error during cleaning up after a test (during removing the data directory): %v\n", err)
}
}