-
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.
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 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,69 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DeBankDeFi/golib/util" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSha1(t *testing.T) { | ||
testCases := []struct { | ||
input []string | ||
expectedLen int | ||
}{ | ||
{ | ||
input: []string{util.RandomName(8), util.RandomName(16)}, | ||
expectedLen: 40, | ||
}, | ||
{ | ||
input: []string{util.RandomName(1), util.RandomName(2)}, | ||
expectedLen: 40, | ||
}, | ||
{ | ||
input: []string{util.RandomName(0), util.RandomName(0)}, | ||
expectedLen: 40, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run("", func(t *testing.T) { | ||
sha := util.Sha1(tc.input...) | ||
require.Len(t, sha, tc.expectedLen) | ||
}) | ||
} | ||
} | ||
|
||
func TestSha1Idempotent(t *testing.T) { | ||
testCases := []struct { | ||
origin string | ||
calcCount int | ||
}{ | ||
{ | ||
origin: util.RandomName(64), | ||
calcCount: 30, | ||
}, | ||
{ | ||
origin: util.RandomName(32), | ||
calcCount: 50, | ||
}, | ||
{ | ||
origin: util.RandomName(16), | ||
calcCount: 100, | ||
}, | ||
{ | ||
origin: util.RandomName(0), | ||
calcCount: 200, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run("", func(t *testing.T) { | ||
oriSha := util.Sha1(tc.origin) | ||
for i := 0; i < tc.calcCount; i++ { | ||
sha := util.Sha1(tc.origin) | ||
require.Equal(t, oriSha, sha, "same input string produce diverse sha result") | ||
} | ||
}) | ||
} | ||
} |
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 util | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// RetryFunc ... | ||
type RetryFunc func() error | ||
|
||
// Retry retryable function with attempts and delay. | ||
func Retry(rf RetryFunc, attempts int, delay time.Duration) error { | ||
if err := rf(); err != nil { | ||
if s, ok := err.(RetryableError); !ok { | ||
return s | ||
} | ||
if attempts--; attempts > 0 { | ||
time.Sleep(delay) | ||
return Retry(rf, attempts, delay) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type RetryableError struct { | ||
err error | ||
} | ||
|
||
// NewRetryableError ... | ||
func NewRetryableError(err error) RetryableError { | ||
return RetryableError{err: err} | ||
} | ||
|
||
func (re RetryableError) Error() string { | ||
if re.err != nil { | ||
return re.err.Error() | ||
} | ||
return "" | ||
} |
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,27 @@ | ||
package util_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/DeBankDeFi/golib/util" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
testCases := []util.RetryFunc{ | ||
func() error { | ||
return nil | ||
}, | ||
func() error { | ||
return util.RetryableError{} | ||
}, | ||
func() error { | ||
return fmt.Errorf("hehe") | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
_ = util.Retry(tc, 10, time.Second) | ||
} | ||
} |