-
Notifications
You must be signed in to change notification settings - Fork 12
/
clientPing_test.go
75 lines (59 loc) · 1.44 KB
/
clientPing_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
package kratos
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/xmidt-org/sallust"
)
var c = ClientConfig{
DeviceName: "mac:ffffff112233",
HandlePingMiss: func() error {
fmt.Println("hi")
pingMissCount++
return nil
},
ClientLogger: nil,
PingConfig: PingConfig{PingWait: 1000000000, MaxPingMiss: 0},
}
var pingMissCount = 0
func TestPingDone(t *testing.T) {
pingMissCount = 0
newClient := &client{
deviceID: c.DeviceName,
handlePingMiss: c.HandlePingMiss,
done: make(chan struct{}, 1),
logger: sallust.Default(),
pingConfig: c.PingConfig,
wg: sync.WaitGroup{},
}
newClient.wg.Add(1)
pinged := make(chan string)
pingTimer := time.NewTimer(newClient.pingConfig.PingWait)
close(newClient.done)
newClient.checkPing(pingTimer, pinged)
newClient.wg.Wait()
assert.Equal(t, pingMissCount, 0)
}
func TestPing(t *testing.T) {
pingMissCount = 0
newClient := &client{
deviceID: c.DeviceName,
handlePingMiss: c.HandlePingMiss,
done: make(chan struct{}, 1),
logger: sallust.Default(),
pingConfig: c.PingConfig,
wg: sync.WaitGroup{},
}
newClient.wg.Add(1)
pinged := make(chan string)
pingTimer := time.NewTimer(newClient.pingConfig.PingWait)
go func() {
pinged <- "ping"
}()
time.Sleep(50)
newClient.checkPing(pingTimer, pinged)
newClient.wg.Wait()
assert.Equal(t, pingMissCount, 1)
}