-
Notifications
You must be signed in to change notification settings - Fork 3
/
store_test.go
136 lines (112 loc) · 2.74 KB
/
store_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
136
package main
import (
"bytes"
"io"
"log/slog"
"os"
"reflect"
"testing"
"time"
)
// dummyReadCloser wraps around a bytes.Buffer and implements a ReadCloser.
type dummyReadCloser struct {
buff *bytes.Buffer
}
func newDummyReadCloser(b *bytes.Buffer) dummyReadCloser {
return dummyReadCloser{buff: b}
}
func (drc dummyReadCloser) Read(p []byte) (int, error) {
return drc.buff.Read(p)
}
func (drc dummyReadCloser) Close() error {
return nil
}
func TestStore(t *testing.T) {
loggerLevel := new(slog.LevelVar)
loggerLevel.Set(slog.LevelDebug)
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: loggerLevel}))
slog.SetDefault(logger)
item := Item{Expires: time.Now().Add(time.Minute).UTC()}
itemDataRaw := []byte("hello world")
itemData := newDummyReadCloser(bytes.NewBuffer(itemDataRaw))
storageDir, err := os.MkdirTemp("", "db")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(storageDir)
store, err := NewStore(storageDir, randomIdGenerator(4), false)
if err != nil {
t.Fatal(err)
}
if _, err := store.Get("whatever"); err != ErrNotFound {
t.Fatal(err)
}
itemId, err := store.Put(item, itemData)
if err != nil {
t.Fatal(err)
}
item.ID = itemId
if itemX, err := store.Get(itemId); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(item, itemX) {
t.Fatalf("Fetched Item mismatches: got %v and expected %v", itemX, item)
}
if f, err := store.GetFile(itemId); err != nil {
t.Fatal(err)
} else {
buff := make([]byte, len(itemDataRaw))
n, err := io.ReadFull(f, buff)
if err != nil {
t.Fatal(n, err)
}
f.Close()
buff = buff[:n]
if !bytes.Equal(itemDataRaw, buff) {
t.Fatalf("Store data mismatch: %v != %v", itemDataRaw, buff)
}
}
if err := store.Delete(item.ID); err != nil {
t.Fatal(err)
} else if _, err := store.Get(item.ID); err != ErrNotFound {
t.Fatal(err)
}
item.Expires = time.Now().Add(-1 * time.Minute).UTC()
if _, err := store.Put(item, itemData); err != nil {
t.Fatal(err)
}
if err := store.deleteExpired(); err != nil {
t.Fatal(err)
} else if _, err := store.Get(item.ID); err != ErrNotFound {
t.Fatal(err)
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
}
func TestStoreCreateId(t *testing.T) {
const ids = 1024
storageDir, err := os.MkdirTemp("", "db")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(storageDir)
store, err := NewStore(storageDir, randomIdGenerator(4), false)
if err != nil {
t.Fatal(err)
}
idCheck := make(map[string]struct{})
for i := 0; i < ids; i++ {
id, err := store.createID()
if err != nil {
t.Fatal(err)
}
if _, exists := idCheck[id]; exists {
t.Fatalf("ID %s does already exist", id)
} else {
idCheck[id] = struct{}{}
}
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
}