forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconnection_manager_test.go
102 lines (90 loc) · 2.51 KB
/
reconnection_manager_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
package pubnub
import (
//"fmt"
"github.com/sprucehealth/pubnub-go/tests/stubs"
"github.com/stretchr/testify/assert"
//"log"
//"os"
"testing"
"time"
)
func TestExponentialExhaustion(t *testing.T) {
assert := assert.New(t)
interceptor := stubs.NewInterceptor()
interceptor.AddStub(&stubs.Stub{
Method: "GET",
Path: "/time/0",
Query: "",
ResponseBody: `[15078947309567840]`,
IgnoreQueryKeys: []string{"uuid", "pnsdk"},
ResponseStatusCode: 200,
})
config := NewConfig()
pn := NewPubNub(config)
pn.Config.MaximumReconnectionRetries = 2
pn.Config.NonSubscribeRequestTimeout = 2
pn.Config.ConnectTimeout = 2
pn.Config.PNReconnectionPolicy = PNExponentialPolicy
pn.SetClient(interceptor.GetClient())
t1 := time.Now()
r := newReconnectionManager(pn)
reconnectionExhausted := false
r.HandleOnMaxReconnectionExhaustion(func() {
reconnectionExhausted = true
})
r.startHeartbeatTimer()
t2 := time.Now()
diff := t2.Unix() - t1.Unix()
assert.True((diff >= 11) && (diff <= 12))
assert.True(reconnectionExhausted)
r.stopHeartbeatTimer()
}
func TestLinearExhaustion(t *testing.T) {
assert := assert.New(t)
interceptor := stubs.NewInterceptor()
interceptor.AddStub(&stubs.Stub{
Method: "GET",
Path: "/time/0",
Query: "",
ResponseBody: `[15078947309567840]`,
IgnoreQueryKeys: []string{"uuid", "pnsdk"},
ResponseStatusCode: 200,
})
config := NewConfig()
pn := NewPubNub(config)
pn.Config.MaximumReconnectionRetries = 1
pn.Config.PNReconnectionPolicy = PNLinearPolicy
pn.SetClient(interceptor.GetClient())
t1 := time.Now()
r := newReconnectionManager(pn)
reconnectionExhausted := false
r.HandleOnMaxReconnectionExhaustion(func() {
reconnectionExhausted = true
})
r.startHeartbeatTimer()
t2 := time.Now()
diff := t2.Unix() - t1.Unix()
assert.True((diff >= 10) && (diff <= 11))
assert.True(reconnectionExhausted)
r.stopHeartbeatTimer()
}
func TestReconnect(t *testing.T) {
assert := assert.New(t)
config := NewConfig()
pn := NewPubNub(config)
pn.Config.MaximumReconnectionRetries = 1
pn.Config.PNReconnectionPolicy = PNLinearPolicy
//pn.Config.Log = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
r := newReconnectionManager(pn)
r.FailedCalls = 1
reconnected := false
doneReconnected := make(chan bool)
r.HandleReconnection(func() {
reconnected = true
doneReconnected <- true
})
go r.startHeartbeatTimer()
<-doneReconnected
assert.True(reconnected)
r.stopHeartbeatTimer()
}