This repository was archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_test.go
More file actions
75 lines (66 loc) · 1.37 KB
/
writer_test.go
File metadata and controls
75 lines (66 loc) · 1.37 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
package recache
import (
"crypto/rand"
"encoding/json"
"fmt"
"hash/adler32"
"testing"
)
func TestBindJSON(t *testing.T) {
cache := NewCache(CacheOptions{})
var f *Frontend
f = cache.NewFrontend(func(k Key, rw *RecordWriter) (err error) {
switch k.(int) {
case 0:
var data string
err = rw.BindJSON(f, 1, &data)
if err != nil {
return
}
return json.NewEncoder(rw).Encode(data)
case 1:
return json.NewEncoder(rw).Encode("foo")
default:
return fmt.Errorf("unknown key: %d", k.(int))
}
})
run := func() {
s, err := f.Get(0)
if err != nil {
t.Fatal(err)
}
assertJsonStringEquals(t, s, "foo")
}
// Initial population
run()
// Read after the data has been populated and made immutable
run()
}
func TestAdlerAppend(t *testing.T) {
t.Parallel()
buf1 := make([]byte, 128)
buf2 := make([]byte, 128)
for _, b := range [...][]byte{buf1, buf2} {
n, err := rand.Read(b)
if err != nil {
t.Fatal(err)
}
if n != 128 {
t.Fatalf("not enough bytes read: %d", n)
}
}
genDesc := func(b []byte) frameDescriptor {
return frameDescriptor{
size: uint32(len(b)),
checksum: adler32.Checksum(b),
}
}
fd1 := genDesc(buf1)
fd2 := genDesc(buf2)
appended := fd1
appended.append(fd2)
combined := genDesc(append(buf1, buf2...))
if appended != combined {
t.Fatalf("descriptors don't match: %+v != %+v", appended, combined)
}
}