github.com/kaydxh/golang@v0.0.131/go/time/wait_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package time_test 23 24 import ( 25 "context" 26 "fmt" 27 "testing" 28 "time" 29 30 time_ "github.com/kaydxh/golang/go/time" 31 ) 32 33 func TestBackOffUntilWithContext(t *testing.T) { 34 testCases := []struct { 35 name string 36 period time.Duration 37 sliding bool 38 f func(context.Context) error 39 loop bool 40 }{ 41 { 42 name: "test-sliding", 43 period: 5 * time.Second, 44 sliding: true, 45 f: func(context.Context) error { 46 time.Sleep(time.Second) 47 fmt.Println("test-sliding") 48 return nil 49 }, 50 loop: true, 51 }, 52 { 53 name: "test-nonsliding", 54 sliding: false, 55 period: 5 * time.Second, 56 f: func(context.Context) error { 57 time.Sleep(time.Second) 58 fmt.Println("test-nonsliding") 59 return nil 60 }, 61 loop: true, 62 }, 63 { 64 name: "test-nonsliding", 65 sliding: false, 66 period: 5 * time.Second, 67 f: func(context.Context) error { 68 time.Sleep(time.Second) 69 fmt.Println("test-nonsliding") 70 return nil 71 }, 72 loop: false, 73 }, 74 } 75 76 for _, testCase := range testCases { 77 t.Run(testCase.name, func(t *testing.T) { 78 //ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) 79 //defer cancel() 80 ctx := context.Background() 81 82 time_.BackOffUntilWithContext( 83 ctx, 84 testCase.f, 85 time_.NewExponentialBackOff( 86 // forever run 87 //time_.WithExponentialBackOffOptionMaxElapsedTime(0), 88 time_.WithExponentialBackOffOptionMaxElapsedTime(20*time.Second), 89 time_.WithExponentialBackOffOptionInitialInterval(testCase.period), 90 time_.WithExponentialBackOffOptionMultiplier(1), 91 time_.WithExponentialBackOffOptionRandomizationFactor(0), 92 ), 93 testCase.sliding, 94 true, 95 ) 96 /* 97 if err != nil { 98 t.Fatalf("failed to write file: %v, got : %s", testCase.name, err) 99 100 } 101 */ 102 103 }) 104 } 105 } 106 107 func TestRetryWithContext(t *testing.T) { 108 testCases := []struct { 109 name string 110 period time.Duration 111 retryTime int 112 f func(context.Context) error 113 }{ 114 { 115 name: "test-retry-nil", 116 period: 5 * time.Second, 117 retryTime: 3, 118 f: func(context.Context) error { 119 time.Sleep(time.Second) 120 fmt.Println("test-sliding") 121 return nil 122 }, 123 }, 124 { 125 name: "test-retry-error", 126 period: 5 * time.Second, 127 retryTime: 0, 128 f: func(context.Context) error { 129 time.Sleep(time.Second) 130 fmt.Println("test-retry-error-sliding") 131 return fmt.Errorf("error") 132 }, 133 }, 134 } 135 136 for _, testCase := range testCases { 137 t.Run(testCase.name, func(t *testing.T) { 138 //ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) 139 //defer cancel() 140 ctx := context.Background() 141 142 err := time_.RetryWithContext( 143 ctx, 144 testCase.f, 145 testCase.period, 146 testCase.retryTime, 147 ) 148 if err != nil { 149 t.Fatalf("failed to call RetryWithContext: %v, got : %s", testCase.name, err) 150 } 151 152 }) 153 } 154 }