-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.go
More file actions
177 lines (162 loc) · 3.85 KB
/
loop.go
File metadata and controls
177 lines (162 loc) · 3.85 KB
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
170
171
172
173
174
175
176
177
package gotime
import (
"time"
"github.com/nitroshare/golist"
)
type afterChanData struct {
expiry time.Time
ch chan time.Time
timer *Timer
}
type resetTimerParams struct {
timer *Timer
duration time.Duration
}
type mockLoop struct {
mockTime time.Time
waitingForAfter bool
afterChan golist.List[*afterChanData]
chanNow chan any
chanAfter chan time.Duration
chanSet chan time.Time
chanAdvance chan time.Duration
chanAdvanceToAfter chan any
chanNewTimer chan *Timer
chanStopTimer chan *Timer
chanResetTimer chan *resetTimerParams
chanAny chan any
chanBool chan bool
chanTime chan time.Time
chanTimeChan chan (<-chan time.Time)
chanTest chan any
chanClose chan any
}
func (m *mockLoop) send(expiry time.Time, ch chan<- time.Time) {
go func() {
ch <- expiry
}()
}
func (m *mockLoop) after(d time.Duration, t *Timer) <-chan time.Time {
var (
expiry = m.mockTime.Add(d)
ch = make(chan time.Time)
)
if m.waitingForAfter {
m.mockTime = expiry
m.send(expiry, ch)
m.waitingForAfter = false
m.chanAny <- nil
} else {
m.afterChan.Add(&afterChanData{
expiry: expiry,
ch: ch,
timer: t,
})
}
return ch
}
func (m *mockLoop) sendElapsed() {
for e := m.afterChan.Front; e != nil; e = e.Next {
d := e.Value
if !d.expiry.After(m.mockTime) {
m.send(d.expiry, d.ch)
if d.timer != nil && d.timer.ticker {
d.expiry = d.expiry.Add(d.timer.duration)
} else {
m.afterChan.Remove(e)
}
}
}
}
func (m *mockLoop) findEarliest() time.Time {
var earliestTime time.Time
for e := m.afterChan.Front; e != nil; e = e.Next {
if earliestTime.IsZero() || e.Value.expiry.Before(earliestTime) {
earliestTime = e.Value.expiry
}
}
return earliestTime
}
func (m *mockLoop) stopTimer(t *Timer) bool {
for e := m.afterChan.Front; e != nil; e = e.Next {
if t == e.Value.timer {
m.afterChan.Remove(e)
return true
}
}
return false
}
func (m *mockLoop) resetTimer(t *Timer, d time.Duration) {
for e := m.afterChan.Front; e != nil; e = e.Next {
if t == e.Value.timer {
e.Value.expiry = m.mockTime.Add(d)
t.duration = d
break
}
}
}
func (m *mockLoop) run() {
defer close(m.chanClose)
for {
select {
case <-m.chanNow:
m.chanTime <- m.mockTime
case d := <-m.chanAfter:
m.chanTimeChan <- m.after(d, nil)
case t := <-m.chanSet:
m.mockTime = t
m.sendElapsed()
m.chanAny <- nil
case d := <-m.chanAdvance:
m.mockTime = m.mockTime.Add(d)
m.sendElapsed()
m.chanAny <- nil
case <-m.chanAdvanceToAfter:
earliestTime := m.findEarliest()
if earliestTime.IsZero() {
if m.chanTest != nil {
m.chanTest <- nil
}
m.waitingForAfter = true
continue
}
m.mockTime = earliestTime
m.sendElapsed()
m.chanAny <- nil
case t := <-m.chanNewTimer:
m.chanTimeChan <- m.after(t.duration, t)
case t := <-m.chanStopTimer:
m.chanBool <- m.stopTimer(t)
case d := <-m.chanResetTimer:
m.resetTimer(d.timer, d.duration)
m.chanAny <- nil
case <-m.chanClose:
return
}
}
}
func newMockLoop() *mockLoop {
m := &mockLoop{
afterChan: golist.List[*afterChanData]{},
chanNow: make(chan any),
chanAfter: make(chan time.Duration),
chanSet: make(chan time.Time),
chanAdvance: make(chan time.Duration),
chanAdvanceToAfter: make(chan any),
chanNewTimer: make(chan *Timer),
chanStopTimer: make(chan *Timer),
chanResetTimer: make(chan *resetTimerParams),
chanAny: make(chan any),
chanBool: make(chan bool),
chanTime: make(chan time.Time),
chanTimeChan: make(chan (<-chan time.Time)),
chanClose: make(chan any),
}
go m.run()
return m
}
func (m *mockLoop) close() {
m.chanClose <- nil
<-m.chanClose
}
var loop *mockLoop