| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/config" |
| 11 | "github.com/git-lfs/git-lfs/v3/internal/testutil" |
| 12 | "github.com/git-lfs/git-lfs/v3/tasklog" |
| 13 | "github.com/git-lfs/git-lfs/v3/tools" |
| 14 | "github.com/jmhodges/clock" |
| 15 | "github.com/stretchr/testify/assert" |
| 16 | ) |
| 17 | |
| 18 | func TestBothCallbackReadersWithCallback(t *testing.T) { |
| 19 | var calls int |
| 20 | allReadSoFar := make([]int64, 0, 2) |
| 21 | |
| 22 | buf := []byte("BOOYA") |
| 23 | bufSize := len(buf) |
| 24 | |
| 25 | cb := func(totalSize int64, readSoFar int64, readSinceLast int) error { |
| 26 | calls++ |
| 27 | allReadSoFar = append(allReadSoFar, readSoFar) |
| 28 | |
| 29 | assert.EqualValues(t, bufSize, totalSize) |
| 30 | |
| 31 | return nil |
| 32 | } |
| 33 | |
| 34 | readBuf := make([]byte, bufSize-2) |
| 35 | readBufSize := len(readBuf) |
| 36 | |
| 37 | r := tools.NewCallbackReader(bytes.NewReader(buf), int64(bufSize), cb) |
| 38 | br := tools.NewBodyWithCallback(tools.NewClosingByteReader(buf), int64(bufSize), cb) |
| 39 | |
| 40 | for _, reader := range []io.Reader{r, br} { |
| 41 | t.Logf("testing with reader: %T", reader) |
| 42 | |
| 43 | n, err := reader.Read(readBuf) |
| 44 | |
| 45 | // The underlying bytes.Reader should always return |
| 46 | // a nil error when the last byte is read. |
| 47 | assert.Equal(t, readBufSize, n) |
| 48 | assert.Nil(t, err) |
| 49 | assert.Equal(t, buf[:readBufSize], readBuf) |
| 50 | |
| 51 | n, err = reader.Read(readBuf) |
| 52 | |
| 53 | assert.Equal(t, bufSize-readBufSize, n) |
| 54 | assert.Nil(t, err) |
| 55 | assert.Equal(t, buf[readBufSize:], readBuf[:bufSize-readBufSize]) |
| 56 | |
| 57 | assert.Equal(t, 2, calls) |
| 58 | assert.Len(t, allReadSoFar, 2) |
| 59 | assert.EqualValues(t, readBufSize, allReadSoFar[0]) |
| 60 | assert.EqualValues(t, bufSize, allReadSoFar[1]) |
| 61 | |
| 62 | calls = 0 |
| 63 | allReadSoFar = allReadSoFar[:0] |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | type copyCallbackFileThrottleTestMode int |
| 68 | |
| 69 | const ( |
| 70 | initialTotalCorrect copyCallbackFileThrottleTestMode = iota |
| 71 | initialTotalOverestimated |
| 72 | initialTotalUnderestimated |
| 73 | initialTotalUnknown |
| 74 | ) |
| 75 | |
| 76 | type copyCallbackFileThrottleTestCase struct { |
| 77 | mode copyCallbackFileThrottleTestMode |
| 78 | readerBufSize int64 |
| 79 | delayedEOF bool |
| 80 | |
| 81 | logFilePath string |
| 82 | fakeClock clock.FakeClock |
| 83 | callbackReader *tools.CallbackReader |
| 84 | |
| 85 | expectedFractions []string |
| 86 | } |
| 87 | |
| 88 | func (c *copyCallbackFileThrottleTestCase) setup(t *testing.T) (*os.File, error) { |
| 89 | tmpDir := t.TempDir() |
| 90 | c.logFilePath = tmpDir + "/git_lfs_progress.log" |
| 91 | osMf := config.UniqMapFetcher(map[string]string{ |
| 92 | "GIT_LFS_PROGRESS": c.logFilePath, |
| 93 | }) |
| 94 | gitMf := config.UniqMapFetcher(map[string]string{}) |
| 95 | |
| 96 | c.fakeClock = clock.NewFake() |
| 97 | gf := GitFilter{ |
| 98 | cfg: &config.Configuration{ |
| 99 | Os: config.EnvironmentOf(osMf), |
| 100 | Git: config.EnvironmentOf(gitMf), |
| 101 | }, |
| 102 | clk: c.fakeClock, |
| 103 | } |
| 104 | |
| 105 | cb, f, err := gf.CopyCallbackFile("clean", "test_copy", 1, 1) |
| 106 | if err != nil { |
| 107 | return f, fmt.Errorf("unable to create progress log callback: %w", err) |
| 108 | } |
| 109 | |
| 110 | // The Assert() method requires at least four separate reads, |
| 111 | // and three additional reads if we are simulating an underestimated |
| 112 | // initial total size. |
| 113 | bufSize := 4 * c.readerBufSize |
| 114 | initialTotalSize := bufSize |
| 115 | switch c.mode { |
| 116 | case initialTotalOverestimated: |
| 117 | bufSize -= c.readerBufSize |
| 118 | case initialTotalUnderestimated: |
| 119 | bufSize += 3 * c.readerBufSize |
| 120 | case initialTotalUnknown: |
| 121 | initialTotalSize = -1 |
| 122 | } |
| 123 | |
| 124 | buf := make([]byte, bufSize) |
| 125 | |
| 126 | var r io.Reader |
| 127 | if c.delayedEOF { |
| 128 | r = testutil.NewDeferredEOFByteReader(buf) |
| 129 | } else { |
| 130 | r = testutil.NewEagerEOFByteReader(buf) |
| 131 | } |
| 132 | |
| 133 | c.callbackReader = tools.NewCallbackReader(r, initialTotalSize, cb) |
| 134 | |
| 135 | return f, err |
| 136 | } |
| 137 | |
| 138 | func (c *copyCallbackFileThrottleTestCase) Assert(t *testing.T) { |
| 139 | buf := make([]byte, c.readerBufSize) |
| 140 | |
| 141 | // Read #1: No message should be logged as the deadline has not passed. |
| 142 | n, err := c.callbackReader.Read(buf) |
| 143 | assert.EqualValues(t, c.readerBufSize, n) |
| 144 | assert.Nil(t, err) |
| 145 | |
| 146 | c.fakeClock.Add(tasklog.DefaultLoggingThrottle) |
| 147 | |
| 148 | // Read #2: A message should be logged as the deadline has passed. |
| 149 | n, err = c.callbackReader.Read(buf) |
| 150 | assert.EqualValues(t, c.readerBufSize, n) |
| 151 | assert.Nil(t, err) |
| 152 | |
| 153 | c.fakeClock.Add(tasklog.DefaultLoggingThrottle / 2) |
| 154 | |
| 155 | // Read #3: When the total size was initially correct, unknown, or |
| 156 | // underestimated, or when EOF is delayed, no message |
| 157 | // should be logged as the full deadline has not passed. |
| 158 | // |
| 159 | // However, when the total size was initially overestimated |
| 160 | // and EOF is not delayed, a message should be logged |
| 161 | // because the actual total is now known. |
| 162 | n, err = c.callbackReader.Read(buf) |
| 163 | assert.EqualValues(t, c.readerBufSize, n) |
| 164 | if c.mode != initialTotalOverestimated || c.delayedEOF { |
| 165 | assert.Nil(t, err) |
| 166 | } else { |
| 167 | assert.Equal(t, io.EOF, err) |
| 168 | } |
| 169 | |
| 170 | if c.mode != initialTotalOverestimated { |
| 171 | // Read #4: When the total size was initially unknown and |
| 172 | // EOF is delayed, no message should be logged as |
| 173 | // the full deadline has not passed. |
| 174 | // |
| 175 | // Otherwise, a message should be logged even though |
| 176 | // the full deadline has not passed either because |
| 177 | // the initial total has been reached or because the |
| 178 | // actual total is now known. |
| 179 | n, err = c.callbackReader.Read(buf) |
| 180 | assert.EqualValues(t, c.readerBufSize, n) |
| 181 | if c.mode == initialTotalUnderestimated || c.delayedEOF { |
| 182 | assert.Nil(t, err) |
| 183 | } else { |
| 184 | assert.Equal(t, io.EOF, err) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if c.mode == initialTotalUnderestimated { |
| 189 | // Read #5: No message should be logged as the deadline |
| 190 | // has not passed, even though the initial total |
| 191 | // has been exceeded. |
| 192 | n, err = c.callbackReader.Read(buf) |
| 193 | assert.EqualValues(t, c.readerBufSize, n) |
| 194 | assert.Nil(t, err) |
| 195 | |
| 196 | c.fakeClock.Add(tasklog.DefaultLoggingThrottle) |
| 197 | |
| 198 | // Read #6: A message should be logged as the deadline |
| 199 | // has passed, even though the initial total |
| 200 | // has been exceeded. |
| 201 | n, err = c.callbackReader.Read(buf) |
| 202 | assert.EqualValues(t, c.readerBufSize, n) |
| 203 | assert.Nil(t, err) |
| 204 | |
| 205 | // Read #7: When EOF is delayed, no message should be logged |
| 206 | // as the deadline has not passed, even though the |
| 207 | // initial total has been exceeded. |
| 208 | // |
| 209 | // Otherwise, a message should be logged even though |
| 210 | // the deadline has not passed because the actual |
| 211 | // total is now known. |
| 212 | n, err = c.callbackReader.Read(buf) |
| 213 | assert.EqualValues(t, c.readerBufSize, n) |
| 214 | if c.delayedEOF { |
| 215 | assert.Nil(t, err) |
| 216 | } else { |
| 217 | assert.Equal(t, io.EOF, err) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if c.delayedEOF { |
| 222 | // Final EOF Read: When the total size was initially correct, |
| 223 | // no callback should be made and so no |
| 224 | // message should be logged. |
| 225 | // |
| 226 | // Otherwise, a message should be logged |
| 227 | // as the actual total is now known, even |
| 228 | // though the deadline has not passed. |
| 229 | n, err = c.callbackReader.Read(buf) |
| 230 | assert.Zero(t, n) |
| 231 | assert.Equal(t, io.EOF, err) |
| 232 | } |
| 233 | |
| 234 | logBytes, err := os.ReadFile(c.logFilePath) |
| 235 | assert.Nil(t, err) |
| 236 | |
| 237 | var expectedLog string |
| 238 | for _, expectedFraction := range c.expectedFractions { |
| 239 | expectedLog += fmt.Sprintf("clean 1/1 %s test_copy\n", expectedFraction) |
| 240 | } |
| 241 | |
| 242 | assert.Equal(t, expectedLog, string(logBytes)) |
| 243 | } |
| 244 | |
| 245 | func TestCopyCallbackFileThrottle(t *testing.T) { |
| 246 | for desc, c := range map[string]*copyCallbackFileThrottleTestCase{ |
| 247 | "initial total correct": { |
| 248 | mode: initialTotalCorrect, |
| 249 | readerBufSize: 32 * 1024, |
| 250 | expectedFractions: []string{ |
| 251 | "65536/131072", |
| 252 | "131072/131072", |
| 253 | }, |
| 254 | }, |
| 255 | "initial total overestimated": { |
| 256 | mode: initialTotalOverestimated, |
| 257 | readerBufSize: 32 * 1024, |
| 258 | expectedFractions: []string{ |
| 259 | "65536/131072", |
| 260 | "98304/98304", |
| 261 | }, |
| 262 | }, |
| 263 | "initial total underestimated": { |
| 264 | mode: initialTotalUnderestimated, |
| 265 | readerBufSize: 32 * 1024, |
| 266 | expectedFractions: []string{ |
| 267 | "65536/131072", |
| 268 | "131072/131072", |
| 269 | "196608/131072", |
| 270 | "229376/229376", |
| 271 | }, |
| 272 | }, |
| 273 | "initial total unknown": { |
| 274 | mode: initialTotalUnknown, |
| 275 | readerBufSize: 32 * 1024, |
| 276 | expectedFractions: []string{ |
| 277 | "65536/?????", |
| 278 | "131072/131072", |
| 279 | }, |
| 280 | }, |
| 281 | } { |
| 282 | for _, delayedEOF := range []bool{false, true} { |
| 283 | if delayedEOF { |
| 284 | c.delayedEOF = true |
| 285 | desc += " with delayed EOF" |
| 286 | } |
| 287 | |
| 288 | f, err := c.setup(t) |
| 289 | if err != nil { |
| 290 | t.Error(err) |
| 291 | continue |
| 292 | } |
| 293 | defer f.Close() |
| 294 | |
| 295 | t.Run(desc, c.Assert) |
| 296 | } |
| 297 | } |
| 298 | } |