|
| 1 | +// nolint:dupl |
| 2 | +package flags |
| 3 | + |
| 4 | +import ( |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/pflag" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAddFlags(t *testing.T) { |
| 12 | + t.Run("AddFlags with default values", func(t *testing.T) { |
| 13 | + o := &BackoffOptions{} |
| 14 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 15 | + o.AddFlags(fs) |
| 16 | + |
| 17 | + // Check default values |
| 18 | + if o.Duration != 5*time.Millisecond { |
| 19 | + t.Errorf("Expected default Duration to be 5ms, got %v", o.Duration) |
| 20 | + } |
| 21 | + if o.Factor != 1.0 { |
| 22 | + t.Errorf("Expected default Factor to be 1.0, got %v", o.Factor) |
| 23 | + } |
| 24 | + if o.Jitter != 0.1 { |
| 25 | + t.Errorf("Expected default Jitter to be 0.1, got %v", o.Jitter) |
| 26 | + } |
| 27 | + if o.Steps != 5 { |
| 28 | + t.Errorf("Expected default Steps to be 5, got %v", o.Steps) |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("AddFlags with custom values", func(t *testing.T) { |
| 33 | + o := &BackoffOptions{} |
| 34 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 35 | + o.AddFlags(fs) |
| 36 | + |
| 37 | + // Check setting values |
| 38 | + err := fs.Parse([]string{"--retry-duration=1s", "--retry-factor=2.0", "--retry-jitter=0.2", "--retry-steps=10"}) |
| 39 | + if err != nil { |
| 40 | + t.Errorf("Expected no error, but got %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + if o.Duration != 1*time.Second { |
| 44 | + t.Errorf("Expected Duration to be 1s, got %v", o.Duration) |
| 45 | + } |
| 46 | + if o.Factor != 2.0 { |
| 47 | + t.Errorf("Expected Factor to be 2.0, got %v", o.Factor) |
| 48 | + } |
| 49 | + if o.Jitter != 0.2 { |
| 50 | + t.Errorf("Expected Jitter to be 0.2, got %v", o.Jitter) |
| 51 | + } |
| 52 | + if o.Steps != 10 { |
| 53 | + t.Errorf("Expected Steps to be 10, got %v", o.Steps) |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("AddFlags with zero values", func(t *testing.T) { |
| 58 | + o := &BackoffOptions{} |
| 59 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 60 | + o.AddFlags(fs) |
| 61 | + |
| 62 | + // Check setting values |
| 63 | + err := fs.Parse([]string{"--retry-duration=0", "--retry-factor=0.0", "--retry-jitter=0.0", "--retry-steps=0"}) |
| 64 | + if err != nil { |
| 65 | + t.Errorf("Expected no error, but got %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if o.Duration != 0 { |
| 69 | + t.Errorf("Expected Duration to be 0, got %v", o.Duration) |
| 70 | + } |
| 71 | + if o.Factor != 0.0 { |
| 72 | + t.Errorf("Expected Factor to be 0.0, got %v", o.Factor) |
| 73 | + } |
| 74 | + if o.Jitter != 0.0 { |
| 75 | + t.Errorf("Expected Jitter to be 0.0, got %v", o.Jitter) |
| 76 | + } |
| 77 | + if o.Steps != 0 { |
| 78 | + t.Errorf("Expected Steps to be 0, got %v", o.Steps) |
| 79 | + } |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func TestDefaultUpdateRetryBackoff(t *testing.T) { |
| 84 | + t.Run("Test with zero values", func(t *testing.T) { |
| 85 | + opts := BackoffOptions{} |
| 86 | + backoff := DefaultUpdateRetryBackoff(opts) |
| 87 | + |
| 88 | + if backoff.Duration != 5*time.Millisecond { |
| 89 | + t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration) |
| 90 | + } |
| 91 | + if backoff.Factor != 1.0 { |
| 92 | + t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor) |
| 93 | + } |
| 94 | + if backoff.Jitter != 0.1 { |
| 95 | + t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter) |
| 96 | + } |
| 97 | + if backoff.Steps != 5 { |
| 98 | + t.Errorf("Expected steps to be 5, got %v", backoff.Steps) |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("Test with custom values", func(t *testing.T) { |
| 103 | + opts := BackoffOptions{ |
| 104 | + Duration: 10 * time.Millisecond, |
| 105 | + Factor: 2.0, |
| 106 | + Jitter: 0.2, |
| 107 | + Steps: 10, |
| 108 | + } |
| 109 | + backoff := DefaultUpdateRetryBackoff(opts) |
| 110 | + |
| 111 | + if backoff.Duration != 10*time.Millisecond { |
| 112 | + t.Errorf("Expected duration to be 10ms, got %v", backoff.Duration) |
| 113 | + } |
| 114 | + if backoff.Factor != 2.0 { |
| 115 | + t.Errorf("Expected factor to be 2.0, got %v", backoff.Factor) |
| 116 | + } |
| 117 | + if backoff.Jitter != 0.2 { |
| 118 | + t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter) |
| 119 | + } |
| 120 | + if backoff.Steps != 10 { |
| 121 | + t.Errorf("Expected steps to be 10, got %v", backoff.Steps) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("Test with negative values", func(t *testing.T) { |
| 126 | + opts := BackoffOptions{ |
| 127 | + Duration: -1 * time.Millisecond, |
| 128 | + Factor: -2.0, |
| 129 | + Jitter: -0.2, |
| 130 | + Steps: -10, |
| 131 | + } |
| 132 | + backoff := DefaultUpdateRetryBackoff(opts) |
| 133 | + |
| 134 | + if backoff.Duration != 5*time.Millisecond { |
| 135 | + t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration) |
| 136 | + } |
| 137 | + if backoff.Factor != 1.0 { |
| 138 | + t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor) |
| 139 | + } |
| 140 | + if backoff.Jitter != 0.1 { |
| 141 | + t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter) |
| 142 | + } |
| 143 | + if backoff.Steps != 5 { |
| 144 | + t.Errorf("Expected steps to be 5, got %v", backoff.Steps) |
| 145 | + } |
| 146 | + }) |
| 147 | + |
| 148 | + t.Run("Test with part values", func(t *testing.T) { |
| 149 | + opts := BackoffOptions{ |
| 150 | + Jitter: 0.2, |
| 151 | + Steps: 10, |
| 152 | + } |
| 153 | + backoff := DefaultUpdateRetryBackoff(opts) |
| 154 | + |
| 155 | + if backoff.Duration != 5*time.Millisecond { |
| 156 | + t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration) |
| 157 | + } |
| 158 | + if backoff.Factor != 1.0 { |
| 159 | + t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor) |
| 160 | + } |
| 161 | + if backoff.Jitter != 0.2 { |
| 162 | + t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter) |
| 163 | + } |
| 164 | + if backoff.Steps != 10 { |
| 165 | + t.Errorf("Expected steps to be 10, got %v", backoff.Steps) |
| 166 | + } |
| 167 | + }) |
| 168 | +} |
0 commit comments