-
Notifications
You must be signed in to change notification settings - Fork 13
/
gozwave_test.go
131 lines (109 loc) · 3.05 KB
/
gozwave_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
package gozwave
import (
"fmt"
"io"
"os"
"testing"
"text/tabwriter"
"time"
"github.com/sirupsen/logrus"
"github.com/stampzilla/gozwave/serialapi"
"github.com/stretchr/testify/assert"
)
func TestConnect(t *testing.T) {
if testing.Verbose() {
logrus.SetLevel(logrus.DebugLevel)
}
mockPO := newMockPortOpener()
controller, err := ConnectWithCustomPortOpener("/test", "", mockPO)
assert.NoError(t, err)
//log.Println("controller: ", controller)
reply(t, mockPO.mockSerial.getFromWrite, mockPO.mockSerial.sendToRead) // Start up a conversation loop
// TODO: make something better than a sleep here
time.Sleep(100 * time.Millisecond)
n := controller.Nodes.Get(8)
assert.NotNil(t, n)
if assert.NotNil(t, n.ProtocolInfo()) {
assert.Equal(t,
serialapi.FuncGetNodeProtocolInfo{
Listening: false,
Routing: true,
Beaming: true,
Version: 0x04,
Flirs: false,
Security: false,
MaxBaud: 40000,
Basic: 0x04,
Generic: 0x40,
Specific: 0x0,
},
*n.ProtocolInfo(),
)
}
}
func reply(t *testing.T, c chan []byte, w chan string) {
replies := map[string][]string{
"06": []string{},
"01030002fe": []string{ // Request discovery nodes
"06", // Ack
"0125010205001d8000000000000000000000000000000000000000000000000000000000050044", // Answer with node 8 active
},
"0104004108b2": []string{ // Request node information node 8
"06", // Ack
"01080141539c0004403c", // Answer with node information
},
}
reads := map[string]int{} // Count the number of reads received
// Receive messages from gozwave and answer
for {
select {
case v := <-c:
s := fmt.Sprintf("%x", v) // Encode to hex string
if r, ok := replies[s]; ok {
// Keep a count of how many times we have received each message
if _, ok := reads[s]; !ok {
reads[s] = 0
}
reads[s]++
for _, rep := range r {
w <- rep // Send the response
}
if len(reads) == len(replies) { // We got all messages expected. Exit the loop
//<-time.After(time.Second)
return
}
break
}
t.Fatalf("Unexpected read %s", s)
return
case <-time.After(time.Second * 3):
fmt.Println("Message counts\n----------------------------------")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.AlignRight|tabwriter.Debug)
for k := range replies {
fmt.Fprintf(w, "%d\t%s\n", reads[k], k)
}
w.Flush()
fmt.Println("----------------------------------")
for k := range replies {
if _, ok := reads[k]; !ok {
t.Errorf("Test timeout. Have not received message %s", k)
}
}
return
}
}
}
type poMock struct{}
func (po *poMock) Open() (io.ReadWriteCloser, error) {
return nil, fmt.Errorf("custom mock connect error")
}
func TestConnectWithCustomPortOpener(t *testing.T) {
// Test failed portopener
c, err := ConnectWithCustomPortOpener("", "", nil)
assert.EqualError(t, err, "portopener is nil")
assert.Nil(t, c)
// Test failed connect
c, err = ConnectWithCustomPortOpener("", "", &poMock{})
assert.EqualError(t, err, "custom mock connect error")
assert.Nil(t, c)
}