-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_encoder_test.go
133 lines (89 loc) · 2.21 KB
/
stream_encoder_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 cypress
import (
"bytes"
"encoding/binary"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektra/neko"
)
func TestStreamEncoder(t *testing.T) {
n := neko.Start(t)
var (
buf *ByteBuffer
se *StreamEncoder
)
n.Setup(func() {
buf = new(ByteBuffer)
se = NewStreamEncoder(buf)
})
n.It("writes a header", func() {
err := se.Init(SNAPPY)
require.NoError(t, err)
hdr := &StreamHeader{}
bt := buf.Bytes()
assert.Equal(t, byte('-'), bt[0])
val, cnt := binary.Uvarint(bt[1:])
err = hdr.Unmarshal(bt[1+cnt : 1+cnt+int(val)])
require.NoError(t, err)
assert.Equal(t, StreamHeader_SNAPPY, hdr.GetCompression())
})
n.It("encodes a message to the output", func() {
m := Log()
m.Add("hello", "world")
err := se.Receive(m)
require.NoError(t, err)
input := bytes.NewReader(buf.Bytes())
dec := NewDecoder(input)
m2, err := dec.Decode()
require.NoError(t, err)
assert.Equal(t, m, m2)
})
n.Only("encodes a message to the output matching the stream's params", func() {
err := se.Init(SNAPPY)
require.NoError(t, err)
m := Log()
m.Add("hello", "world")
err = se.Receive(m)
require.NoError(t, err)
err = se.Close()
require.NoError(t, err)
input := bytes.NewReader(buf.Bytes())
probe := NewProbe(input)
err = probe.Probe()
require.NoError(t, err)
dec := NewDecoder(probe.Reader())
m2, err := dec.Decode()
require.NoError(t, err)
assert.Equal(t, m, m2)
})
n.It("figures out the right settings for a file", func() {
f, err := ioutil.TempFile("", "cypress")
require.NoError(t, err)
defer os.Remove(f.Name())
err = se.Init(SNAPPY)
require.NoError(t, err)
_, err = f.Write(buf.Bytes())
require.NoError(t, err)
_, err = f.Seek(0, os.SEEK_SET)
require.NoError(t, err)
se = NewStreamEncoder(f)
err = se.OpenFile(f)
require.NoError(t, err)
m := Log()
m.Add("hello", "world")
err = se.Receive(m)
require.NoError(t, err)
_, err = f.Seek(0, os.SEEK_SET)
probe := NewProbe(f)
err = probe.Probe()
require.NoError(t, err)
dec := NewDecoder(probe.Reader())
m2, err := dec.Decode()
require.NoError(t, err)
assert.Equal(t, m, m2)
})
n.Meow()
}