-
Notifications
You must be signed in to change notification settings - Fork 1
/
identity_test.go
71 lines (53 loc) · 1014 Bytes
/
identity_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
package eventsource
import (
"io"
"reflect"
"strconv"
"testing"
)
func randomEvents() []Event {
events := make([]Event, 1000)
for i := 0; i < 1000; i++ {
event := Event{Type: "custom"}
if i%3 == 0 {
event.Type = "other"
}
if i%5 == 0 {
event.Retry = strconv.FormatInt(int64(i*1000), 10)
}
if i%10 == 0 {
event.ResetID = true
} else {
event.ID = strconv.FormatInt(int64(i), 10)
}
if i%20 != 0 {
event.Data = []byte(strconv.FormatInt(int64(i), 10))
}
events[i] = event
}
return events
}
func TestEncodeDecodeIdentity(t *testing.T) {
r, w := io.Pipe()
d, e := NewDecoder(r), NewEncoder(w)
in := randomEvents()
go func() {
for _, event := range in {
if err := e.Encode(event); err != nil {
t.Fatal(err)
}
}
w.Close()
}()
out := make([]Event, 0, len(in))
for {
var event Event
if d.Decode(&event) != nil {
break
}
out = append(out, event)
}
if !reflect.DeepEqual(in, out) {
t.Fatal("output does not match input")
}
}