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

Closed
mmjconolly opened this issue Jul 28, 2017 · 3 comments
Closed
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@mmjconolly
Copy link

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
@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jul 30, 2017
@rsc
Copy link
Contributor

rsc commented Oct 30, 2017

/cc @Sajmani

@Sajmani
Copy link
Contributor

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.

@Sajmani
Copy link
Contributor

Sajmani commented Jun 25, 2020

Closing as passing "now" to the limiter functions is sufficient to support testing already.

@Sajmani Sajmani closed this as completed Jun 25, 2020
@golang golang locked and limited conversation to collaborators Jun 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants