-
Notifications
You must be signed in to change notification settings - Fork 102
/
stream_test.go
117 lines (102 loc) · 2.62 KB
/
stream_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
package diskv
import (
"bytes"
"io/ioutil"
"testing"
)
func TestBasicStreamCaching(t *testing.T) {
d := New(Options{
BasePath: "test-data",
CacheSizeMax: 1024,
})
defer d.EraseAll()
input := "a1b2c3"
key, writeBuf, sync := "a", bytes.NewBufferString(input), true
if err := d.WriteStream(key, writeBuf, sync); err != nil {
t.Fatal(err)
}
if d.isCached(key) {
t.Fatalf("'%s' cached, but shouldn't be (yet)", key)
}
rc, err := d.ReadStream(key, false)
if err != nil {
t.Fatal(err)
}
readBuf, err := ioutil.ReadAll(rc)
if err != nil {
t.Fatal(err)
}
if !cmpBytes(readBuf, []byte(input)) {
t.Fatalf("'%s' != '%s'", string(readBuf), input)
}
if !d.isCached(key) {
t.Fatalf("'%s' isn't cached, but should be", key)
}
}
func TestReadStreamDirect(t *testing.T) {
var (
basePath = "test-data"
)
dWrite := New(Options{
BasePath: basePath,
CacheSizeMax: 0,
})
defer dWrite.EraseAll()
dRead := New(Options{
BasePath: basePath,
CacheSizeMax: 1024,
})
// Write
key, val1, val2 := "a", []byte(`1234567890`), []byte(`aaaaaaaaaa`)
if err := dWrite.Write(key, val1); err != nil {
t.Fatalf("during first write: %s", err)
}
// First, caching read.
val, err := dRead.Read(key)
if err != nil {
t.Fatalf("during initial read: %s", err)
}
t.Logf("read 1: %s => %s", key, string(val))
if !cmpBytes(val1, val) {
t.Errorf("expected %q, got %q", string(val1), string(val))
}
if !dRead.isCached(key) {
t.Errorf("%q should be cached, but isn't", key)
}
// Write a different value.
if err := dWrite.Write(key, val2); err != nil {
t.Fatalf("during second write: %s", err)
}
// Second read, should hit cache and get the old value.
val, err = dRead.Read(key)
if err != nil {
t.Fatalf("during second (cache-hit) read: %s", err)
}
t.Logf("read 2: %s => %s", key, string(val))
if !cmpBytes(val1, val) {
t.Errorf("expected %q, got %q", string(val1), string(val))
}
// Third, direct read, should get the updated value.
rc, err := dRead.ReadStream(key, true)
if err != nil {
t.Fatalf("during third (direct) read, ReadStream: %s", err)
}
defer rc.Close()
val, err = ioutil.ReadAll(rc)
if err != nil {
t.Fatalf("during third (direct) read, ReadAll: %s", err)
}
t.Logf("read 3: %s => %s", key, string(val))
if !cmpBytes(val2, val) {
t.Errorf("expected %q, got %q", string(val1), string(val))
}
// Fourth read, should hit cache and get the new value.
val, err = dRead.Read(key)
if err != nil {
t.Fatalf("during fourth (cache-hit) read: %s", err)
}
t.Logf("read 4: %s => %s", key, string(val))
if !cmpBytes(val2, val) {
t.Errorf("expected %q, got %q", string(val1), string(val))
}
}