forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
179 lines (154 loc) · 2.68 KB
/
loader_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
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
// +build ignore
package goflow
import (
"sync"
"testing"
)
// Starter component fires the network
// by sending a number given in its constructor
// to its output port.
type starter struct {
Component
Start <-chan float64
Out chan<- int
}
func (s *starter) OnStart(num float64) {
s.Out <- int(num)
}
func newStarter() interface{} {
s := new(starter)
return s
}
func init() {
Register("starter", newStarter)
}
// SequenceGenerator generates a sequence of integers
// from 0 to a number passed to its input.
type sequenceGenerator struct {
Component
Num <-chan int
Sequence chan<- int
}
func (s *sequenceGenerator) OnNum(n int) {
for i := 1; i <= n; i++ {
s.Sequence <- i
}
}
func newSequenceGenerator() interface{} {
return new(sequenceGenerator)
}
func init() {
Register("sequenceGenerator", newSequenceGenerator)
}
// Summarizer component sums all its input packets and
// produces a sum output just before shutdown
type summarizer struct {
Component
In <-chan int
// Flush <-chan bool
Sum chan<- int
StateLock *sync.Mutex
current int
}
func newSummarizer() interface{} {
s := new(summarizer)
s.Component.Mode = ComponentModeSync
return s
}
func init() {
Register("summarizer", newSummarizer)
}
func (s *summarizer) OnIn(i int) {
s.current += i
}
func (s *summarizer) Finish() {
s.Sum <- s.current
}
var runtimeNetworkJSON = `{
"properties": {
"name": "runtimeNetwork"
},
"processes": {
"starter": {
"component": "starter"
},
"generator": {
"component": "sequenceGenerator"
},
"doubler": {
"component": "doubler"
},
"sum": {
"component": "summarizer"
}
},
"connections": [
{
"data": 10,
"tgt": {
"process": "starter",
"port": "Start"
}
},
{
"src": {
"process": "starter",
"port": "Out"
},
"tgt": {
"process": "generator",
"port": "Num"
}
},
{
"src": {
"process": "generator",
"port": "Sequence"
},
"tgt": {
"process": "doubler",
"port": "In"
}
},
{
"src": {
"process": "doubler",
"port": "Out"
},
"tgt": {
"process": "sum",
"port": "In"
}
}
],
"exports": [
{
"private": "starter.Start",
"public": "Start"
},
{
"private": "sum.Sum",
"public": "Out"
}
]
}`
func TestRuntimeNetwork(t *testing.T) {
net := ParseJSON([]byte(runtimeNetworkJSON))
if net == nil {
t.Error("Could not load JSON")
}
start := make(chan float64)
out := make(chan int)
net.SetInPort("Start", start)
net.SetOutPort("Out", out)
RunNet(net)
// Wait for the network setup
<-net.Ready()
// Close start to halt it normally
close(start)
i := <-out
if i != 110 {
t.Errorf("Wrong result: %d != 110", i)
}
<-net.Wait()
}