-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmanager_test.go
More file actions
190 lines (167 loc) · 5.01 KB
/
manager_test.go
File metadata and controls
190 lines (167 loc) · 5.01 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
package gobot
import (
"errors"
"fmt"
"log"
"os"
"testing"
"time"
multierror "github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func initTestManager() *Manager {
log.SetOutput(&NullReadWriteCloser{})
g := NewManager()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot(""))
return g
}
func initTestManager1Robot() *Manager {
log.SetOutput(&NullReadWriteCloser{})
g := NewManager()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot99"))
return g
}
func TestNullReadWriteCloser(t *testing.T) {
n := &NullReadWriteCloser{}
i, _ := n.Write([]byte{1, 2, 3})
assert.Equal(t, 3, i)
i, _ = n.Read(make([]byte, 10))
assert.Equal(t, 10, i)
require.NoError(t, n.Close())
}
func TestManagerRobot(t *testing.T) {
g := initTestManager()
assert.Equal(t, "Robot1", g.Robot("Robot1").Name)
assert.Equal(t, (*Robot)(nil), g.Robot("Robot4"))
assert.Equal(t, (Device)(nil), g.Robot("Robot4").Device("Device1"))
assert.Equal(t, (Connection)(nil), g.Robot("Robot4").Connection("Connection1"))
assert.Equal(t, (Device)(nil), g.Robot("Robot1").Device("Device4"))
assert.Equal(t, "Device1", g.Robot("Robot1").Device("Device1").Name())
assert.Equal(t, 3, g.Robot("Robot1").Devices().Len())
assert.Equal(t, (Connection)(nil), g.Robot("Robot1").Connection("Connection4"))
assert.Equal(t, 3, g.Robot("Robot1").Connections().Len())
}
func TestManagerToJSON(t *testing.T) {
g := initTestManager()
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := NewJSONManager(g)
assert.Len(t, json.Robots, g.Robots().Len())
assert.Len(t, json.Commands, len(g.Commands()))
}
func TestManagerStart(t *testing.T) {
g := initTestManager()
require.NoError(t, g.Start())
require.NoError(t, g.Stop())
assert.False(t, g.Running())
}
func TestManagerStartAutoRun(t *testing.T) {
g := NewManager()
g.AddRobot(newTestRobot("Robot99"))
go func() { _ = g.Start() }()
time.Sleep(10 * time.Millisecond)
assert.True(t, g.Running())
// stop it
require.NoError(t, g.Stop())
assert.False(t, g.Running())
}
func TestManagerStartDriverErrors(t *testing.T) {
// arrange
g := initTestManager1Robot()
var ec int
es := [4]error{
nil,
errors.New("driver start error 1"),
errors.New("driver start error 2"),
errors.New("driver start error 3"),
}
testDriverStart = func() error {
ec++
return es[ec]
}
defer func() { testDriverStart = func() error { return nil } }()
var want error
want = multierror.Append(want, fmt.Errorf("'Device1' start error: %w", es[1]))
want = multierror.Append(want, fmt.Errorf("'Device2' start error: %w", es[2]))
want = multierror.Append(want, fmt.Errorf("'' start error: %w", es[3]))
// act & assert
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
}
func TestManagerHaltFromRobotDriverErrors(t *testing.T) {
// arrange
g := initTestManager1Robot()
es := [4]error{
nil,
errors.New("driver halt error 1"),
errors.New("driver halt error 2"),
errors.New("driver halt error 3"),
}
var ec int
testDriverHalt = func() error {
ec++
return es[ec]
}
defer func() { testDriverHalt = func() error { return nil } }()
var want error
want = multierror.Append(want, fmt.Errorf("'Device1' halt error: %w", es[1]))
want = multierror.Append(want, fmt.Errorf("'Device2' halt error: %w", es[2]))
want = multierror.Append(want, fmt.Errorf("'' halt error: %w", es[3]))
// act & assert
assert.Equal(t, want, g.Start())
}
func TestManagerStartRobotAdaptorErrors(t *testing.T) {
// arrange
g := initTestManager1Robot()
es := [4]error{
nil,
errors.New("adaptor start error 1"),
errors.New("adaptor start error 2"),
errors.New("adaptor start error 3"),
}
var ec int
testAdaptorConnect = func() error {
ec++
return es[ec]
}
defer func() { testAdaptorConnect = func() error { return nil } }()
var want error
want = multierror.Append(want, fmt.Errorf("'Connection1' connect error: %w", es[1]))
want = multierror.Append(want, fmt.Errorf("'Connection2' connect error: %w", es[2]))
want = multierror.Append(want, fmt.Errorf("'' connect error: %w", es[3]))
// act & assert
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
}
func TestManagerFinalizeErrors(t *testing.T) {
// arrange
g := initTestManager1Robot()
es := [4]error{
nil,
errors.New("adaptor finalize error 1"),
errors.New("adaptor finalize error 2"),
errors.New("adaptor finalize error 3"),
}
var ec int
testAdaptorFinalize = func() error {
ec++
return fmt.Errorf("adaptor finalize error %d", ec)
}
defer func() { testAdaptorFinalize = func() error { return nil } }()
var want error
want = multierror.Append(want, fmt.Errorf("'Connection1' finalize error: %w", es[1]))
want = multierror.Append(want, fmt.Errorf("'Connection2' finalize error: %w", es[2]))
want = multierror.Append(want, fmt.Errorf("'' finalize error: %w", es[3]))
// act & assert
assert.Equal(t, want, g.Start())
}