forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding attempt + repeat + times
- Loading branch information
Showing
7 changed files
with
241 additions
and
11 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
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,16 @@ | ||
package lo | ||
|
||
// Attempt invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned. | ||
func Attempt(maxIteration int, f func(int) error) (int, error) { | ||
var err error | ||
|
||
for i := 0; maxIteration <= 0 || i < maxIteration; i++ { | ||
// for retries >= 0 { | ||
err = f(i) | ||
if err == nil { | ||
return i + 1, nil | ||
} | ||
} | ||
|
||
return maxIteration, err | ||
} |
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,48 @@ | ||
package lo | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAttempt(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
err := fmt.Errorf("failed") | ||
|
||
iter1, err1 := Attempt(42, func(i int) error { | ||
return nil | ||
}) | ||
iter2, err2 := Attempt(42, func(i int) error { | ||
if i == 5 { | ||
return nil | ||
} | ||
|
||
return err | ||
}) | ||
iter3, err3 := Attempt(2, func(i int) error { | ||
if i == 5 { | ||
return nil | ||
} | ||
|
||
return err | ||
}) | ||
iter4, err4 := Attempt(0, func(i int) error { | ||
if i < 42 { | ||
return fmt.Errorf("failed") | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
is.Equal(iter1, 1) | ||
is.Equal(err1, nil) | ||
is.Equal(iter2, 6) | ||
is.Equal(err2, nil) | ||
is.Equal(iter3, 2) | ||
is.Equal(err3, err) | ||
is.Equal(iter4, 43) | ||
is.Equal(err4, nil) | ||
} |
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