forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monotonic_test.go
154 lines (126 loc) · 3.09 KB
/
monotonic_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
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package timers
import (
"github.com/algorand/go-algorand/test/partitiontest"
"math/rand"
"testing"
"time"
)
func polled(ch <-chan time.Time) bool {
select {
case <-ch:
return true
default:
return false
}
}
func TestMonotonicDelta(t *testing.T) {
partitiontest.PartitionTest(t)
var m Monotonic
var c Clock
var ch <-chan time.Time
d := time.Millisecond * 100
c = m.Zero()
ch = c.TimeoutAt(d)
if polled(ch) {
t.Errorf("channel fired ~100ms early")
}
<-time.After(d * 2)
if !polled(ch) {
t.Errorf("channel failed to fire at 100ms")
}
ch = c.TimeoutAt(d / 2)
if !polled(ch) {
t.Errorf("channel failed to fire at 50ms")
}
}
func TestMonotonicZeroDelta(t *testing.T) {
partitiontest.PartitionTest(t)
var m Monotonic
var c Clock
var ch <-chan time.Time
c = m.Zero()
ch = c.TimeoutAt(0)
if !polled(ch) {
t.Errorf("read failed on channel at zero timeout")
}
}
func TestMonotonicNegativeDelta(t *testing.T) {
partitiontest.PartitionTest(t)
var m Monotonic
var c Clock
var ch <-chan time.Time
c = m.Zero()
ch = c.TimeoutAt(-time.Second)
if !polled(ch) {
t.Errorf("read failed on channel at negative timeout")
}
}
func TestMonotonicZeroTwice(t *testing.T) {
partitiontest.PartitionTest(t)
var m Monotonic
var c Clock
var ch <-chan time.Time
d := time.Millisecond * 100
c = m.Zero()
ch = c.TimeoutAt(d)
if polled(ch) {
t.Errorf("channel fired ~100ms early")
}
<-time.After(d * 2)
if !polled(ch) {
t.Errorf("channel failed to fire at 100ms")
}
c = c.Zero()
ch = c.TimeoutAt(d)
if polled(ch) {
t.Errorf("channel fired ~100ms early after call to Zero")
}
<-time.After(d * 2)
if !polled(ch) {
t.Errorf("channel failed to fire at 100ms after call to Zero")
}
}
func TestMonotonicEncodeDecode(t *testing.T) {
partitiontest.PartitionTest(t)
singleTest := func(c Clock, descr string) {
data := c.Encode()
c0, err := c.Decode(data)
if err != nil {
t.Errorf("decoding error: %v", err)
}
if !time.Time(c.(*Monotonic).zero).Equal(time.Time(c0.(*Monotonic).zero)) {
t.Errorf("%v clock not encoded properly: %v != %v", descr, c, c0)
}
}
var c Clock
var m Monotonic
c = Clock(&m)
singleTest(c, "empty")
c = c.Zero()
singleTest(c, "Zero()'ed")
now := time.Now()
for i := 0; i < 100; i++ {
r := time.Duration(rand.Int63())
c = Clock(
&Monotonic{
zero: now.Add(r),
},
)
singleTest(c, "random")
}
}