github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/synckit/backoff_test.go (about) 1 // Copyright 2020 Insolar Network Ltd. 2 // All rights reserved. 3 // This material is licensed under the Insolar License version 1.0, 4 // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md. 5 6 package synckit 7 8 import ( 9 "reflect" 10 "testing" 11 "time" 12 ) 13 14 func Test1(t *testing.T) { 15 16 b := &Backoff{ 17 Min: 100 * time.Millisecond, 18 Max: 10 * time.Second, 19 Factor: 2, 20 } 21 22 equals(t, b.Duration(), 100*time.Millisecond) 23 equals(t, b.Duration(), 200*time.Millisecond) 24 equals(t, b.Duration(), 400*time.Millisecond) 25 b.Reset() 26 equals(t, b.Duration(), 100*time.Millisecond) 27 } 28 29 func TestForAttempt(t *testing.T) { 30 31 b := &Backoff{ 32 Min: 100 * time.Millisecond, 33 Max: 10 * time.Second, 34 Factor: 2, 35 } 36 37 equals(t, b.ForAttempt(0), 100*time.Millisecond) 38 equals(t, b.ForAttempt(1), 200*time.Millisecond) 39 equals(t, b.ForAttempt(2), 400*time.Millisecond) 40 b.Reset() 41 equals(t, b.ForAttempt(0), 100*time.Millisecond) 42 } 43 44 func Test2(t *testing.T) { 45 46 b := &Backoff{ 47 Min: 100 * time.Millisecond, 48 Max: 10 * time.Second, 49 Factor: 1.5, 50 } 51 52 equals(t, b.Duration(), 100*time.Millisecond) 53 equals(t, b.Duration(), 150*time.Millisecond) 54 equals(t, b.Duration(), 225*time.Millisecond) 55 b.Reset() 56 equals(t, b.Duration(), 100*time.Millisecond) 57 } 58 59 func Test3(t *testing.T) { 60 61 b := &Backoff{ 62 Min: 100 * time.Nanosecond, 63 Max: 10 * time.Second, 64 Factor: 1.75, 65 } 66 67 equals(t, b.Duration(), 100*time.Nanosecond) 68 equals(t, b.Duration(), 175*time.Nanosecond) 69 equals(t, b.Duration(), 306*time.Nanosecond) 70 b.Reset() 71 equals(t, b.Duration(), 100*time.Nanosecond) 72 } 73 74 func Test4(t *testing.T) { 75 b := &Backoff{ 76 Min: 500 * time.Second, 77 Max: 100 * time.Second, 78 Factor: 1, 79 } 80 81 equals(t, b.Duration(), b.Max) 82 } 83 84 func TestGetAttempt(t *testing.T) { 85 b := &Backoff{ 86 Min: 100 * time.Millisecond, 87 Max: 10 * time.Second, 88 Factor: 2, 89 } 90 equals(t, b.Attempt(), 0) 91 equals(t, b.Duration(), 100*time.Millisecond) 92 equals(t, b.Attempt(), 1) 93 equals(t, b.Duration(), 200*time.Millisecond) 94 equals(t, b.Attempt(), 2) 95 equals(t, b.Duration(), 400*time.Millisecond) 96 equals(t, b.Attempt(), 3) 97 b.Reset() 98 equals(t, b.Attempt(), 0) 99 equals(t, b.Duration(), 100*time.Millisecond) 100 equals(t, b.Attempt(), 1) 101 } 102 103 func TestJitter(t *testing.T) { 104 b := &Backoff{ 105 Min: 100 * time.Millisecond, 106 Max: 10 * time.Second, 107 Factor: 2, 108 Jitter: true, 109 } 110 111 equals(t, b.Duration(), 100*time.Millisecond) 112 between(t, b.Duration(), 100*time.Millisecond, 200*time.Millisecond) 113 between(t, b.Duration(), 100*time.Millisecond, 400*time.Millisecond) 114 b.Reset() 115 equals(t, b.Duration(), 100*time.Millisecond) 116 } 117 118 func TestCopy(t *testing.T) { 119 b := &Backoff{ 120 Min: 100 * time.Millisecond, 121 Max: 10 * time.Second, 122 Factor: 2, 123 } 124 b2 := b.Copy() 125 equals(t, b, b2) 126 } 127 128 func between(t *testing.T, actual, low, high time.Duration) { 129 if actual < low { 130 t.Fatalf("Got %s, Expecting >= %s", actual, low) 131 } 132 if actual > high { 133 t.Fatalf("Got %s, Expecting <= %s", actual, high) 134 } 135 } 136 137 func equals(t *testing.T, v1, v2 interface{}) { 138 if !reflect.DeepEqual(v1, v2) { 139 t.Fatalf("Got %v, Expecting %v", v1, v2) 140 } 141 }