-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter_test.go
178 lines (156 loc) · 4.39 KB
/
splitter_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package modbus
import (
"github.com/aldas/go-modbus-client/packet"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSplit_validationError(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeInt8,
},
{
ServerAddress: "", UnitID: 0, // ServerAddress is empty
Address: 1, Type: FieldTypeInt8,
},
}
batched, err := split(given, splitToFC3TCP)
assert.EqualError(t, err, "field server address can not be empty")
assert.Nil(t, batched)
}
func TestSplit_single(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeInt8,
},
}
batched, err := split(given, splitToFC3TCP)
assert.NoError(t, err)
assert.Len(t, batched, 1)
pReq, _ := packet.NewReadHoldingRegistersRequestTCP(0, 1, 1)
pReq.TransactionID = 123
expect := BuilderRequest{
ServerAddress: ":502",
StartAddress: 1,
Request: pReq,
Fields: []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeInt8,
},
},
}
batched[0].Request.(*packet.ReadHoldingRegistersRequestTCP).TransactionID = 123
assert.Equal(t, expect, batched[0])
}
func TestSplit_many(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeInt8,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 118, Length: 11, Type: FieldTypeString, // 118 + 6 + 124
},
{
ServerAddress: ":502", UnitID: 0,
Address: 121, Type: FieldTypeUint64,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 122, Type: FieldTypeInt16,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 122, Type: FieldTypeFloat32,
},
}
batched, err := split(given, splitToFC3TCP)
assert.NoError(t, err)
assert.Len(t, batched, 1)
expect, _ := packet.NewReadHoldingRegistersRequestTCP(0, 1, 124)
expect.TransactionID = 123
batched[0].Request.(*packet.ReadHoldingRegistersRequestTCP).TransactionID = 123
assert.Equal(t, expect, batched[0].Request)
}
func TestSplit_to2RegisterBatches(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeInt8,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 119, Length: 15, Type: FieldTypeString, // 119,120,121,122, 123,124,125,126 == new request
},
{
ServerAddress: ":502", UnitID: 0,
Address: 121, Type: FieldTypeUint64, // 121,122,123,124
},
{
ServerAddress: ":502", UnitID: 0,
Address: 122, Type: FieldTypeFloat32, // 122, 123
},
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeCoil, // should be ignored
},
}
batched, err := split(given, splitToFC3TCP)
assert.NoError(t, err)
assert.Len(t, batched, 2)
expect, _ := packet.NewReadHoldingRegistersRequestTCP(0, 1, 1)
expect.TransactionID = 123
firstBatch := batched[0]
firstBatch.Request.(*packet.ReadHoldingRegistersRequestTCP).TransactionID = 123
assert.Equal(t, expect, firstBatch.Request)
assert.Len(t, firstBatch.Fields, 1)
expect2, _ := packet.NewReadHoldingRegistersRequestTCP(0, 119, 8)
expect2.TransactionID = 124
secondBatch := batched[1]
secondBatch.Request.(*packet.ReadHoldingRegistersRequestTCP).TransactionID = 124
assert.Equal(t, expect2, secondBatch.Request)
assert.Len(t, secondBatch.Fields, 3)
}
func TestSplit_to2CoilsBatches(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeCoil,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 1, Type: FieldTypeCoil, // at same place previous field
},
{
ServerAddress: ":502", UnitID: 0,
Address: 100, Type: FieldTypeCoil,
},
{
ServerAddress: ":502", UnitID: 0,
Address: 2001, Type: FieldTypeCoil, // should go to next batch
},
{
ServerAddress: ":502", UnitID: 0,
Address: 122, Type: FieldTypeFloat32, // should be ignored
},
}
batched, err := split(given, splitToFC1TCP)
assert.NoError(t, err)
assert.Len(t, batched, 2)
expect, _ := packet.NewReadCoilsRequestTCP(0, 1, 100)
expect.TransactionID = 123
firstBatch := batched[0]
firstBatch.Request.(*packet.ReadCoilsRequestTCP).TransactionID = 123
assert.Equal(t, expect, firstBatch.Request)
assert.Len(t, firstBatch.Fields, 3)
expect2, _ := packet.NewReadCoilsRequestTCP(0, 2001, 1)
expect2.TransactionID = 124
secondBatch := batched[1]
secondBatch.Request.(*packet.ReadCoilsRequestTCP).TransactionID = 124
assert.Equal(t, expect2, secondBatch.Request)
assert.Len(t, secondBatch.Fields, 1)
}