Skip to content

fix(assert): prevent panic on nil condition in Eventually and Never - #1980

Closed
AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:fix-eventually-never-nil-condition-panic
Closed

AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:fix-eventually-never-nil-condition-panic

Conversation

@AdamMagued

Copy link
Copy Markdown

Validate that condition != nil and tick > 0 at the start of Eventually, EventuallyWithT, and Never before creating timers or spawning worker goroutines.

Previously, passing a nil condition to Eventually, EventuallyWithT, or Never caused a panic inside a newly spawned goroutine when executing condition(). Because the panic occurred on a separate goroutine, it could not be recovered by the calling test function and caused the entire test binary to abort without reporting an assertion failure to TestingT. Similarly, passing a non-positive tick caused time.NewTicker to panic.

With this change:

  • If condition is nil, a clean assertion failure is reported to TestingT with "Condition must not be nil" and the function returns false.
  • If tick <= 0, an assertion failure is reported with "tick must be greater than 0" and the function returns false.
  • Added regression tests in assert/assertions_test.go covering nil conditions and non-positive tick intervals for Eventually, EventuallyWithT, and Never.

Fixes #1970

Validate that condition is non-nil and tick is greater than 0 in Eventually, EventuallyWithT, and Never. Previously, a nil condition panicked on a newly spawned goroutine, causing the entire test binary to crash unrecoverably without reporting assertion failure to TestingT. Passing a non-positive tick also panicked in time.NewTicker. Both cases now cleanly fail the assertion via Fail and return false. Fixes stretchr#1970
@AdamMagued

Copy link
Copy Markdown
Author

Closing in favor of issue author's PR #1971 to keep the review queue clean.

@AdamMagued AdamMagued closed this Oct 2, 2026
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.

Eventually/Never panic on a nil condition, aborting the test binary

1 participant