-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2024 Fantom Foundation | ||
// | ||
// Use of this software is governed by the Business Source License included | ||
// in the LICENSE file and at fantom.foundation/bsl11. | ||
// | ||
// Change Date: 2028-4-16 | ||
// | ||
// On the date above, in accordance with the Business Source License, use of | ||
// this software will be governed by the GNU Lesser General Public License v3. | ||
|
||
package ticker | ||
|
||
import "time" | ||
|
||
//go:generate mockgen -source ticker.go -destination ticker_mocks.go -package ticker | ||
|
||
// Ticker is an abstraction of a ticker | ||
type Ticker interface { | ||
|
||
// C returns the channel on which the ticks are delivered. | ||
C() <-chan time.Time | ||
|
||
// Stop turns off a ticker. After Stop, no more ticks will be sent. | ||
Stop() | ||
} | ||
|
||
// TimeTicker is a wrapper around time.Ticker | ||
type TimeTicker struct { | ||
ticker *time.Ticker | ||
} | ||
|
||
// NewTimeTicker creates a new TimeTicker | ||
func NewTimeTicker(d time.Duration) TimeTicker { | ||
return TimeTicker{time.NewTicker(d)} | ||
} | ||
|
||
func (t TimeTicker) C() <-chan time.Time { | ||
return t.ticker.C | ||
} | ||
|
||
func (t TimeTicker) Stop() { | ||
t.ticker.Stop() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2024 Fantom Foundation | ||
// | ||
// Use of this software is governed by the Business Source License included | ||
// in the LICENSE file and at fantom.foundation/bsl11. | ||
// | ||
// Change Date: 2028-4-16 | ||
// | ||
// On the date above, in accordance with the Business Source License, use of | ||
// this software will be governed by the GNU Lesser General Public License v3. | ||
|
||
package ticker | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestTicker_Ticks(t *testing.T) { | ||
last := time.Now() | ||
|
||
ticker := NewTimeTicker(10 * time.Millisecond) | ||
|
||
const loops = 100 | ||
for i := 0; i < loops; i++ { | ||
tick := <-ticker.C() | ||
if tick.Before(last) { | ||
t.Errorf("tick %d is before last tick: %v < %v", i, last, tick) | ||
} | ||
last = tick | ||
} | ||
ticker.Stop() | ||
|
||
// no more ticks should be received | ||
select { | ||
case tick := <-ticker.C(): | ||
t.Errorf("unexpected tick: %v", tick) | ||
case <-time.After(1000 * time.Millisecond): | ||
// done | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters