Skip to content

Commit

Permalink
Initial clock and mock
Browse files Browse the repository at this point in the history
Gomock generated code checked in, allows reuse of our existing pattern
across many repositories.
  • Loading branch information
odsod committed Jul 26, 2019
1 parent ed91c56 commit 011ea39
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.gobincache
54 changes: 54 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# all: run a complete build
all: \
markdown-lint \
mocks \
go-lint \
go-review \
go-mod-tidy \
git-verify-nodiff \
git-verify-submodules

export GO111MODULE := on

# clean: remove all generated build files
.PHONY: clean
clean:
rm -rf build

.PHONY: build
build:
@git submodule update --init --recursive $@

include build/rules.mk
build/rules.mk: build
@# included in submodule: build

# markdown-lint: lint Markdown files
.PHONY: markdown-lint
markdown-lint: $(PRETTIER)
$(PRETTIER) --check **/*.md --parser markdown

# go-mod-tidy: update Go module files
.PHONY: go-mod-tidy
go-mod-tidy:
go mod tidy -v

# go-lint: lint Go files
.PHONY: go-lint
go-lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT) run --enable-all

# go-review: review Go files
.PHONY: go-review
go-review: $(GOREVIEW)
$(GOREVIEW) -c 1 ./...

# mocks: generate Go mocks
.PHONY: mocks
mocks: pkg/mockclock/clock.go

pkg/mockclock/clock.go: pkg/clock/clock.go $(GOBIN)
$(GOBIN) -m -run github.com/golang/mock/mockgen \
-destination $@ \
-package mockclock \
github.com/einride/clock-go/pkg/clock Clock,Ticker
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/einride/clock-go

go 1.12

require (
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.3.2
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
32 changes: 32 additions & 0 deletions pkg/clock/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Package clock provides primitives for mocking time.
package clock

import (
"time"

"github.com/golang/protobuf/ptypes/timestamp"
)

// Clock provides capabilities from the time standard library package.
type Clock interface {
// After waits for the duration to elapse and then sends the current time on the returned channel.
After(duration time.Duration) <-chan time.Time

// NewTicker returns a new Ticker.
NewTicker(d time.Duration) Ticker

// Now returns the current local time.
Now() time.Time

// NowProto returns a new Protobuf timestamp representing the current local time.
NowProto() *timestamp.Timestamp
}

// Ticker wraps the time.Ticker class.
type Ticker interface {
// C returns the channel on which the ticks are delivered.
C() <-chan time.Time

// Stop the Ticker.
Stop()
}
41 changes: 41 additions & 0 deletions pkg/clock/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package clock

import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
)

// System returns a Clock implementation that delegate to the time package.
func System() Clock {
return &systemClock{}
}

type systemClock struct{}

var _ Clock = &systemClock{}

func (c systemClock) After(d time.Duration) <-chan time.Time {
return time.After(d)
}

func (c systemClock) NowProto() *timestamp.Timestamp {
return ptypes.TimestampNow()
}

func (c systemClock) NewTicker(d time.Duration) Ticker {
return &systemTicker{Ticker: *time.NewTicker(d)}
}

func (c systemClock) Now() time.Time {
return time.Now()
}

type systemTicker struct {
time.Ticker
}

func (t systemTicker) C() <-chan time.Time {
return t.Ticker.C
}
141 changes: 141 additions & 0 deletions pkg/mockclock/clock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build tools

package tools

import _ "github.com/golang/mock/mockgen"

0 comments on commit 011ea39

Please sign in to comment.