Files
prysm/testing/require/requires.go
Preston Van Loon 27c009e7ff Tests: Add require.Eventually and fix a few test flakes (#16217)
**What type of PR is this?**

Other

**What does this PR do? Why is it needed?**

This is a better way to wait for a test condition to hit, rather than
time.Sleep.

**Which issues(s) does this PR fix?**


**Other notes for review**

**Acknowledgements**

- [x] I have read
[CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md).
- [x] I have included a uniquely named [changelog fragment
file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd).
- [x] I have added a description with sufficient context for reviewers
to understand this PR.
- [x] I have tested that my changes work as expected and I added a
testing plan to the PR description (if applicable).
2026-01-06 18:20:27 +00:00

98 lines
4.0 KiB
Go

package require
import (
"time"
"github.com/OffchainLabs/prysm/v7/testing/assertions"
"github.com/sirupsen/logrus/hooks/test"
)
// Equal compares values using comparison operator.
func Equal(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.Equal(tb.Fatalf, expected, actual, msg...)
}
// NotEqual compares values using comparison operator.
func NotEqual(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.NotEqual(tb.Fatalf, expected, actual, msg...)
}
// DeepEqual compares values using DeepEqual.
// NOTE: this function does not work for checking arrays/slices or maps of protobuf messages.
// For arrays/slices, please use DeepSSZEqual.
// For maps, please iterate through and compare the individual keys and values.
func DeepEqual(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.DeepEqual(tb.Fatalf, expected, actual, msg...)
}
// DeepNotEqual compares values using DeepEqual.
// NOTE: this function does not work for checking arrays/slices or maps of protobuf messages.
// For arrays/slices, please use DeepNotSSZEqual.
// For maps, please iterate through and compare the individual keys and values.
func DeepNotEqual(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.DeepNotEqual(tb.Fatalf, expected, actual, msg...)
}
// DeepSSZEqual compares values using DeepEqual.
func DeepSSZEqual(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.DeepSSZEqual(tb.Fatalf, expected, actual, msg...)
}
// DeepNotSSZEqual compares values using DeepEqual.
func DeepNotSSZEqual(tb assertions.AssertionTestingTB, expected, actual any, msg ...any) {
assertions.DeepNotSSZEqual(tb.Fatalf, expected, actual, msg...)
}
// NoError asserts that error is nil.
func NoError(tb assertions.AssertionTestingTB, err error, msg ...any) {
assertions.NoError(tb.Fatalf, err, msg...)
}
// ErrorContains asserts that actual error contains wanted message.
func ErrorContains(tb assertions.AssertionTestingTB, want string, err error, msg ...any) {
assertions.ErrorContains(tb.Fatalf, want, err, msg...)
}
// IsNil asserts that the observed value is nil.
func IsNil(tb assertions.AssertionTestingTB, got any, msg ...any) {
assertions.IsNil(tb.Fatalf, got, msg...)
}
// NotNil asserts that passed value is not nil.
func NotNil(tb assertions.AssertionTestingTB, obj any, msg ...any) {
assertions.NotNil(tb.Fatalf, obj, msg...)
}
// LogsContain checks that the desired string is a subset of the current log output.
func LogsContain(tb assertions.AssertionTestingTB, hook *test.Hook, want string, msg ...any) {
assertions.LogsContain(tb.Fatalf, hook, want, true, msg...)
}
// LogsDoNotContain is the inverse check of LogsContain.
func LogsDoNotContain(tb assertions.AssertionTestingTB, hook *test.Hook, want string, msg ...any) {
assertions.LogsContain(tb.Fatalf, hook, want, false, msg...)
}
// NotEmpty checks that the object fields are not empty. This method also checks all of the
// pointer fields to ensure none of those fields are empty.
func NotEmpty(tb assertions.AssertionTestingTB, obj any, msg ...any) {
assertions.NotEmpty(tb.Fatalf, obj, msg...)
}
// ErrorIs uses Errors.Is to recursively unwrap err looking for target in the chain.
// If any error in the chain matches target, the assertion will pass.
func ErrorIs(tb assertions.AssertionTestingTB, err, target error, msg ...any) {
assertions.ErrorIs(tb.Fatalf, err, target, msg)
}
// StringContains asserts that actual string contains expected message.
func StringContains(tb assertions.AssertionTestingTB, expected, actual string, msg ...any) {
assertions.StringContains(tb.Fatalf, expected, actual, true, msg)
}
// Eventually asserts that given condition will be met within waitFor time,
// periodically checking target function each tick.
func Eventually(tb assertions.AssertionTestingTB, condition func() bool, waitFor, tick time.Duration, msg ...any) {
assertions.Eventually(tb.Fatalf, condition, waitFor, tick, msg...)
}