forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjittered_ticker_test.go
111 lines (104 loc) · 2.96 KB
/
jittered_ticker_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
// Copyright (c) 2016-2017, 2020 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jitter
import (
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)
var _ = Describe("Real 20ms + 10ms Ticker", func() {
var ticker *Ticker
var startTime time.Time
BeforeEach(func() {
startTime = time.Now()
ticker = NewTicker(20*time.Millisecond,
10*time.Millisecond)
})
AfterEach(func() {
ticker.Stop()
})
It("should never tick before minDelay", func() {
<-ticker.Channel()
now := time.Now()
duration := now.Sub(startTime)
Expect(duration).To(BeNumerically(">=", 20*time.Millisecond))
}, 1)
It("should produce longer and shorter ticks", func() {
lastTime := startTime
foundLT5 := false
foundGT5 := false
for i := 0; i < 40; i++ {
<-ticker.Channel()
now := time.Now()
duration := time.Since(lastTime)
logrus.WithField("duration", duration).Debug("Tick")
if duration < 25*time.Millisecond {
foundLT5 = true
} else {
foundGT5 = true
}
if foundLT5 && foundGT5 {
break
}
lastTime = now
}
Expect(foundLT5).To(BeTrue())
Expect(foundGT5).To(BeTrue())
}, 1)
})
var _ = Describe("Delay calculation", func() {
var ticker *Ticker
BeforeEach(func() {
ticker = NewTicker(20*time.Millisecond, 10*time.Millisecond)
ticker.Stop()
})
for i := 0; i < 10; i++ {
It(fmt.Sprintf("should tick before max delay, trial: %v", i), func() {
duration := ticker.calculateDelay()
Expect(duration).To(BeNumerically("<=", 30*time.Millisecond))
})
It(fmt.Sprintf("should never tick before min delay, trial: %v", i), func() {
duration := ticker.calculateDelay()
Expect(duration).To(BeNumerically(">=", 20*time.Millisecond))
})
}
It("should produce longer and shorter ticks", func() {
foundLT5 := false
foundGT5 := false
for i := 0; i < 40; i++ {
duration := ticker.calculateDelay()
logrus.WithField("duration", duration).Debug("Tick")
if duration < 25*time.Millisecond {
foundLT5 = true
} else {
foundGT5 = true
}
if foundLT5 && foundGT5 {
break
}
}
Expect(foundLT5).To(BeTrue())
Expect(foundGT5).To(BeTrue())
}, 1)
})
var _ = Describe("Ticker constructor", func() {
It("should panic on negative duration", func() {
Expect(func() { NewTicker(-1*time.Second, 0) }).To(Panic())
})
It("should panic on negative jitter", func() {
Expect(func() { NewTicker(1*time.Second, -1*time.Second) }).To(Panic())
})
})