-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add a test for qps.go #8391
base: master
Are you sure you want to change the base?
Add a test for qps.go #8391
Conversation
func NewCounter(window time.Duration) *Counter { | ||
return new(window, nil) | ||
} | ||
|
||
func NewCounterForTesting(window time.Duration, ticker <-chan time.Time) *Counter { | ||
return new(window, ticker) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: probably more conventional to have only a single constructor and have it accept an instance of clockwork.Clock
rather than a ticker channel
} | ||
// The current bin is incremented asynchronously upon receiving a tick via | ||
// the ticker channel. Give it some time to run. | ||
time.Sleep(10 * time.Millisecond) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will likely be flaky. Could do one of these instead
- Have the QPS counter accept an optional
updates
channel (chan struct{}
) and send a non-blocking message on that channel whenever a tick is processed. Then the test can receive from the channel here instead of waiting for an update. - Do whitebox testing - move qps_test to the qps package, and manually call some
update(now time.Time)
function in the QPS counter rather than driving the counter using a ticker. - Do a poll here and wait indefinitely for the condition to become true - probably the simplest option but the downside though is that the test will just reach the test timeout if there's a bug rather than immediately failing - a little annoying but probably fine.
|
||
func TestQPS(t *testing.T) { | ||
ticker := make(chan time.Time, 1) | ||
counter := qps.NewCounterForTesting(5*time.Second, ticker) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably want to defer counter.Stop()
here
|
||
func TestRaciness(t *testing.T) { | ||
ticker := make(chan time.Time, 1) | ||
counter := qps.NewCounterForTesting(5*time.Second, ticker) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defer counter.Stop()
here too
I would like to use a similar time-binning ring buffer to track error rates for performing failover in a new gRPC client. This is the first step towards extracting the time-binning logic into a different place where it can be reused.