-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter_test.go
125 lines (109 loc) · 3.1 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
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,
RegisterAddress: 1, Type: FieldTypeInt8,
},
{
ServerAddress: "", UnitID: 0, // ServerAddress is empty
RegisterAddress: 1, Type: FieldTypeInt8,
},
}
batched, err := split(given, "fc3_tcp")
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,
RegisterAddress: 1, Type: FieldTypeInt8,
},
}
batched, err := split(given, "fc3_tcp")
assert.NoError(t, err)
assert.Len(t, batched, 1)
pReq, _ := packet.NewReadHoldingRegistersRequestTCP(0, 1, 1)
pReq.TransactionID = 123
expect := RegisterRequest{
ServerAddress: ":502",
StartAddress: 1,
Request: pReq,
Fields: []Field{
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 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,
RegisterAddress: 1, Type: FieldTypeInt8,
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 118, Length: 11, Type: FieldTypeString, // 118 + 6 + 124
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 121, Type: FieldTypeUint64,
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 122, Type: FieldTypeFloat32,
},
}
batched, err := split(given, "fc3_tcp")
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_to2batches(t *testing.T) {
given := []Field{
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 1, Type: FieldTypeInt8,
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 119, Length: 15, Type: FieldTypeString, // 119,120,121,122, 123,124,125,126 == new request
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 121, Type: FieldTypeUint64, // 121,122,123,124
},
{
ServerAddress: ":502", UnitID: 0,
RegisterAddress: 122, Type: FieldTypeFloat32, // 122, 123
},
}
batched, err := split(given, "fc3_tcp")
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)
}