forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_control_test.go
More file actions
146 lines (119 loc) · 3.68 KB
/
transfer_control_test.go
File metadata and controls
146 lines (119 loc) · 3.68 KB
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
package connect
import (
"context"
"fmt"
mathrand "math/rand"
"testing"
"time"
"github.com/go-playground/assert/v2"
"github.com/urnetwork/connect/protocol"
)
func TestControlSync(t *testing.T) {
// control sync to flood control messages,
// drop transports for longer than ack timeout
if testing.Short() {
t.Skip("skipping testing in short mode")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
timeout := 60 * time.Second
dropTimeout := 5 * time.Second
allowTimeout := 2 * time.Second
ackTimeout := 100 * time.Millisecond
sendDelay := 20 * time.Millisecond
k := 4
b := 1000
clientASettings := DefaultClientSettings()
clientASettings.SendBufferSettings.AckTimeout = ackTimeout
clientA := NewClient(ctx, NewId(), NewNoContractClientOob(), clientASettings)
controlClientA := NewClientWithDefaults(ctx, ControlId, NewNoContractClientOob())
controlClientA.ContractManager().AddNoContractPeer(clientA.ClientId())
controlSyncM1 := NewControlSync(ctx, clientA, "m1")
receive := make(chan *protocol.SimpleMessage)
// on receive a test message, set the channel
controlClientA.AddReceiveCallback(func(source TransferPath, frames []*protocol.Frame, provideMode protocol.ProvideMode) {
for _, frame := range frames {
m, err := FromFrame(frame)
assert.Equal(t, err, nil)
switch v := m.(type) {
case *protocol.SimpleMessage:
select {
case <-ctx.Done():
case receive <- v:
case <-time.After(timeout):
t.FailNow()
}
}
}
})
for i := range k {
go func() {
for j := range b {
frame, err := ToFrame(&protocol.SimpleMessage{
MessageIndex: uint32(i*b + j),
}, DefaultProtocolVersion)
assert.Equal(t, err, nil)
controlSyncM1.Send(
frame,
nil,
nil,
)
select {
case <-time.After(time.Duration(mathrand.Int63n(int64(sendDelay)))):
case <-ctx.Done():
return
}
}
}()
go func() {
for {
// wait
// create transport
// wait
// drop transport
select {
case <-ctx.Done():
case <-time.After(time.Duration(mathrand.Int63n(int64(dropTimeout)))):
}
clientASendTransport := NewSendGatewayTransport()
clientAReceiveTransport := NewReceiveGatewayTransport()
controlClientASendTransport := NewSendGatewayTransport()
controlClientAReceiveTransport := NewReceiveGatewayTransport()
clientASend := make(chan []byte)
clientAReceive := make(chan []byte)
clientA.RouteManager().UpdateTransport(clientASendTransport, []Route{clientASend})
clientA.RouteManager().UpdateTransport(clientAReceiveTransport, []Route{clientAReceive})
controlClientA.RouteManager().UpdateTransport(controlClientASendTransport, []Route{clientAReceive})
controlClientA.RouteManager().UpdateTransport(controlClientAReceiveTransport, []Route{clientASend})
select {
case <-ctx.Done():
case <-time.After(time.Duration(mathrand.Int63n(int64(allowTimeout)))):
}
clientA.RouteManager().UpdateTransport(clientASendTransport, nil)
clientA.RouteManager().UpdateTransport(clientAReceiveTransport, nil)
controlClientA.RouteManager().UpdateTransport(controlClientASendTransport, nil)
controlClientA.RouteManager().UpdateTransport(controlClientAReceiveTransport, nil)
}
}()
func() {
p := uint32(0)
for {
// select from message channel
// when select message k + b, stop
// if timeout, error
select {
case m := <-receive:
end := uint32(b*i + b - 1)
fmt.Printf("[csync]%d/%d (%d)\n", m.MessageIndex, end, p)
assert.Equal(t, p <= m.MessageIndex, true)
p = m.MessageIndex
if m.MessageIndex == end {
return
}
case <-time.After(timeout):
t.FailNow()
}
}
}()
}
}