Repository navigation
Expand file tree
/
Copy pathgraph_test.go
More file actions
252 lines (196 loc) · 4.57 KB
/
Copy pathgraph_test.go
File metadata and controls
252 lines (196 loc) · 4.57 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package goflow
import (
"fmt"
"testing"
)
func newDoubleEcho() (*Graph, error) {
n := NewGraph()
// Components
e1 := new(echo)
e2 := new(echo)
// Structure
if err := n.Add("e1", e1); err != nil {
return nil, err
}
if err := n.Add("e2", e2); err != nil {
return nil, err
}
if err := n.Connect("e1", "Out", "e2", "In"); err != nil {
return nil, err
}
// Ports
n.MapInPort("In", "e1", "In")
n.MapOutPort("Out", "e2", "Out")
return n, nil
}
func TestSimpleGraph(t *testing.T) {
n, err := newDoubleEcho()
if err != nil {
t.Error(err)
return
}
testGraphWithNumberSequence(n, t)
}
func testGraphWithNumberSequence(n *Graph, t *testing.T) {
data := []int{7, 97, 16, 356, 81}
in := make(chan int)
out := make(chan int)
n.SetInPort("In", in)
n.SetOutPort("Out", out)
wait := Run(n)
go func() {
for _, n := range data {
in <- n
}
close(in)
}()
i := 0
for actual := range out {
expected := data[i]
if actual != expected {
t.Errorf("%d != %d", actual, expected)
}
i++
}
<-wait
}
func TestAddInvalidProcess(t *testing.T) {
s := struct{ Name string }{"This is not a Component"}
n := NewGraph()
err := n.Add("wrong", s)
if err == nil {
t.Errorf("Expected an error")
}
}
func TestRemove(t *testing.T) {
n := NewGraph()
e1 := new(echo)
if err := n.Add("e1", e1); err != nil {
t.Error(err)
return
}
if err := n.Remove("e1"); err != nil {
t.Error(err)
return
}
if err := n.Remove("e2"); err == nil {
t.Errorf("Expected an error")
return
}
}
// TestFanInCloseRace verifies that when multiple senders share an output
// channel (fan-in), no data race or double-close panic occurs when their
// closeProcOuts goroutines run concurrently. Regression test for the
// channel close race guarded by Graph.closeChan.
func TestFanInCloseRace(t *testing.T) {
const numSenders = 30
n := NewGraph()
// Create senders
senders := make([]*singleShot, numSenders)
for i := range numSenders {
senders[i] = new(singleShot)
if err := n.Add(fmt.Sprintf("s%d", i), senders[i]); err != nil {
t.Fatal(err)
}
}
// Create a single receiver (echo reads until its input closes)
r := new(echo)
if err := n.Add("r", r); err != nil {
t.Fatal(err)
}
// Fan-in: connect all sender outputs to the same receiver input
for i := range numSenders {
if err := n.Connect(fmt.Sprintf("s%d", i), "Out", "r", "In"); err != nil {
t.Fatal(err)
}
}
// Map inports
for i := range numSenders {
n.MapInPort(fmt.Sprintf("In%d", i), fmt.Sprintf("s%d", i), "In")
}
n.MapOutPort("Out", "r", "Out")
// Set up external channels
inChans := make([]chan int, numSenders)
for i := range numSenders {
inChans[i] = make(chan int)
if err := n.SetInPort(fmt.Sprintf("In%d", i), inChans[i]); err != nil {
t.Fatal(err)
}
}
out := make(chan int, numSenders)
if err := n.SetOutPort("Out", out); err != nil {
t.Fatal(err)
}
wait := Run(n)
// Send one value to each sender and close each input
for i := range numSenders {
inChans[i] <- i
close(inChans[i])
}
// Read all results
count := 0
for range out {
count++
}
if count != numSenders {
t.Errorf("expected %d values, got %d", numSenders, count)
}
<-wait
}
// TestIIPAndCloseOutRace verifies that when an IIP sends on a channel that
// is also an output port of another process, no double-close race occurs.
// The IIP goroutine and closeProcOuts both target the same underlying channel.
func TestIIPAndCloseOutRace(t *testing.T) {
n := NewGraph()
// A sender component
s := new(singleShot)
if err := n.Add("s", s); err != nil {
t.Fatal(err)
}
// Receiver
r := new(echo)
if err := n.Add("r", r); err != nil {
t.Fatal(err)
}
// Connect sender to receiver
if err := n.Connect("s", "Out", "r", "In"); err != nil {
t.Fatal(err)
}
// Add IIP to the same receiver input port (same underlying channel)
if err := n.AddIIP("r", "In", 99); err != nil {
t.Fatal(err)
}
// Map ports
n.MapInPort("In", "s", "In")
n.MapOutPort("Out", "r", "Out")
in := make(chan int)
out := make(chan int, 2)
if err := n.SetInPort("In", in); err != nil {
t.Fatal(err)
}
if err := n.SetOutPort("Out", out); err != nil {
t.Fatal(err)
}
wait := Run(n)
// Send to the sender
in <- 42
close(in)
count := 0
for range out {
count++
}
// Expect: 42 from sender + 99 from IIP
if count != 2 {
t.Errorf("expected 2 values, got %d", count)
}
<-wait
}
func RegisterTestGraph(f *Factory) error {
f.Register("doubleEcho", func() (any, error) {
return newDoubleEcho()
})
f.Annotate("doubleEcho", Annotation{
Description: "Contains a chain of two echo components",
})
return nil
}