11package retry_test
22
33import (
4+ "fmt"
45 "testing"
56 "time"
67
78 "github.com/stretchr/testify/require"
89 "github.com/tkcrm/modules/pkg/retry"
910)
1011
12+ type logger struct {}
13+
14+ func (l * logger ) Infof (format string , args ... any ) {
15+ fmt .Println (fmt .Sprintf (format , args ... ))
16+ }
17+
1118func TestRetry_Do (t * testing.T ) {
19+ l := & logger {}
20+
1221 t .Run ("linear retry" , func (t * testing.T ) {
13- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
22+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
1423 err := r .Do (func () error {
1524 return nil
1625 })
1726 require .NoError (t , err )
1827 })
1928 t .Run ("backoff retry" , func (t * testing.T ) {
20- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
29+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
2130 err := r .Do (func () error {
2231 return nil
2332 })
2433 require .NoError (t , err )
2534 })
2635
2736 t .Run ("unsupported retry policy" , func (t * testing.T ) {
28- r := retry .New (retry .WithPolicy (retry .Policy (3 )), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
37+ r := retry .New (retry .WithPolicy (retry .Policy (3 )), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
2938 err := r .Do (func () error {
3039 return nil
3140 })
3241 require .Error (t , err )
3342 })
3443
3544 t .Run ("linear retry failed" , func (t * testing.T ) {
36- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
45+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
3746 err := r .Do (func () error {
3847 return retry .ErrRetry
3948 })
4049 require .Error (t , err )
4150 })
4251
4352 t .Run ("backoff retry failed" , func (t * testing.T ) {
44- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
53+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
4554 err := r .Do (func () error {
4655 return retry .ErrRetry
4756 })
4857 require .Error (t , err )
4958 })
5059
5160 t .Run ("linear retry failed after 3 attempts" , func (t * testing.T ) {
52- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
61+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
5362 err := r .Do (func () error {
5463 return retry .ErrRetry
5564 })
@@ -58,7 +67,7 @@ func TestRetry_Do(t *testing.T) {
5867 })
5968
6069 t .Run ("backoff retry failed after 3 attempts" , func (t * testing.T ) {
61- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
70+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
6271 err := r .Do (func () error {
6372 return retry .ErrRetry
6473 })
@@ -67,7 +76,7 @@ func TestRetry_Do(t *testing.T) {
6776 })
6877
6978 t .Run ("linear retry failed after 3 attempts with custom delay" , func (t * testing.T ) {
70- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
79+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (3 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
7180 err := r .Do (func () error {
7281 return retry .ErrRetry
7382 })
@@ -76,7 +85,7 @@ func TestRetry_Do(t *testing.T) {
7685 })
7786
7887 t .Run ("backoff retry failed after 3 attempts with custom delay" , func (t * testing.T ) {
79- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
88+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (3 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
8089 err := r .Do (func () error {
8190 return retry .ErrRetry
8291 })
@@ -85,7 +94,7 @@ func TestRetry_Do(t *testing.T) {
8594 })
8695
8796 t .Run ("linear retry failed after 3 attempts with custom max attempts" , func (t * testing.T ) {
88- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
97+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
8998 err := r .Do (func () error {
9099 return retry .ErrRetry
91100 })
@@ -94,7 +103,7 @@ func TestRetry_Do(t *testing.T) {
94103 })
95104
96105 t .Run ("backoff retry failed after 3 attempts with custom max attempts" , func (t * testing.T ) {
97- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
106+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
98107 err := r .Do (func () error {
99108 return retry .ErrRetry
100109 })
@@ -103,7 +112,7 @@ func TestRetry_Do(t *testing.T) {
103112 })
104113
105114 t .Run ("linear retry failed after 3 attempts with custom max attempts and delay" , func (t * testing.T ) {
106- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
115+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
107116 err := r .Do (func () error {
108117 return retry .ErrRetry
109118 })
@@ -112,7 +121,7 @@ func TestRetry_Do(t *testing.T) {
112121 })
113122
114123 t .Run ("backoff retry failed after 3 attempts with custom max attempts and delay" , func (t * testing.T ) {
115- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
124+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
116125 err := r .Do (func () error {
117126 return retry .ErrRetry
118127 })
@@ -121,7 +130,7 @@ func TestRetry_Do(t *testing.T) {
121130 })
122131
123132 t .Run ("linear retry failed after 3 attempts with custom max attempts and delay and custom retry function" , func (t * testing.T ) {
124- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
133+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
125134 err := r .Do (func () error {
126135 return retry .ErrRetry
127136 })
@@ -130,7 +139,7 @@ func TestRetry_Do(t *testing.T) {
130139 })
131140
132141 t .Run ("backoff retry failed after 3 attempts with custom max attempts and delay and custom retry function" , func (t * testing.T ) {
133- r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetDebug ( true )
142+ r := retry .New (retry .WithPolicy (retry .PolicyBackoff ), retry .WithMaxAttempts (5 ), retry .WithDelay (400 * time .Millisecond )).SetLogger ( l )
134143 err := r .Do (func () error {
135144 return retry .ErrRetry
136145 })
@@ -139,7 +148,7 @@ func TestRetry_Do(t *testing.T) {
139148 })
140149
141150 t .Run ("linear retry failed with exit error" , func (t * testing.T ) {
142- r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithDelay (200 * time .Millisecond )).SetDebug ( true )
151+ r := retry .New (retry .WithPolicy (retry .PolicyLinear ), retry .WithDelay (200 * time .Millisecond )).SetLogger ( l )
143152 var attempt int
144153 err := r .Do (func () error {
145154 attempt ++
0 commit comments