|
1 | 1 | package docker
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "math" |
4 | 6 | "net/http"
|
5 | 7 | "testing"
|
| 8 | + "time" |
6 | 9 |
|
| 10 | + "github.com/sirupsen/logrus" |
7 | 11 | "github.com/stretchr/testify/assert"
|
8 | 12 | "github.com/stretchr/testify/require"
|
9 | 13 | )
|
@@ -108,3 +112,85 @@ func TestParseContentRange(t *testing.T) {
|
108 | 112 | assert.Error(t, err, c, c)
|
109 | 113 | }
|
110 | 114 | }
|
| 115 | + |
| 116 | +func TestMillisecondsSinceOptional(t *testing.T) { |
| 117 | + current := time.Date(2023, 2, 9, 8, 7, 6, 5, time.UTC) |
| 118 | + res := millisecondsSinceOptional(current, time.Time{}) |
| 119 | + assert.True(t, math.IsNaN(res)) |
| 120 | + tm := current.Add(-60 * time.Second) // 60 seconds _before_ current |
| 121 | + res = millisecondsSinceOptional(current, tm) |
| 122 | + assert.Equal(t, res, 60_000.0) |
| 123 | +} |
| 124 | + |
| 125 | +func TestBodyReaderErrorIfNotReconnecting(t *testing.T) { |
| 126 | + // Silence logrus.Info logs in the tested method |
| 127 | + prevLevel := logrus.StandardLogger().Level |
| 128 | + logrus.StandardLogger().SetLevel(logrus.WarnLevel) |
| 129 | + t.Cleanup(func() { |
| 130 | + logrus.StandardLogger().SetLevel(prevLevel) |
| 131 | + }) |
| 132 | + |
| 133 | + for _, c := range []struct { |
| 134 | + name string |
| 135 | + previousRetry bool |
| 136 | + currentOffset int64 |
| 137 | + currentTime int // milliseconds |
| 138 | + expectReconnect bool |
| 139 | + }{ |
| 140 | + { |
| 141 | + name: "A lot of progress, after a long time, second retry", |
| 142 | + previousRetry: true, |
| 143 | + currentOffset: 2 * bodyReaderMinimumProgress, |
| 144 | + currentTime: 2 * bodyReaderMSSinceLastRetry, |
| 145 | + expectReconnect: true, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "A lot of progress, after little time, second retry", |
| 149 | + previousRetry: true, |
| 150 | + currentOffset: 2 * bodyReaderMinimumProgress, |
| 151 | + currentTime: 1, |
| 152 | + expectReconnect: true, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "Little progress, after a long time, second retry", |
| 156 | + previousRetry: true, |
| 157 | + currentOffset: 1, |
| 158 | + currentTime: 2 * bodyReaderMSSinceLastRetry, |
| 159 | + expectReconnect: true, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "Little progress, after little time, second retry", |
| 163 | + previousRetry: true, |
| 164 | + currentOffset: 1, |
| 165 | + currentTime: 1, |
| 166 | + expectReconnect: false, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "Little progress, after little time, first retry", |
| 170 | + previousRetry: false, |
| 171 | + currentOffset: 1, |
| 172 | + currentTime: bodyReaderMSSinceLastRetry / 2, |
| 173 | + expectReconnect: true, |
| 174 | + }, |
| 175 | + } { |
| 176 | + tm := time.Now() |
| 177 | + br := bodyReader{} |
| 178 | + if c.previousRetry { |
| 179 | + br.lastRetryOffset = 2 * bodyReaderMinimumProgress |
| 180 | + br.offset = br.lastRetryOffset + c.currentOffset |
| 181 | + br.firstConnectionTime = tm.Add(-time.Duration(c.currentTime+2*bodyReaderMSSinceLastRetry) * time.Millisecond) |
| 182 | + br.lastRetryTime = tm.Add(-time.Duration(c.currentTime) * time.Millisecond) |
| 183 | + } else { |
| 184 | + br.lastRetryOffset = -1 |
| 185 | + br.lastRetryTime = time.Time{} |
| 186 | + br.offset = c.currentOffset |
| 187 | + br.firstConnectionTime = tm.Add(-time.Duration(c.currentTime) * time.Millisecond) |
| 188 | + } |
| 189 | + err := br.errorIfNotReconnecting(errors.New("some error for error text only"), "URL for error text only") |
| 190 | + if c.expectReconnect { |
| 191 | + assert.NoError(t, err, c.name, br) |
| 192 | + } else { |
| 193 | + assert.Error(t, err, c.name, br) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments