-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
manager_test.go
163 lines (138 loc) · 3.94 KB
/
manager_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
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) {
g := initTestManager1Robot()
e := errors.New("driver start error 1")
testDriverStart = func() error {
return e
}
var want error
want = multierror.Append(want, e)
want = multierror.Append(want, e)
want = multierror.Append(want, e)
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
testDriverStart = func() error { return nil }
}
func TestManagerHaltFromRobotDriverErrors(t *testing.T) {
g := initTestManager1Robot()
var ec int
testDriverHalt = func() error {
ec++
return fmt.Errorf("driver halt error %d", ec)
}
defer func() { testDriverHalt = func() error { return nil } }()
var want error
for i := 1; i <= 3; i++ {
e := fmt.Errorf("driver halt error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
}
func TestManagerStartRobotAdaptorErrors(t *testing.T) {
g := initTestManager1Robot()
var ec int
testAdaptorConnect = func() error {
ec++
return fmt.Errorf("adaptor start error %d", ec)
}
defer func() { testAdaptorConnect = func() error { return nil } }()
var want error
for i := 1; i <= 3; i++ {
e := fmt.Errorf("adaptor start error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
testAdaptorConnect = func() error { return nil }
}
func TestManagerFinalizeErrors(t *testing.T) {
g := initTestManager1Robot()
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
for i := 1; i <= 3; i++ {
e := fmt.Errorf("adaptor finalize error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
}