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

Add a test for qps.go #8391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add a test for qps.go #8391

wants to merge 1 commit into from

Conversation

iain-macdonald
Copy link
Contributor

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.

Comment on lines 41 to +47
func NewCounter(window time.Duration) *Counter {
return new(window, nil)
}

func NewCounterForTesting(window time.Duration, ticker <-chan time.Time) *Counter {
return new(window, ticker)
}
Copy link
Member

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)
Copy link
Member

@bduffany bduffany Feb 12, 2025

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)
Copy link
Member

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)
Copy link
Member

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

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

Successfully merging this pull request may close these issues.

2 participants