test/integration: add test helper to avoid race conditions

We were seeing a race in this test code since the require.NoError() and
require.Eventually() would write to the same testing.T state on separate
goroutines. Hopefully this helper function should cover the cases when we want
to require.NoError() inside a require.Eventually() without causing a race.

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
Co-authored-by: Margo Crawford <margaretc@vmware.com>
Co-authored-by: Monis Khan <i@monis.app>
This commit is contained in:
Andrew Keesler
2021-01-14 10:19:35 -05:00
co-authored by Margo Crawford Monis Khan
parent a0546942b8
commit 8a916ce8ae
2 changed files with 30 additions and 5 deletions
+4 -5
View File
@@ -258,16 +258,15 @@ func hasOwnerRef(t *testing.T, obj metav1.Object, ref metav1.OwnerReference) {
func isEventuallyDeleted(t *testing.T, f func() error) {
t.Helper()
require.Eventually(t, func() bool {
library.RequireEventuallyWithoutError(t, func() (bool, error) {
err := f()
switch {
case err == nil:
return false
return false, nil
case errors.IsNotFound(err):
return true
return true, nil
default:
require.NoError(t, err)
return false
return false, err
}
}, time.Minute, time.Second)
}