- Notifications
You must be signed in to change notification settings - Fork907
fix: add timeouts to test telemetry snapshot#17879
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -55,3 +55,61 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) { | ||
// OK! | ||
} | ||
} | ||
// SoftTryReceive will attempt to receive a value from the chan and return it. If | ||
// the context expires before a value can be received, it will mark the test as | ||
// failed but continue execution. If the channel is closed, the zero value of the | ||
// channel type will be returned. | ||
// The second return value indicates whether the receive was successful. In | ||
// particular, if the channel is closed, the second return value will be true. | ||
// | ||
// Safety: can be called from any goroutine. | ||
func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bool) { | ||
t.Helper() | ||
select { | ||
case <-ctx.Done(): | ||
t.Error("timeout") | ||
var a A | ||
return a, false | ||
case a := <-c: | ||
return a, true | ||
} | ||
} | ||
// AssertReceive will receive a value from the chan and return it. If the | ||
// context expires or the channel is closed before a value can be received, | ||
// it will mark the test as failed but continue execution. | ||
// The second return value indicates whether the receive was successful. | ||
// | ||
// Safety: can be called from any goroutine. | ||
func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nit: could we abstract the logic and either use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There's also the extra return value. I suppose we could create internal helper functions which would be called with a bool param by the Require/Assert variants. Not sure if it's worth it here - what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nah, | ||
t.Helper() | ||
select { | ||
case <-ctx.Done(): | ||
t.Error("timeout") | ||
var a A | ||
return a, false | ||
case a, ok := <-c: | ||
if !ok { | ||
t.Error("channel closed") | ||
} | ||
return a, ok | ||
} | ||
} | ||
// AssertSend will send the given value over the chan and then return. If | ||
// the context expires before the send succeeds, it will mark the test as failed | ||
// but continue execution. | ||
// The second return value indicates whether the send was successful. | ||
// | ||
// Safety: can be called from any goroutine. | ||
func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool { | ||
t.Helper() | ||
select { | ||
case <-ctx.Done(): | ||
t.Error("timeout") | ||
return false | ||
case c <- a: | ||
return true | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.