• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nitroshare / mocktime / 16574775734

28 Jul 2025 04:35PM UTC coverage: 96.491% (-3.5%) from 100.0%
16574775734

push

github

nathan-osman
Bump golist version to v1.0.2.

55 of 57 relevant lines covered (96.49%)

1.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.49
/mocktime.go
1
package mocktime
2

3
import (
4
        "sync"
5
        "time"
6

7
        "github.com/nitroshare/golist"
8
)
9

10
type afterChanData struct {
11
        expiry time.Time
12
        ch     chan time.Time
13
}
14

15
var (
16
        mutex     sync.RWMutex
17
        mockTime  time.Time
18
        afterChan = golist.List[*afterChanData]{}
19
        firedFn   func(<-chan time.Time)
20
)
21

22
// MockNow returns the current mocked time. Although this can be set by
23
// reassigning Now, this is typically handled automatically by Mock.
24
func MockNow() time.Time {
1✔
25
        mutex.RLock()
1✔
26
        defer mutex.RUnlock()
1✔
27
        return mockTime
1✔
28
}
1✔
29

30
func MockAfter(d time.Duration) <-chan time.Time {
1✔
31
        mutex.Lock()
1✔
32
        mutex.Unlock()
1✔
33
        ch := make(chan time.Time)
1✔
34
        afterChan.Add(&afterChanData{
1✔
35
                expiry: mockTime.Add(d),
1✔
36
                ch:     ch,
1✔
37
        })
1✔
38
        return ch
1✔
39
}
1✔
40

41
func setAdvance(t *time.Time, d *time.Duration) {
1✔
42
        mutex.Lock()
1✔
43
        mutex.Unlock()
1✔
44
        if t != nil {
2✔
45
                mockTime = *t
1✔
46
        }
1✔
47
        if d != nil {
2✔
48
                mockTime = mockTime.Add(*d)
1✔
49
        }
1✔
50
        for e := afterChan.Front; e != nil; e = e.Next {
2✔
51
                if !e.Value.expiry.After(mockTime) {
2✔
52
                        if firedFn != nil {
2✔
53
                                firedFn(e.Value.ch)
1✔
54
                        }
1✔
55
                        capturedV := e.Value
1✔
56
                        go func() {
1✔
57
                                capturedV.ch <- capturedV.expiry
×
58
                        }()
×
59
                        afterChan.Remove(e)
1✔
60
                }
61
        }
62
}
63

64
// Set explicitly sets the mocked time.
65
func Set(t time.Time) {
1✔
66
        setAdvance(&t, nil)
1✔
67
}
1✔
68

69
// Advance advances the mocked time by the specified duration.
70
func Advance(d time.Duration) {
1✔
71
        setAdvance(nil, &d)
1✔
72
}
1✔
73

74
var (
75

76
        // Now normally points to time.Now but can also be pointed to with MockNow.
77
        Now func() time.Time
78

79
        // After normally points to time.After but can also be pointed to with MockAfter.
80
        After func(d time.Duration) <-chan time.Time
81
)
82

83
func set() {
1✔
84
        Now = MockNow
1✔
85
        After = MockAfter
1✔
86
}
1✔
87

88
func reset() {
1✔
89
        Now = time.Now
1✔
90
        After = time.After
1✔
91
}
1✔
92

93
func init() {
1✔
94
        reset()
1✔
95
}
1✔
96

97
// Mock replaces the time functions in this package with their mocked equivalents.
98
func Mock() {
1✔
99
        set()
1✔
100
}
1✔
101

102
// Unmock replaces the mocked time functions with their original equivalents.
103
func Unmock() {
1✔
104
        reset()
1✔
105
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc