cockroachdb / cockroachdb/cockroach

testing: lint against `require.Eventually` (or `assert.Eventually`)

Open
#100,796 7 comments 1 reaction 0 assignees View on GitHub
A-testeng-foundations A-testing C-bug quality-friday T-testeng
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

There are two design problems in the `require.Eventually` / `assert.Eventually` code.

1. the function calls its condition in a separate goroutine. So if the condition function tries to call `t.Fail` (via `t.Error` etc) this violates the API of `*testing.T`.
2. it does not wait for the goroutine to complete before it returns. So if the condition doesn't return before the `Eventually` function gives up (e.g. because it gets stuck, or because it takes a longer time) and the `Eventually` logic fails, there will be a dangling goroutine.
3. Then also if the condition function eventually runs `t.Fail` _after the Eventually call completes and also the surrounding test_, the t.Fail call will occur after the test has terminated which is also an API violation.

**To Reproduce**

Example code that demonstrates the problems:

Problem 1:
```go
require.Eventually(t, func() bool {
require.True(t, false) // invalid use of t.Fail in separate goroutine
}, 3*time.Second, time.Second)
```

Problem 2:

```go
require.Eventually(t, func() bool {
time.Sleep(5 *time.Second) // will cause goroutine to dangle and potentially trigger leaktest
}, 3*time.Second, time.Second)
```

Problem 3:

```go
require.Eventually(t, func() bool {
time.Sleep(5 *time.Second)
require.True(t, false) // fail after the test terminates
}, 3*time.Second, time.Second)
```

**Expected behavior**

We should disallow any use of `require.Eventually` outright until the bug is fixed in the require/assert library.

`testutils.SucceedsSoon` doesn't have this problem.

Jira issue: CRDB-26629

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.