Skip to content

Commit

Permalink
deduplicate code in build tags that isn't using mutex in either
Browse files Browse the repository at this point in the history
  • Loading branch information
Takumi2008 committed Apr 25, 2022
1 parent 0ed8e0c commit 0c59bbe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 64 deletions.
32 changes: 0 additions & 32 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ func After(d time.Duration) <-chan time.Time {
return provider.After(d)
}

// Timer see time.Timer.
type Timer interface {
C() <-chan time.Time
Stop() bool
Reset(d time.Duration) bool
}

// NewTimer see time.NewTimer.
func NewTimer(d time.Duration) Timer {
return provider.NewTimer(d)
Expand All @@ -101,12 +94,6 @@ func AfterFunc(d time.Duration, f func()) Timer {
return provider.AfterFunc(d, f)
}

// Ticker see time.Ticker.
type Ticker interface {
C() <-chan time.Time
Stop()
}

// NewTicker see time.Ticker.
func NewTicker(d time.Duration) Ticker {
return provider.NewTicker(d)
Expand All @@ -116,22 +103,3 @@ func NewTicker(d time.Duration) Ticker {
func Tick(d time.Duration) <-chan time.Time {
return provider.Tick(d)
}

// NewStoppedTimer returns a stopped timer. Call Reset to get it ticking.
func NewStoppedTimer() Timer {
t := NewTimer(42 * time.Hour)
t.Stop()
return t
}

// Clock is an interface that mimics the one of the SDK time package.
type Clock interface {
Now() time.Time
Sleep(d time.Duration)
After(d time.Duration) <-chan time.Time
NewTimer(d time.Duration) Timer
AfterFunc(d time.Duration, f func()) Timer
NewTicker(d time.Duration) Ticker
Tick(d time.Duration) <-chan time.Time
Wait4Scheduled(n int, timeout time.Duration) bool
}
40 changes: 8 additions & 32 deletions clock/clock_mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,54 +102,30 @@ func After(d time.Duration) <-chan time.Time {
return provider.After(d)
}

// Timer see time.Timer.
type Timer interface {
C() <-chan time.Time
Stop() bool
Reset(d time.Duration) bool
}

// NewTimer see time.NewTimer.
func NewTimer(d time.Duration) Timer {
rwMutex.RLock()
defer rwMutex.RUnlock()
return provider.NewTimer(d)
}

// AfterFunc see time.AfterFunc.
func AfterFunc(d time.Duration, f func()) Timer {
rwMutex.RLock()
defer rwMutex.RUnlock()
return provider.AfterFunc(d, f)
}

// Ticker see time.Ticker.
type Ticker interface {
C() <-chan time.Time
Stop()
}

// NewTicker see time.Ticker.
func NewTicker(d time.Duration) Ticker {
rwMutex.RLock()
defer rwMutex.RUnlock()
return provider.NewTicker(d)
}

// Tick see time.Tick.
func Tick(d time.Duration) <-chan time.Time {
rwMutex.RLock()
defer rwMutex.RUnlock()
return provider.Tick(d)
}

// NewStoppedTimer returns a stopped timer. Call Reset to get it ticking.
func NewStoppedTimer() Timer {
t := NewTimer(42 * time.Hour)
t.Stop()
return t
}

// Clock is an interface that mimics the one of the SDK time package.
type Clock interface {
Now() time.Time
Sleep(d time.Duration)
After(d time.Duration) <-chan time.Time
NewTimer(d time.Duration) Timer
AfterFunc(d time.Duration, f func()) Timer
NewTicker(d time.Duration) Ticker
Tick(d time.Duration) <-chan time.Time
Wait4Scheduled(n int, timeout time.Duration) bool
}
35 changes: 35 additions & 0 deletions clock/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package clock

import "time"

// Timer see time.Timer.
type Timer interface {
C() <-chan time.Time
Stop() bool
Reset(d time.Duration) bool
}

// Ticker see time.Ticker.
type Ticker interface {
C() <-chan time.Time
Stop()
}

// NewStoppedTimer returns a stopped timer. Call Reset to get it ticking.
func NewStoppedTimer() Timer {
t := NewTimer(42 * time.Hour)
t.Stop()
return t
}

// Clock is an interface that mimics the one of the SDK time package.
type Clock interface {
Now() time.Time
Sleep(d time.Duration)
After(d time.Duration) <-chan time.Time
NewTimer(d time.Duration) Timer
AfterFunc(d time.Duration, f func()) Timer
NewTicker(d time.Duration) Ticker
Tick(d time.Duration) <-chan time.Time
Wait4Scheduled(n int, timeout time.Duration) bool
}

0 comments on commit 0c59bbe

Please sign in to comment.