forked from abdullin/cellar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
133 lines (113 loc) · 2.77 KB
/
writer_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
package cellar
import (
"crypto/rand"
"fmt"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func genRandBytes(size int) []byte {
key := make([]byte, size)
var err error
if _, err = io.ReadFull(rand.Reader, key); err != nil {
panic(err)
}
return key
}
func genSeedBytes(size int, seed int) []byte {
buf := make([]byte, size)
for i := 0; i < size; i++ {
buf[i] = byte((i + seed) % 256)
}
return buf
}
func checkSeedBytes(data []byte, seed int) error {
for i := 0; i < len(data); i++ {
expect := byte((i + seed) % 256)
if data[i] != expect {
return fmt.Errorf("Given seed %d expected %d at position %d but got %d", seed, expect, i, data[i])
}
}
return nil
}
func newCompressor() Compressor {
return &ChainCompressor{CompressionLevel: 10}
}
func newDecompressor() Decompressor {
return &ChainDecompressor{}
}
var key = genRandBytes(16)
func newCipher() Cipher {
return NewAES(key)
}
func TestWriter_Append_Read(t *testing.T) {
db, err := New(dbDir, WithNoFileLock, WithMetaDB(newBoltMetaDB()))
require.NoError(t, err)
tcs := []struct {
input string
}{
{
"small string",
},
{
"larger string with some fluff",
},
{
"fairly large string with some fluff and other stuff to see if we can write large things",
},
}
for _, tc := range tcs {
_, err := db.Append([]byte(tc.input))
require.NoError(t, err)
}
db.Flush()
reader := db.Reader()
for _, tc := range tcs {
found := false
err = reader.Scan(func(pos *ReaderInfo, data []byte) error {
if string(data) == tc.input {
found = true
}
return nil
})
time.Sleep(1 * time.Second)
require.NoError(t, err)
assert.True(t, found)
}
}
func BenchmarkWriter_Append_Small(b *testing.B) {
const (
Message = "a fairly small message"
)
db, err := New(dbDir, WithNoFileLock, WithMetaDB(newBoltMetaDB()))
require.NoError(b, err)
for i := 0; i < b.N; i++ {
_, err = db.Append([]byte(Message))
require.NoError(b, err)
}
}
func BenchmarkWriter_Append_Medium(b *testing.B) {
const (
Message = "a medium sized message, this one is approximately three times as long."
)
db, err := New(dbDir, WithNoFileLock, WithMetaDB(newBoltMetaDB()))
require.NoError(b, err)
for i := 0; i < b.N; i++ {
_, err = db.Append([]byte(Message))
require.NoError(b, err)
}
}
func BenchmarkWriter_Append_Large(b *testing.B) {
const (
Message = "a large sized message, this one is approximately 9 times as long as the smaller message, and 3 times as long" +
"as the medium message. I wonder how these are autogenerated? I bet it is an AI powered by AR."
)
db, err := New(dbDir, WithNoFileLock, WithMetaDB(newBoltMetaDB()))
require.NoError(b, err)
for i := 0; i < b.N; i++ {
_, err = db.Append([]byte(Message))
require.NoError(b, err)
}
}