-
Notifications
You must be signed in to change notification settings - Fork 10
/
failover_server_test.go
193 lines (170 loc) · 5.16 KB
/
failover_server_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package modbusone_test
import (
"fmt"
"io"
"os"
"sync/atomic"
"testing"
"time"
"github.com/xiegeo/coloredgoroutine"
. "github.com/xiegeo/modbusone"
)
type counter struct {
reads int64 // first for alignment
writes int64
*Stats
}
func (c *counter) total() int64 {
return c.Stats.TotalDrops() + atomic.LoadInt64(&c.reads) + atomic.LoadInt64(&c.writes)
}
func (c *counter) reset() {
c.Stats.Reset()
atomic.StoreInt64(&c.reads, 0)
atomic.StoreInt64(&c.writes, 0)
}
func (c *counter) same(to *counter) bool {
return atomic.LoadInt64(&c.reads) == atomic.LoadInt64(&to.reads) &&
atomic.LoadInt64(&c.writes) == atomic.LoadInt64(&to.writes)
}
func (c *counter) sameInverted(to *counter) bool {
return atomic.LoadInt64(&c.reads) == atomic.LoadInt64(&to.writes) &&
atomic.LoadInt64(&c.writes) == atomic.LoadInt64(&to.reads)
}
func (c *counter) String() string {
return fmt.Sprintf("reads:%v writes:%v drops:%v", atomic.LoadInt64(&c.reads), atomic.LoadInt64(&c.writes), c.TotalDrops())
}
func newTestHandler(name string, t *testing.T) ([]uint16, *SimpleHandler, *counter) {
var holdingRegisters [100]uint16
count := counter{}
shA := &SimpleHandler{
ReadHoldingRegisters: func(address, quantity uint16) ([]uint16, error) {
t.Logf("Read %s %v, quantity %v\n", name, address, quantity)
atomic.AddInt64(&count.reads, int64(quantity))
return holdingRegisters[address : address+quantity], nil
},
WriteHoldingRegisters: func(address uint16, values []uint16) error {
t.Logf("Write %s %v, quantity %v\n", name, address, len(values))
atomic.AddInt64(&count.writes, int64(len(values)))
for i, v := range values {
holdingRegisters[address+uint16(i)] = v
}
return nil
},
}
return holdingRegisters[:], shA, &count
}
func setDelays(f *FailoverSerialConn) {
f.SecondaryDelay = serverProcessingTime / 2
f.MissDelay = serverProcessingTime
}
func connectToMockServers(t *testing.T, slaveID byte) (*RTUClient, *FailoverSerialConn, *counter, *counter, *counter, func()) {
// pipes
ra, wa := io.Pipe() // server a
rb, wb := io.Pipe() // server b
rc, wc := io.Pipe() // client
// everyone writes to everyone else
wfa := io.MultiWriter(wb, wc) // write from a, etc...
wfb := io.MultiWriter(wa, wc)
wfc := io.MultiWriter(wa, wb)
sa := NewFailoverConn(newMockSerial("sa", ra, wfa, ra), false, false) // server a connection
sb := NewFailoverConn(newMockSerial("sb", rb, wfb, rb), true, false) // server b connection
cc := newMockSerial("cc", rc, wfc, rc) // client connection
serverA := NewRTUServer(sa, slaveID)
serverB := NewRTUServer(sb, slaveID)
client := NewRTUClient(cc, slaveID)
// faster timeouts during testing
client.SetServerProcessingTime(serverProcessingTime)
setDelays(sa)
setDelays(sb)
_, shA, countA := newTestHandler("server A", t)
countA.Stats = sa.Stats()
_, shB, countB := newTestHandler("server B", t)
countB.Stats = sb.Stats()
holdingRegistersC, shC, countC := newTestHandler("client", t)
countC.Stats = cc.Stats()
for i := range holdingRegistersC {
holdingRegistersC[i] = uint16(i + 1<<8)
}
go serverA.Serve(shA)
go serverB.Serve(shB)
go client.Serve(shC)
return client, sa, countA, countB, countC, func() {
serverA.Close()
serverB.Close()
client.Close()
}
}
func TestFailoverServer(t *testing.T) {
id := byte(0x77)
client, pc, countA, countB, countC, closeServers := connectToMockServers(t, id)
defer closeServers()
exCount := counter{Stats: &Stats{}}
resetCounts := func() {
exCount.reset()
countA.reset()
countB.reset()
countC.reset()
}
type tc struct {
fc FunctionCode
size uint16
}
testCases := []tc{
{FcWriteSingleRegister, 5},
{FcWriteMultipleRegisters, 5},
{FcReadHoldingRegisters, 5},
}
_ = os.Stdout
_ = coloredgoroutine.Colors
SetDebugOut(coloredgoroutine.Colors(os.Stdout))
defer func() { SetDebugOut(nil) }()
t.Run("cold start", func(t *testing.T) {
reqs, err := MakePDURequestHeadersSized(FcReadHoldingRegisters, 0, 1, 1, nil)
if err != nil {
t.Fatal(err)
}
_, err = DoTransactions(client, id, reqs)
if err == nil {
t.Fatal("cold start, not expecting any active servers")
}
for i := 0; i < 3; /*ServerMissesMax*/ i++ {
// activates server
DoTransactions(client, id, reqs)
}
time.Sleep(serverProcessingTime * 2)
if !pc.IsActive() {
t.Fatal("primary servers should be active")
}
})
for i, ts := range testCases {
t.Run(fmt.Sprintf("normal %v fc:%v size:%v", i, ts.fc, ts.size), func(t *testing.T) {
resetCounts()
reqs, err := MakePDURequestHeadersSized(ts.fc, 0, ts.size, 1, nil)
if err != nil {
t.Fatal(err)
}
_, err = DoTransactions(client, id, reqs)
if err != nil {
t.Fatal(err)
}
time.Sleep(serverProcessingTime * 2)
if ts.fc.IsWriteToServer() {
exCount.writes += int64(ts.size)
} else {
exCount.reads += int64(ts.size)
}
if !exCount.sameInverted(countC) {
t.Error("client counter ", countC)
t.Error("expected (inverted)", exCount)
}
if !exCount.same(countA) {
t.Error("server a counter", countA)
t.Error("expected ", exCount)
}
if !exCount.same(countB) {
t.Error("server b counter", countB)
t.Error("expected ", exCount)
}
})
}
}