Skip to content

Commit

Permalink
External clock updated
Browse files Browse the repository at this point in the history
Does not set timestamp on update as its not needed.
  • Loading branch information
ohlsont committed Nov 17, 2020
1 parent f3a4bbb commit 4821140
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/einride/clock-go

go 1.14
go 1.15

require (
github.com/golang/mock v1.4.3
Expand Down
5 changes: 2 additions & 3 deletions pkg/external/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ func NewClock(logger *zap.Logger) *Clock {
return c
}

func (g *Clock) SetTimestamp(t *timestamppb.Timestamp) error {
g.timestampChan <- t.AsTime()
return nil
func (g *Clock) SetTimestamp(t time.Time) {
g.timestampChan <- t
}

func (g *Clock) Run(ctx context.Context) error {
Expand Down
43 changes: 11 additions & 32 deletions pkg/external/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/timestamppb"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand Down Expand Up @@ -36,9 +35,7 @@ func TestExternalClock_Stop(t *testing.T) {

// should not be able to tick
loopTicks := loopTicker.C()
nanoTime := timestamppb.New(time.Unix(0, tickTime.Nanoseconds()+1))
err := externalClock.SetTimestamp(nanoTime)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, tickTime.Nanoseconds()+1))
didSet := false
select {
case <-time.After(1 * time.Millisecond):
Expand Down Expand Up @@ -70,9 +67,7 @@ func TestExternalClock_Tick(t *testing.T) {
loopTicks := loopTicker.C()

// Send a tick
tickTimeProto := timestamppb.New(time.Unix(0, tickTime.Nanoseconds()))
err := externalClock.SetTimestamp(tickTimeProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, tickTime.Nanoseconds()))

// exect didSet to be true
didSet := false
Expand All @@ -93,9 +88,7 @@ func TestExternalClock_After(t *testing.T) {
afterChan := externalClock.After(tickTime)

// Send a tick
tickTimeProto := timestamppb.New(time.Unix(0, tickTime.Nanoseconds()+1))
err := externalClock.SetTimestamp(tickTimeProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, tickTime.Nanoseconds()+1))

// exect didSet to be true
didSet := false
Expand All @@ -116,9 +109,7 @@ func TestExternalClock_Removed(t *testing.T) {
afterChan := externalClock.After(tickTime)

// Send a tick
tickTimeProto := timestamppb.New(time.Unix(0, tickTime.Nanoseconds()))
err := externalClock.SetTimestamp(tickTimeProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, tickTime.Nanoseconds()))

// exect didSet to be true
select {
Expand All @@ -137,9 +128,7 @@ func TestExternalClock_AfterFailToShortTime(t *testing.T) {
afterChan := externalClock.After(tickTime)

// Send a tick
tickTimeProto := timestamppb.New(time.Unix(0, 0))
err := externalClock.SetTimestamp(tickTimeProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, 0))

// exect didSet to be true
select {
Expand All @@ -158,9 +147,7 @@ func TestExternalClock_NewTicker_Tick_Periodically(t *testing.T) {

// Send a tick
for i := range make([]int64, 1000) {
tickTimeProto := timestamppb.New(time.Unix(0, int64(i+1)*tickTime.Nanoseconds()+1))
err := externalClock.SetTimestamp(tickTimeProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, int64(i+1)*tickTime.Nanoseconds()+1))

// exect didSet to be true
didSet := false
Expand All @@ -187,9 +174,7 @@ func TestExternalClock_TestLooper(t *testing.T) {
if tick == target {
cancel()
} else {
tickProto := timestamppb.New(time.Unix(0, (tick+1)*time.Millisecond.Nanoseconds()))
err := externalClock.SetTimestamp(tickProto)
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, (tick+1)*time.Millisecond.Nanoseconds()))
}
},
}
Expand All @@ -198,11 +183,8 @@ func TestExternalClock_TestLooper(t *testing.T) {
return looper.Run(ctx)
})
<-looper.init
tickProto := timestamppb.New(time.Unix(0, 1*time.Millisecond.Nanoseconds()))
err := externalClock.SetTimestamp(tickProto)
assert.NilError(t, err)
err = g.Wait()
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, 1*time.Millisecond.Nanoseconds()))
assert.NilError(t, g.Wait())
assert.Equal(t, looper.Target, looper.counter)
}

Expand All @@ -222,11 +204,8 @@ func TestExternalClock_TestLooper_AddTicker(t *testing.T) {
return looper.Run(ctx)
})
<-looper.init
tickProto := timestamppb.New(time.Unix(0, 1*time.Millisecond.Nanoseconds()+1))
err := externalClock.SetTimestamp(tickProto)
assert.NilError(t, err)
err = g.Wait()
assert.NilError(t, err)
externalClock.SetTimestamp(time.Unix(0, 1*time.Millisecond.Nanoseconds()+1))
assert.NilError(t, g.Wait())
}

type testLooper struct {
Expand Down

0 comments on commit 4821140

Please sign in to comment.