Atlas › Test
TestContextWithTimeout_Canceled
Exact test identity: go.temporal.io/server/common/clock/TestContextWithTimeout_Canceled
- Package
go.temporal.io/server/common/clock
- Suite / test hierarchy
TestContextWithTimeout_Canceled
- Test
TestContextWithTimeout_Canceled
- Introduced at
- TestContextWithTimeout_Canceled Frontier kind: Test frontier
- Covered ranges
- 18
- Covered lines
- 72
- Covered files
- 2
Covered source
Expand a file to inspect source; the > gutter marks covered lines.
go.temporal.io/server/common/clock/event_time_source.go 40 covered LOC · 11 ranges
Open complete file
39
40
// NewEventTimeSource returns a EventTimeSource with the current time set to Unix zero: 1970-01-01 00:00:00 +0000 UTC.
42
>
return &EventTimeSource{
43
>
now: time.Unix(0, 0),
44
>
}
45
>
}
46
47
// Some clients depend on the fact that the runtime's timers do _not_ run synchronously.
55
56
// Now return the current time.
58
>
ts.mu.RLock()
59
>
defer ts.mu.RUnlock()
60
>
61
>
return ts.now
62
>
}
63
64
func (ts *EventTimeSource) Since(t time.Time) time.Duration {
71
// wrap all such calls in a goroutine. If the duration is non-positive, the callback will fire immediately before
72
// AfterFunc returns.
74
>
if d < 0 {
75
d = 0
76
}
77
>
timer := &fakeTimer{timeSource: ts, deadline: ts.Now().Add(d), callback: f}
event_time_source.go
78
>
ts.addTimer(timer)
79
>
return timer
80
}
81
96
}
97
99
>
ts.mu.Lock()
100
>
defer ts.mu.Unlock()
101
>
t.index = len(ts.timers)
102
>
ts.timers = append(ts.timers, t)
103
>
ts.fireTimers()
104
>
}
105
106
// Update the fake current time. It returns the timeSource so that you can chain calls like this:
107
// timeSource := NewEventTimeSource().Update(time.Now())
109
>
ts.mu.Lock()
110
>
defer ts.mu.Unlock()
111
>
112
>
ts.now = now
113
>
ts.fireTimers()
114
>
return ts
115
>
}
116
117
// Advance the timer by the specified duration.
156
157
// fireTimers fires all timers that are ready.
159
>
n := 0
160
>
for _, t := range ts.timers {
163
>
t.index = n
164
>
n++
166
if ts.async {
167
go t.callback()
go.temporal.io/server/common/clock/context.go 32 covered LOC · 7 ranges
Open complete file
16
}
17
18
>
func (ctx *ctxWithDeadline) Deadline() (deadline time.Time, ok bool) {
context.go
19
>
return ctx.deadline, true
20
>
}
21
22
>
func (ctx *ctxWithDeadline) Done() <-chan struct{} {
context.go
23
>
return ctx.done
24
>
}
25
26
>
func (ctx *ctxWithDeadline) Err() error {
context.go
27
>
select {
28
>
case <-ctx.done:
29
>
return ctx.err
30
default:
31
return nil
40
}
41
42
>
func (ctx *ctxWithDeadline) cancel() {
context.go
43
>
ctx.once.Do(func() {
44
>
// We'd like to call ctx.timer.Stop() here, but we can't: the time source may call
context.go
45
>
// deadlineExceeded while holding its lock, which acquires the once mutex. Here we have
46
>
// the once mutex and want to cancel a timer, which would create a potential lock
47
>
// cycle. So just leave the timer as a no-op.
48
>
ctx.err = context.Canceled
49
>
close(ctx.done)
50
>
})
51
}
52
55
deadline time.Time,
56
timeSource TimeSource,
57
>
) (context.Context, context.CancelFunc) {
context.go
58
>
ctxd := &ctxWithDeadline{
59
>
Context: ctx,
60
>
deadline: deadline,
61
>
done: make(chan struct{}),
62
>
}
63
>
timer := timeSource.AfterFunc(deadline.Sub(timeSource.Now()), ctxd.deadlineExceeded)
64
>
ctxd.timer = timer
65
>
return ctxd, ctxd.cancel
66
>
}
67
68
func ContextWithTimeout(
70
timeout time.Duration,
71
timeSource TimeSource,
72
>
) (context.Context, context.CancelFunc) {
context.go
73
>
return ContextWithDeadline(ctx, timeSource.Now().Add(timeout), timeSource)
74
>
}