-
Notifications
You must be signed in to change notification settings - Fork 309
/
time.go
118 lines (101 loc) · 2.49 KB
/
time.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 test
import (
"fmt"
"sync"
"time"
)
// MockClock is used to mock time package functionality.
type MockClock struct {
nowMu sync.RWMutex
now time.Time
nowChsMu sync.RWMutex
nowChs map[chan<- time.Time]struct{}
}
// NewMockClock returns a new MockClock.
func NewMockClock(t time.Time) *MockClock {
return &MockClock{
now: t,
nowChs: map[chan<- time.Time]struct{}{},
}
}
func (m *MockClock) notifyNow(t time.Time) {
m.nowChsMu.RLock()
defer m.nowChsMu.RUnlock()
for ch := range m.nowChs {
ch <- t
}
}
// Set sets clock to time t and returns old clock time.
func (m *MockClock) Set(t time.Time) time.Time {
m.nowMu.Lock()
defer m.nowMu.Unlock()
old := m.now
if old.After(t) {
panic(fmt.Sprintf("current time (`%s`) is after the one being set (`%s`)", old, t))
}
m.now = t
m.notifyNow(t)
return old
}
// Add adds d to clock and returns new clock time.
func (m *MockClock) Add(d time.Duration) time.Time {
m.nowMu.Lock()
defer m.nowMu.Unlock()
m.now = m.now.Add(d)
m.notifyNow(m.now)
return m.now
}
// Now returns current clock time.
func (m *MockClock) Now() time.Time {
m.nowMu.RLock()
defer m.nowMu.RUnlock()
return m.now
}
// After returns a channel, on which current time.Time will be sent once d passes.
func (m *MockClock) After(d time.Duration) <-chan time.Time {
m.nowMu.RLock()
defer m.nowMu.RUnlock()
if d <= 0 {
ch := make(chan time.Time, 1)
ch <- m.now
close(ch)
return ch
}
m.nowChsMu.Lock()
nowCh := make(chan time.Time)
m.nowChs[nowCh] = struct{}{}
m.nowChsMu.Unlock()
at := m.now.Add(d)
afterCh := make(chan time.Time, 1)
go func() {
for now := range nowCh {
if now.Before(at) {
continue
}
afterCh <- now
close(afterCh)
go func() {
m.nowChsMu.Lock()
defer m.nowChsMu.Unlock()
delete(m.nowChs, nowCh)
close(nowCh)
}()
for range nowCh {
}
}
}()
return afterCh
}