Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeutil/timetest: Keep the mutex of timetest.Clock private #64

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions timeutil/timetest/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Clock struct {
sync.RWMutex
mu sync.RWMutex

now time.Time

Expand All @@ -17,15 +17,15 @@ type Clock struct {
}

func (c *Clock) MoveTo(t time.Time) {
c.Lock()
c.mu.Lock()
c.moveTo(t)
c.Unlock()
c.mu.Unlock()
}

func (c *Clock) MoveBy(d time.Duration) {
c.Lock()
c.mu.Lock()
c.moveTo(c.now.Add(d))
c.Unlock()
c.mu.Unlock()
}

func (c *Clock) moveTo(n time.Time) {
Expand All @@ -41,7 +41,7 @@ func (c *Clock) moveTo(n time.Time) {
}

func (c *Clock) newTimer(d time.Duration, fn func()) *timer {
c.Lock()
c.mu.Lock()
t := timer{
c: make(chan time.Time, 1),
now: c.now,
Expand All @@ -50,7 +50,7 @@ func (c *Clock) newTimer(d time.Duration, fn func()) *timer {
}

c.timers = append(c.timers, &t)
c.Unlock()
c.mu.Unlock()

return &t
}
Expand All @@ -64,23 +64,23 @@ func (c *Clock) TimerFunc(d time.Duration, fn func()) timeutil.Timer {
}

func (c *Clock) Ticker(d time.Duration) timeutil.Ticker {
c.Lock()
c.mu.Lock()
t := ticker{
c: make(chan time.Time, 1),
d: d,
t: c.now.Add(d),
}

c.tickers = append(c.tickers, &t)
c.Unlock()
c.mu.Unlock()

return &t
}

func (c *Clock) Now() time.Time {
c.RLock()
c.mu.RLock()
t := c.now
c.RUnlock()
c.mu.RUnlock()

return t
}
Expand Down
Loading