Atlas › Test

TestTimerFireAfterUpdate_NotActive_Updated

Exact test identity: go.temporal.io/server/common/timer/TestLocalTimerGateSuite/TestTimerFireAfterUpdate_NotActive_Updated

Package
go.temporal.io/server/common/timer
Suite / test hierarchy
TestLocalTimerGateSuite/TestTimerFireAfterUpdate_NotActive_Updated
Test
TestTimerFireAfterUpdate_NotActive_Updated
Introduced at
TestTimerFire, TestTimerFireAfterUpdate_Active_Updated, +3 Frontier kind: Test frontier
Covered ranges
12
Covered lines
44
Covered files
2

Co-introduced tests

4 other tests enter at the same concept.

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/timer/local_gate.go 38 covered LOC · 10 ranges

Open complete file

27
28 // NewLocalGate create a new timer gate instance
29 > func NewLocalGate(timeSource clock.TimeSource) LocalGate { local_gate.go
30 > lg := &LocalGateImpl{
31 > timer: time.NewTimer(0),
32 > nextWakeupTime: time.Time{},
33 > fireCh: make(chan struct{}, 1),
34 > closeCh: make(chan struct{}),
35 > timeSource: timeSource,
36 > }
37 > // the timer should be stopped when initialized
38 > if !lg.timer.Stop() {
39 // drain the existing signal if exist
40 <-lg.timer.C
41 }
42
43 > go func() { local_gate.go
44 > defer close(lg.fireCh)
45 > defer lg.timer.Stop()
46 > loop:
47 > for {
48 > select {
49 > case <-lg.timer.C: local_gate.go
50 > select {
51 // re-transmit on gateC
52 > case lg.fireCh <- struct{}{}: local_gate.go
53 default:
54 }
55
56 > case <-lg.closeCh: local_gate.go
57 > // closed; cleanup and quit
58 > break loop
59 }
60 }
61 }()
62
63 > return lg local_gate.go
64 }
65
66 // FireCh return the channel which will be fired when time is up
67 > func (lg *LocalGateImpl) FireCh() <-chan struct{} { local_gate.go
68 > return lg.fireCh
69 > }
70
71 // FireAfter check will the timer get fired after a certain time
76 // Update the timer gate, return true if update is a success.
77 // Success means timer is idle or timer is set with a sooner time to fire
78 > func (lg *LocalGateImpl) Update(nextTime time.Time) bool { local_gate.go
79 > // NOTE: negative duration will make the timer fire immediately
80 > now := lg.timeSource.Now()
81 >
82 > if lg.timer.Stop() && lg.nextWakeupTime.Before(nextTime) {
83 // this means the timer, before stopped, is active && next wake-up time do not have to be updated
84 lg.timer.Reset(lg.nextWakeupTime.Sub(now))
88 // this means the timer, before stopped, is active && next wake-up time has to be updated
89 // or this means the timer, before stopped, is already fired / never active
90 > lg.nextWakeupTime = nextTime local_gate.go
91 > lg.timer.Reset(nextTime.Sub(now))
92 > // Notifies caller that next notification is reset to fire at passed in 'next' visibility time
93 > return true
94 }
95
96 // Close shutdown the timer
97 > func (lg *LocalGateImpl) Close() { local_gate.go
98 > close(lg.closeCh)
99 > }
go.temporal.io/server/common/clock/time_source.go 6 covered LOC · 2 ranges

Open complete file

31
32 // NewRealTimeSource returns a timeSource that uses the real wall timeSource time.
33 > func NewRealTimeSource() RealTimeSource { time_source.go
34 > return RealTimeSource{}
35 > }
36
37 // Now returns the current time, with the location set to UTC.
38 > func (ts RealTimeSource) Now() time.Time { time_source.go
39 > return time.Now().UTC()
40 > }
41
42 // Since returns the time elapsed since t