-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathorderliness_test.go
130 lines (104 loc) · 2.55 KB
/
orderliness_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
package pub0sub
import (
"bytes"
"context"
"encoding/binary"
"io"
"testing"
"time"
"github.com/itzmeanjan/pub0sub/hub"
"github.com/itzmeanjan/pub0sub/ops"
"github.com/itzmeanjan/pub0sub/publisher"
"github.com/itzmeanjan/pub0sub/subscriber"
)
func prepareData(w io.Writer, num uint64) error {
return binary.Write(w, binary.BigEndian, num)
}
func recoverData(r io.Reader) (uint64, error) {
var num uint64
if err := binary.Read(r, binary.BigEndian, &num); err != nil {
return 0, err
}
return num, nil
}
func TestDeliveryOrderliness(t *testing.T) {
addr := "127.0.0.1:0"
proto := "tcp"
capacity := uint64(256)
topic_1 := "topic_1"
topics := []string{topic_1}
end := uint64(1_000_000)
delay := time.Duration(100) * time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
h, err := hub.New(ctx, addr, capacity)
if err != nil {
t.Fatalf("Failed to start Hub : %s\n", err.Error())
}
pub, err := publisher.New(ctx, proto, h.Addr())
if err != nil {
t.Fatalf("Failed to start publisher : %s\n", err.Error())
}
sub, err := subscriber.New(ctx, proto, h.Addr(), capacity, topics...)
if err != nil {
t.Fatalf("Failed to start subscriber %s\n", err.Error())
}
pubDone := make(chan struct{})
go func() {
var current uint64 = 1
buf := new(bytes.Buffer)
msg := ops.Msg{Topics: topics}
for ; current <= end; current++ {
if err := prepareData(buf, current); err != nil {
t.Errorf("Failed to prepare data : %s\n", err.Error())
}
msg.Data = buf.Bytes()
if _, err := pub.Publish(&msg); err != nil {
t.Errorf("Failed to publish : %s\n", err.Error())
}
buf.Reset()
}
close(pubDone)
}()
subDone := make(chan struct{})
go func() {
var current uint64 = 1
OUT:
for {
select {
case <-ctx.Done():
break OUT
case <-sub.Watch():
msg := sub.Next()
if msg == nil {
t.Errorf("Received nil msg\n")
break
}
num, err := recoverData(bytes.NewReader(msg.Data))
if err != nil {
t.Errorf("Failed to recover from stream : %s\n", err.Error())
}
if current != num {
t.Errorf("Expected to receive %d, received : %d\n", current, num)
}
current++
if num >= end {
break OUT
}
}
}
close(subDone)
}()
<-pubDone
<-subDone
if sub.Queued() {
t.Fatalf("Expected zero queued messages\n")
}
if _, err := sub.UnsubscribeAll(); err != nil {
t.Errorf("Failed to unsubscribe : %s\n", err.Error())
}
if err := sub.Disconnect(); err != nil {
t.Errorf("Failed to disconnect subscriber : %s\n", err.Error())
}
cancel()
<-time.After(delay)
}