-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeout_test.go
169 lines (148 loc) · 3.65 KB
/
timeout_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
// Copyright (c) 2015-2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.
package longpoll_test
import (
"testing"
"time"
"github.com/teris-io/longpoll"
)
func TestTimeout_onNewTimeout_success(t *testing.T) {
tor, err := longpoll.NewTimeout(time.Minute, nil)
if err != nil {
t.Error("no error expected")
}
if !tor.IsAlive() {
t.Error("tor not alive on start")
}
}
func TestTimeout_onNewTimeout_whenZeroDuration_error(t *testing.T) {
var tm time.Duration
_, err := longpoll.NewTimeout(tm, nil)
if err == nil {
t.Error("error expected")
}
}
func TestTimeout_onMustNewTimeout_success(t *testing.T) {
tor := longpoll.MustNewTimeout(time.Minute, nil)
if !tor.IsAlive() {
t.Error("tor not alive on start")
}
}
func TestTimeout_onMustNewTimeout_whenZeroDuration_panics(t *testing.T) {
var tm time.Duration
defer func() {
if r := recover(); r == nil {
t.Error("panic expected")
}
}()
longpoll.MustNewTimeout(tm, nil)
}
func TestTimeout_onNoPing_expires(t *testing.T) {
timeout := 200 * time.Millisecond
tolerance := 50 * time.Millisecond
start := time.Now()
var end time.Time
tor := longpoll.MustNewTimeout(timeout, func() {
end = time.Now()
})
if !tor.IsAlive() {
t.Errorf("tor not alive on start")
}
time.Sleep(timeout + tolerance)
if tor.IsAlive() {
t.Errorf("tor alive after timeout")
}
if end.Sub(start) < timeout {
t.Errorf("timeout too early")
}
if end.Sub(start) > timeout+tolerance {
t.Errorf("timeout too late")
}
}
func TestTimeout_onPing_extends(t *testing.T) {
timeout := 200 * time.Millisecond
tolerance := 50 * time.Millisecond
start := time.Now()
var end time.Time
tor := longpoll.MustNewTimeout(timeout, func() {
end = time.Now()
})
time.Sleep(timeout - tolerance)
tor.Ping()
time.Sleep(tolerance + tolerance)
if !tor.IsAlive() {
t.Errorf("tor not after ping")
}
time.Sleep(timeout)
if tor.IsAlive() {
t.Errorf("tor alive after timeout")
}
if end.Sub(start) < timeout+timeout-tolerance {
t.Errorf("timeout too early")
}
if end.Sub(start) > timeout+timeout {
t.Errorf("timeout too late")
}
}
func TestTimeout_onExpiry_callsHandler_andReportsOnChannel(t *testing.T) {
timeout := 200 * time.Millisecond
tolerance := 50 * time.Millisecond
failed := true
tor := longpoll.MustNewTimeout(timeout, func() {
failed = false
})
time.Sleep(timeout + tolerance)
if failed {
t.Errorf("onTimeout handler not called")
}
select {
case <-tor.ReportChan(): // all good, ignore
default:
t.Errorf("timeout not reported on channel")
}
}
func TestTimeout_onNoHandler_reportsOnChannelOnExpiry(t *testing.T) {
timeout := 200 * time.Millisecond
tolerance := 50 * time.Millisecond
tor := longpoll.MustNewTimeout(timeout, nil)
if !tor.IsAlive() {
t.Errorf("tor not alive on start")
}
time.Sleep(timeout + tolerance)
if tor.IsAlive() {
t.Errorf("tor alive after timeout")
}
select {
case <-tor.ReportChan(): // all good, ignore
default:
t.Errorf("timeout not reported on channel")
}
}
func TestTimeout_onDrop_skipsHandler_butReportsOnChannel(t *testing.T) {
timeout := 200 * time.Millisecond
tolerance := 50 * time.Millisecond
failed := false
tor := longpoll.MustNewTimeout(timeout, func() {
failed = true
})
if !tor.IsAlive() {
t.Errorf("tor not alive on start")
}
time.Sleep(tolerance)
if !tor.IsAlive() {
t.Errorf("tor not alive on start")
}
tor.Drop()
time.Sleep(tolerance)
if tor.IsAlive() {
t.Errorf("tor alive after drop and wait")
}
if failed {
t.Errorf("handler called on drop")
}
select {
case <-tor.ReportChan(): // all good, ignore
default:
t.Errorf("timeout or drop not reported on channel")
}
}