-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnetwork_test.go
More file actions
130 lines (110 loc) · 3.16 KB
/
network_test.go
File metadata and controls
130 lines (110 loc) · 3.16 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
package flowbase
import (
"os"
"os/exec"
"sync"
"testing"
)
func TestSetWfName(t *testing.T) {
initTestLogs()
net := NewNetwork("TestNetwork")
expectedWfName := "TestNetwork"
if net.name != expectedWfName {
t.Errorf("Network name is wrong, should be %s but is %s\n", net.name, expectedWfName)
}
}
// --------------------------------------------------------------------------------
// MapToTag helper process
// --------------------------------------------------------------------------------
type MapToTags struct {
BaseProcess
mapFunc func(ip *Packet) map[string]string
}
func NewMapToTags(net *Network, name string, mapFunc func(ip *Packet) map[string]string) *MapToTags {
p := &MapToTags{
BaseProcess: NewBaseProcess(net, name),
mapFunc: mapFunc,
}
p.InitInPort(p, "in")
p.InitOutPort(p, "out")
net.AddProc(p)
return p
}
func (p *MapToTags) In() *InPort { return p.InPort("in") }
func (p *MapToTags) Out() *OutPort { return p.OutPort("out") }
func (p *MapToTags) Run() {
defer p.CloseOutPorts()
for ip := range p.In().Chan {
newTags := p.mapFunc(ip)
ip.AddTags(newTags)
p.Out().Send(ip)
}
}
// --------------------------------------------------------------------------------
// FileSource helper process
// --------------------------------------------------------------------------------
// FileSource is initiated with a set of file paths, which it will send as a
// stream of File IPs on its outport Out()
type FileSource struct {
BaseProcess
filePaths []string
}
// NewFileSource returns a new initialized FileSource process
func NewFileSource(net *Network, name string, filePaths ...string) *FileSource {
p := &FileSource{
BaseProcess: NewBaseProcess(net, name),
filePaths: filePaths,
}
p.InitOutPort(p, "out")
net.AddProc(p)
return p
}
// Out returns the out-port, on which file IPs based on the file paths the
// process was initialized with, will be retrieved.
func (p *FileSource) Out() *OutPort { return p.OutPort("out") }
// Run runs the FileSource process
func (p *FileSource) Run() {
defer p.CloseOutPorts()
for _, filePath := range p.filePaths {
newIP := NewPacket(filePath)
p.Out().Send(newIP)
}
}
// --------------------------------
// BogusProcess helper process
// --------------------------------
// A process with does just satisfy the Process interface, without doing any
// actual work.
type BogusProcess struct {
BaseProcess
name string
WasRun bool
WasRunLock sync.Mutex
}
func NewBogusProcess(name string) *BogusProcess {
return &BogusProcess{WasRun: false, name: name}
}
func (p *BogusProcess) Run() {
p.WasRunLock.Lock()
p.WasRun = true
p.WasRunLock.Unlock()
}
func (p *BogusProcess) Name() string {
return p.name
}
func (p *BogusProcess) Ready() bool {
return true
}
func ensureFailsProgram(testName string, crasher func(), t *testing.T) {
// After https://talks.golang.org/2014/testing.slide#23
if os.Getenv("BE_CRASHER") == "1" {
crasher()
}
cmd := exec.Command(os.Args[0], "-test.run="+testName)
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}