-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client_test.go
144 lines (137 loc) · 2.87 KB
/
Client_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
package modbus
import (
"testing"
"time"
)
func TestClient(t *testing.T) {
t.Run("GetClientHandle", func(t *testing.T) {
for _, cs := range testConSettings {
var validStr string
if cs.isValid {
validStr = "valid/"
} else {
validStr = "invalid/"
}
t.Run(validStr+ModeNames[cs.Mode], func(t *testing.T) {
testGetClientHandle(t, cs)
})
}
})
t.Run("Send", runSendTests)
// Shutdown the clntMngr, this is just for testing purposes to avoid a
// data race
clntMngr.exit <- true
time.Sleep(50 * time.Millisecond)
if len(clntMngr.clients) > 0 {
t.Fatal("Clients did not shutdown on close")
}
}
func testGetClientHandle(t *testing.T, cs testConnectionSettings) {
t.Parallel()
done := make(chan interface{})
var ch ClientHandle
var err error
go func() {
ch, err = GetClientHandle(cs.ConnectionSettings)
done <- true
}()
select {
case <-done:
case <-time.After(1000 * time.Millisecond):
t.Fatal("GetClient timed out")
}
if cs.isValid {
if nil != err {
t.Fatal(err)
}
if ch.GetConnectionSettings() != cs.ConnectionSettings {
t.Errorf("Incorrect ConnectionSettings, want %v got %v",
cs.ConnectionSettings, ch.GetConnectionSettings())
}
cs := cs.ConnectionSettings
cs.Timeout += 500
_, err := GetClientHandle(cs)
if nil == err {
t.Error("Altered ConnectionSettings err is nil")
}
if ch.Close() != nil {
t.Error(err)
}
if ch.Close() == nil {
t.Error("Second Close is nil")
}
if _, err := ch.Send(testQueries[0].Query); nil == err {
t.Error("Send after Close returned " +
"nil error")
}
} else {
if nil == err {
t.Error("Did not return an error")
}
}
}
func testSend(t *testing.T, cs ConnectionSettings, q testQuery) {
t.Parallel()
ch, err := GetClientHandle(cs)
if nil != err {
t.Fatal(err)
}
done := make(chan interface{})
var data []byte
go func() {
data, err = ch.Send(q.Query)
done <- true
}()
var timedOut bool
select {
case <-done:
case <-time.After(1000 * time.Millisecond):
t.Error("Send timed out")
timedOut = true
}
if !timedOut {
if q.isValid {
if nil != err {
t.Error(err)
}
if nil == data {
t.Error("Response data is nil")
}
} else {
if nil == err {
t.Error("Error is nil")
}
if nil != data {
t.Error("Response data is not nil")
}
}
}
if err := ch.Close(); nil != err {
t.Error(err)
}
}
func runSendTests(t *testing.T) {
for _, cs := range testConSettings {
if cs.isValid {
for _, q := range testQueries {
var validStr string
if q.isValid {
validStr = "valid"
} else {
validStr = "invalid"
}
fName, ok := FunctionNames[q.FunctionCode]
if !ok {
fName = "Invalid FunctionCode"
}
testName := ModeNames[cs.Mode] + "/" +
validStr + "/" +
fName + "/" +
q.test
t.Run(testName, func(t *testing.T) {
testSend(t, cs.ConnectionSettings, q)
})
}
}
}
}