-
Notifications
You must be signed in to change notification settings - Fork 1
/
parcelChannel_test.go
66 lines (55 loc) · 1.22 KB
/
parcelChannel_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
package p2p
import (
"testing"
"time"
)
func TestParcelChannel_Send(t *testing.T) {
smolChannel := newParcelChannel(1)
bigChannel := newParcelChannel(15)
parcel := new(Parcel)
parcel.Payload = []byte("test")
finished := false
go func() {
time.Sleep(time.Millisecond * 10)
if !finished {
t.Error("Channels caused a deadlock while writing")
}
}()
for i := 0; i < 10; i++ {
smolChannel.Send(parcel)
bigChannel.Send(parcel)
}
finished = true
if len(smolChannel) != 1 {
t.Errorf("Small channel has unexpected length: %d", len(smolChannel))
}
if len(bigChannel) != 10 {
t.Errorf("Big channel has unexpected length: %d", len(bigChannel))
}
}
func TestParcelChannel_Reader(t *testing.T) {
ch := newParcelChannel(100)
for i := 0; i < 100; i++ {
parcel := new(Parcel)
parcel.Payload = []byte{byte(i)}
ch.Send(parcel)
}
reader := ch.Reader()
received := 0
Outer:
for {
switch {
case len(reader) > 0:
p := <-reader
if p.Payload[0] != byte(received) {
t.Fatalf("Received parcel out of order, want %d got %d", received, int(p.Payload[0]))
}
received++
default:
if received != 100 {
t.Fatalf("Did not receive all parcels, only got %d", received)
}
break Outer
}
}
}