-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_pong_test.go
74 lines (65 loc) · 1.7 KB
/
ping_pong_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
//go:build longtime
// +build longtime
package art
import (
"fmt"
"testing"
)
func Test_NoTimeout_When_ClientSendPingSeconds_LessThan_ServerWaitPingSeconds(t *testing.T) {
clientSendPingSeconds := 1
serverWaitPingSeconds := 2
err := pingpong(t, clientSendPingSeconds, serverWaitPingSeconds)
if err != nil {
t.Errorf("pingping should success: %v", err)
return
}
}
func Test_HasTimeout_When_ClientSendPingSeconds_GreaterThan_ServerWaitPingSeconds(t *testing.T) {
clientSendPingSeconds := 2
serverWaitPingSeconds := 1
err := pingpong(t, clientSendPingSeconds, serverWaitPingSeconds)
if err == nil {
t.Errorf("pingping should timeout")
}
// t.Logf("pingping fail: %v", err)
}
func pingpong(t *testing.T, clientSendPingSeconds, serverWaitPingSeconds int) error {
sendPingCount := 0
targetCount := 2
isStop := func() bool { return sendPingCount == targetCount }
clientWaitPong := NewWaitPingPong()
serverWaitPing := NewWaitPingPong()
notify := make(chan error, 2)
client := func() {
clientSendPing := func() error {
sendPingCount++
t.Logf("client send ping")
serverWaitPing.Ack()
t.Logf("server ack ping")
return nil
}
err := SendPingWaitPong(clientSendPingSeconds, clientSendPing, clientWaitPong, isStop)
if err != nil {
notify <- fmt.Errorf("client: %w", err)
}
notify <- nil
}
server := func() {
serverSendPong := func() error {
t.Logf("server send pong")
clientWaitPong.Ack()
t.Logf("client ack pong")
t.Logf("")
return nil
}
err := WaitPingSendPong(serverWaitPingSeconds, serverWaitPing, serverSendPong, isStop)
if err != nil {
notify <- fmt.Errorf("server: %w", err)
return
}
notify <- nil
}
go client()
go server()
return <-notify
}