Skip to content

Commit

Permalink
timer: move Timer inteface to dbft package
Browse files Browse the repository at this point in the history
Store Timer interface along with other dBFT interfaces and provide
default timer implementation in `timer` package.

A part of #84.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Mar 6, 2024
1 parent 2e3645e commit d26fc83
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Config[H Hash] struct {
// Logger
Logger *zap.Logger
// Timer
Timer timer.Timer
Timer Timer
// SecondsPerBlock is the number of seconds that
// need to pass before another block will be accepted.
SecondsPerBlock time.Duration
Expand Down Expand Up @@ -176,7 +176,7 @@ func WithLogger[H Hash](log *zap.Logger) func(config *Config[H]) {
}

// WithTimer sets Timer.
func WithTimer[H Hash](t timer.Timer) func(config *Config[H]) {
func WithTimer[H Hash](t Timer) func(config *Config[H]) {
return func(cfg *Config[H]) {
cfg.Timer = t
}
Expand Down
26 changes: 26 additions & 0 deletions timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dbft

import (
"time"

timer2 "github.com/nspcc-dev/dbft/timer"
)

// Timer is an interface which implements all time-related
// functions. It can be mocked for testing.
type Timer interface {
// Now returns current time.
Now() time.Time
// Reset
Reset(s timer2.HV, d time.Duration)
// Sleep stops execution for duration d.
Sleep(d time.Duration)
// Extend extends current timer with duration d.
Extend(d time.Duration)
// Stop stops timer.
Stop()
// HV returns current height and view set for the timer.
HV() timer2.HV
// C returns channel for timer events.
C() <-chan time.Time
}
46 changes: 15 additions & 31 deletions timer/timer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
/*
Package timer contains default implementation of [dbft.Timer] interface and provides
all necessary timer-related functionality to [dbft.DBFT] service.
*/
package timer

import (
"time"
)

type (
// Timer is an interface which implements all time-related
// functions. It can be mocked for testing.
Timer interface {
// Now returns current time.
Now() time.Time
// Reset
Reset(s HV, d time.Duration)
// Sleep stops execution for duration d.
Sleep(d time.Duration)
// Extend extends current timer with duration d.
Extend(d time.Duration)
// Stop stops timer.
Stop()
// HV returns current height and view set for the timer.
HV() HV
// C returns channel for timer events.
C() <-chan time.Time
}

value struct {
HV
s time.Time
Expand All @@ -36,26 +21,25 @@ type (
View byte
}

timer struct {
// Timer is a default [dbft.Timer] implementation.
Timer struct {
val value
tt *time.Timer
ch chan time.Time
}
)

var _ Timer = (*timer)(nil)

// New returns default Timer implementation.
func New() Timer {
t := &timer{
func New() *Timer {
t := &Timer{
ch: make(chan time.Time, 1),
}

return t
}

// C implements Timer interface.
func (t *timer) C() <-chan time.Time {
func (t *Timer) C() <-chan time.Time {
if t.tt == nil {
return t.ch
}
Expand All @@ -64,12 +48,12 @@ func (t *timer) C() <-chan time.Time {
}

// HV implements Timer interface.
func (t *timer) HV() HV {
func (t *Timer) HV() HV {
return t.val.HV
}

// Reset implements Timer interface.
func (t *timer) Reset(hv HV, d time.Duration) {
func (t *Timer) Reset(hv HV, d time.Duration) {
t.Stop()

t.val.s = t.Now()
Expand All @@ -93,20 +77,20 @@ func drain(ch <-chan time.Time) {
}

// Stop implements Timer interface.
func (t *timer) Stop() {
func (t *Timer) Stop() {
if t.tt != nil {
t.tt.Stop()
t.tt = nil
}
}

// Sleep implements Timer interface.
func (t *timer) Sleep(d time.Duration) {
func (t *Timer) Sleep(d time.Duration) {
time.Sleep(d)
}

// Extend implements Timer interface.
func (t *timer) Extend(d time.Duration) {
func (t *Timer) Extend(d time.Duration) {
t.val.d += d

if elapsed := time.Since(t.val.s); t.val.d > elapsed {
Expand All @@ -116,6 +100,6 @@ func (t *timer) Extend(d time.Duration) {
}

// Now implements Timer interface.
func (t *timer) Now() time.Time {
func (t *Timer) Now() time.Time {
return time.Now()
}
5 changes: 3 additions & 2 deletions timer/timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/nspcc-dev/dbft"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -40,7 +41,7 @@ func TestTimer_Reset(t *testing.T) {
shouldNotReceive(t, tt, "timer was not stopped")
}

func shouldReceive(t *testing.T, tt Timer, hv HV, msg string) {
func shouldReceive(t *testing.T, tt dbft.Timer, hv HV, msg string) {
select {
case <-tt.C():
got := tt.HV()
Expand All @@ -50,7 +51,7 @@ func shouldReceive(t *testing.T, tt Timer, hv HV, msg string) {
}
}

func shouldNotReceive(t *testing.T, tt Timer, msg string) {
func shouldNotReceive(t *testing.T, tt dbft.Timer, msg string) {
select {
case <-tt.C():
require.Fail(t, msg)
Expand Down

0 comments on commit d26fc83

Please sign in to comment.