This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriter_test.go
64 lines (54 loc) · 1.93 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
package sntable_test
import (
"bytes"
"math/rand"
"github.com/bsm/sntable"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Writer", func() {
var buf *bytes.Buffer
var subject *sntable.Writer
var testdata = []byte("testdata")
BeforeEach(func() {
buf = new(bytes.Buffer)
subject = sntable.NewWriter(buf, nil)
})
AfterEach(func() {
_ = subject.Close()
})
It("should write empty", func() {
Expect(subject.Close()).To(Succeed())
Expect(buf.Len()).To(Equal(16))
})
It("should prevent out-of-order appends", func() {
Expect(subject.Append(20, testdata)).To(Succeed())
Expect(subject.Append(19, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 19 must be > 20`))
Expect(subject.Append(22, testdata)).To(Succeed())
Expect(subject.Append(20, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 20 must be > 22`))
Expect(subject.Append(23, testdata)).To(Succeed())
Expect(subject.Append(23, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 23 must be > 23`))
Expect(subject.Append(24, testdata)).To(Succeed())
})
It("should write (non-compressable)", func() {
rnd := rand.New(rand.NewSource(1))
val := make([]byte, 128)
for key := uint64(0); key < 100000; key += 2 {
_, err := rnd.Read(val)
Expect(err).NotTo(HaveOccurred())
Expect(subject.Append(key, val)).To(Succeed())
}
Expect(subject.Close()).To(Succeed())
Expect(buf.Len()).To(BeNumerically("~", 6575289, 1024))
Expect(buf.String()[buf.Len()-8:]).To(Equal("\x47\x27\x86\xBE\x1F\x7a\x65\xDB"))
})
It("should write (well-compressable)", func() {
val := bytes.Repeat(testdata, 16)
for key := uint64(0); key < 100000; key += 2 {
Expect(subject.Append(key, val)).To(Succeed())
}
Expect(subject.Close()).To(Succeed())
Expect(buf.Len()).To(BeNumerically("~", 362538, 1024))
Expect(buf.String()[buf.Len()-8:]).To(Equal("\x47\x27\x86\xBE\x1F\x7a\x65\xDB"))
})
})