forked from alephzero/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_writer_test.go
95 lines (81 loc) · 2.16 KB
/
reader_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
package alephzero
import (
"fmt"
"sync"
"testing"
)
func TestReaderWriter(t *testing.T) {
FileRemove("foo")
file, err := FileOpen("foo", nil)
check(t, err)
defer file.Close()
w, err := NewWriter(file.Arena())
check(t, err)
defer w.Close()
rs, err := NewReaderSync(file.Arena(), INIT_OLDEST, ITER_NEXT)
check(t, err)
defer rs.Close()
cnd := sync.NewCond(&sync.Mutex{})
allPayloads := [][]byte{}
r, err := NewReader(file.Arena(), INIT_OLDEST, ITER_NEXT, func(pkt Packet) {
cnd.L.Lock()
allPayloads = append(allPayloads, pkt.Payload)
cnd.Signal()
cnd.L.Unlock()
})
check(t, err)
defer r.Close()
if hasNext, err := rs.HasNext(); err != nil || hasNext {
t.Error("HasNext() should be false")
}
check(t, w.Write(NewPacket(nil, []byte("hello"))))
if hasNext, err := rs.HasNext(); err != nil || !hasNext {
t.Error("HasNext() should be true")
}
pkt, err := rs.Next()
check(t, err)
if string(pkt.Payload) != "hello" {
t.Error("Payload() should be 'hello'")
}
if hasNext, err := rs.HasNext(); err != nil || hasNext {
t.Error("HasNext() should be false")
}
check(t, w.Push(AddTransportSeqHeader()))
w2, err := w.Wrap(AddWriterSeqHeader())
check(t, err)
check(t, w.Write(NewPacket(nil, []byte("aaa"))))
check(t, w2.Write(NewPacket(nil, []byte("bbb"))))
pkt, err = rs.Next()
check(t, err)
if string(pkt.Payload) != "aaa" {
t.Error("Payload() should be 'aaa'")
}
if fmt.Sprint(pkt.Headers) != "map[a0_transport_seq:[1]]" {
t.Error("Headers() should be 'map[a0_transport_seq:[1]]'")
}
pkt, err = rs.Next()
check(t, err)
if string(pkt.Payload) != "bbb" {
t.Error("Payload() should be 'bbb'")
}
if fmt.Sprint(pkt.Headers) != "map[a0_transport_seq:[2] a0_writer_seq:[0]]" {
t.Error("Headers() should be 'map[a0_transport_seq:[2] a0_writer_seq:[0]]'")
}
cnd.L.Lock()
for len(allPayloads) < 3 {
cnd.Wait()
}
cnd.L.Unlock()
if len(allPayloads) != 3 {
t.Error("should have received 3 packet")
}
if string(allPayloads[0]) != "hello" {
t.Error("payload 0 should be 'hello'")
}
if string(allPayloads[1]) != "aaa" {
t.Error("payload 1 should be 'aaa'")
}
if string(allPayloads[2]) != "bbb" {
t.Error("payload 2 should be 'bbb'")
}
}