Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/time/rate: allow injecting of time functions to make testing easier #21210

Open
mmjconolly opened this issue Jul 28, 2017 · 2 comments
Open

x/time/rate: allow injecting of time functions to make testing easier #21210

mmjconolly opened this issue Jul 28, 2017 · 2 comments
Labels
Milestone

Comments

@mmjconolly
Copy link

@mmjconolly mmjconolly commented Jul 28, 2017

What did you do?

Limiter from x/time/rate directly uses time functions without ability to provide replacement.

When testing code that uses Limiter, it is hard to write a test that doesn't involve the passing of actual wall clock time.
It would be convenient if we could inject a function to provide the time functions used by Limiter's methods, like time.Now()

There are also examples of clocks that would be convenient to inject, such as andres-erbsen/clock. Though just a replacement for time.Now() is likely sufficient.

What did you expect to see?

lim := rate.NewLimiter(1.0, 1, func() time.Time {
  return ... 
} )

What did you see instead?

lim := rate.NewLimiter(1.0, 1)

System details

go version go1.8.3 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mconolly/code"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bh/z5skz7kx0zjd8d88s4mhskkm0000gn/T/go-build993526661=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
GOROOT/bin/go version: go version go1.8.3 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.8.3 X:framepointer
uname -v: Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.12.5
BuildVersion:	16F73
lldb --version: lldb-370.0.42
  Swift-3.1
@gopherbot gopherbot added this to the Unreleased milestone Jul 28, 2017
@ALTree ALTree changed the title x/time/rate is awkward to test, directly uses time.Now x/time/rate: allow injecting of time functions to make testing easier Jul 30, 2017
@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Oct 30, 2017

/cc @Sajmani

@Sajmani

This comment has been minimized.

Copy link
Contributor

@Sajmani Sajmani commented Nov 16, 2017

Limiter.AllowN and ReserveN already allow you to pass "now" as a parameter. This is how we test the limiter with fake times already. I think you should be able to use those in your tests as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.