-
Notifications
You must be signed in to change notification settings - Fork 1
/
route_builder_test.go
121 lines (100 loc) · 2 KB
/
route_builder_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
package go_data_routing
import (
"context"
"testing"
"time"
)
type Probe struct {
x string
}
func (r *Probe) Run() {}
type Probe2 struct {
x string
}
func (r *Probe2) Run() {
time.Sleep(200 * time.Millisecond)
}
func TestEnrich(t *testing.T) {
// TestRaceCondition
for i := 0; i < 100; i++ {
Enrich(t, 1*time.Second, 10*time.Millisecond)
}
// TestNormalMode
for i := 0; i < 5; i++ {
Enrich(t, 100*time.Millisecond, 2*time.Second)
}
EnrichTestOfDeadlock(t, 500*time.Millisecond, 10*time.Second)
}
func Enrich(t *testing.T, sendMsgPeriod, cancelAfter time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
rc := NewRouterContext(ctx)
rc.Route("main").
Source(func(n *Node) {
select {
case i, _ := <-n.Input:
if i.Type == Stop {
return
}
case <-time.After(sendMsgPeriod):
n.Send(Exchange{Msg: &Probe{"a"}})
}
}).
To("enrich-rt").
Sink(func(e Exchange) error {
//fmt.Println("Sink >", e.Initiator)
return nil
})
rc.Route("enrich-rt").
Process(1)
go func() {
time.Sleep(cancelAfter)
cancel()
}()
rc.Run()
// check all nodes are stopped by now
for _, r := range rc.routes {
for _, n := range r.nodes {
if !n.stopped {
t.Fail()
}
}
}
}
func EnrichTestOfDeadlock(t *testing.T, sendMsgPeriod, cancelAfter time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
rc := NewRouterContext(ctx)
rc.Route("main").
Source(func(n *Node) {
for i := 0; i < 10; i++ {
select {
case i, _ := <-n.Input:
if i.Type == Stop {
return
}
case <-time.After(sendMsgPeriod):
n.Send(Exchange{Msg: &Probe2{"a"}})
}
}
}).
To("enrich-rt").
Sink(func(e Exchange) error {
//fmt.Println("Sink >", e)
return nil
})
rc.Route("enrich-rt").
Process(1)
go func() {
time.Sleep(cancelAfter)
cancel()
}()
rc.Run()
rc.Print()
// check all nodes are stopped by now
for _, r := range rc.routes {
for _, n := range r.nodes {
if !n.stopped {
t.Fail()
}
}
}
}