-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Dean Oren <[email protected]>
- Loading branch information
Showing
4 changed files
with
135 additions
and
6 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
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
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,42 @@ | ||
package retry | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestRetry_Until(t *testing.T) { | ||
r := New() | ||
r.UntilFns = []func(*http.Response) bool{noOp} | ||
|
||
type args struct { | ||
f []func(*http.Response) bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Retry | ||
}{ | ||
{"ok", args{[]func(*http.Response) bool{noOp}}, r}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := New() | ||
got := c.Until(tt.args.f...) | ||
if len(got.UntilFns) != len(r.UntilFns) { | ||
t.Error("wrong lengths") | ||
return | ||
} | ||
for k, v := range got.UntilFns { | ||
if GetFunctionName(r.UntilFns[k]) != GetFunctionName(v) { | ||
t.Errorf("%s != %s", GetFunctionName(r.UntilFns[k]), GetFunctionName(v)) | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func noOp(_ *http.Response) bool { | ||
return true | ||
} |
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,31 @@ | ||
package retry | ||
|
||
import "net/http" | ||
|
||
const ( | ||
// Until configuration constants | ||
CONFIG_UNTIL = "UntilFns" | ||
) | ||
|
||
// UntilFns is the config struct | ||
type UntilFns struct { | ||
fnList []func(*http.Response) bool | ||
} | ||
|
||
// Until sets functions that determin if the response given is the expected response | ||
// this functionality is useful, for example, when waiting for an API status to be ready | ||
func (c *Retry) Until(f ...func(*http.Response) bool) *Retry { | ||
return c.withConfig(&UntilFns{ | ||
fnList: f, | ||
}) | ||
} | ||
|
||
var _ = Config(&UntilFns{}) | ||
|
||
func (c *UntilFns) String() string { | ||
return CONFIG_UNTIL | ||
} | ||
|
||
func (c *UntilFns) Value() interface{} { | ||
return c.fnList | ||
} |