-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
327 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// Package retry provides support for repeating operations in tests. | ||
// | ||
// A sample retry operation looks like this: | ||
// | ||
// func TestX(t *testing.T) { | ||
// retry.Run(t, func(r *retry.R) { | ||
// if err := foo(); err != nil { | ||
// r.Errorf("foo: %s", err) | ||
// return | ||
// } | ||
// }) | ||
// } | ||
// | ||
// Run uses the DefaultFailer, which is a Timer with a Timeout of 7s, | ||
// and a Wait of 25ms. To customize, use RunWith. | ||
// | ||
// WARNING: unlike *testing.T, *retry.R#Fatal and FailNow *do not* | ||
// fail the test function entirely, only the current run the retry func | ||
package retry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package retry | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var nilInf TestingTB = nil | ||
|
||
// Assertion that our TestingTB can be passed to | ||
var _ require.TestingT = nilInf | ||
var _ assert.TestingT = nilInf | ||
|
||
// TestingTB is an interface that describes the implementation of the testing object. | ||
// Using an interface that describes testing.TB instead of the actual implementation | ||
// makes testutil usable in a wider variety of contexts (e.g. use with ginkgo : https://godoc.org/github.com/onsi/ginkgo#GinkgoT) | ||
type TestingTB interface { | ||
Cleanup(func()) | ||
Error(args ...any) | ||
Errorf(format string, args ...any) | ||
Fail() | ||
FailNow() | ||
Failed() bool | ||
Fatal(args ...any) | ||
Fatalf(format string, args ...any) | ||
Helper() | ||
Log(args ...any) | ||
Logf(format string, args ...any) | ||
Name() string | ||
Setenv(key, value string) | ||
TempDir() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package retry | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func dedup(a []string) string { | ||
if len(a) == 0 { | ||
return "" | ||
} | ||
seen := map[string]struct{}{} | ||
var b bytes.Buffer | ||
for _, s := range a { | ||
if _, ok := seen[s]; ok { | ||
continue | ||
} | ||
seen[s] = struct{}{} | ||
b.WriteString(s) | ||
b.WriteRune('\n') | ||
} | ||
return b.String() | ||
} | ||
|
||
func decorate(s string) string { | ||
_, file, line, ok := runtime.Caller(3) | ||
if ok { | ||
n := strings.LastIndex(file, "/") | ||
if n >= 0 { | ||
file = file[n+1:] | ||
} | ||
} else { | ||
file = "???" | ||
line = 1 | ||
} | ||
return fmt.Sprintf("%s:%d: %s", file, line, s) | ||
} |
Oops, something went wrong.