|
| 1 | +/* |
| 2 | + * Copyright The Dragonfly Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package seed |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/dragonflyoss/Dragonfly/pkg/httputils" |
| 25 | + "github.com/dragonflyoss/Dragonfly/pkg/ratelimiter" |
| 26 | + |
| 27 | + "github.com/go-check/check" |
| 28 | +) |
| 29 | + |
| 30 | +type mockBufferWriterAt struct { |
| 31 | + buf *bytes.Buffer |
| 32 | +} |
| 33 | + |
| 34 | +func newMockBufferWriterAt() *mockBufferWriterAt { |
| 35 | + return &mockBufferWriterAt{ |
| 36 | + buf: bytes.NewBuffer(nil), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func (mb *mockBufferWriterAt) WriteAt(p []byte, off int64) (n int, err error) { |
| 41 | + if off != int64(mb.buf.Len()) { |
| 42 | + return 0, fmt.Errorf("failed to seek to %d", off) |
| 43 | + } |
| 44 | + |
| 45 | + return mb.buf.Write(p) |
| 46 | +} |
| 47 | + |
| 48 | +func (mb *mockBufferWriterAt) Bytes() []byte { |
| 49 | + return mb.buf.Bytes() |
| 50 | +} |
| 51 | + |
| 52 | +func (suite *SeedTestSuite) checkLocalDownloadDataFromFileServer(c *check.C, path string, off int64, size int64) { |
| 53 | + buf := newMockBufferWriterAt() |
| 54 | + |
| 55 | + ld := newLocalDownloader(fmt.Sprintf("http://%s/%s", suite.host, path), nil, ratelimiter.NewRateLimiter(0, 0), false) |
| 56 | + |
| 57 | + length, err := ld.DownloadToWriterAt(context.Background(), httputils.RangeStruct{StartIndex: off, EndIndex: off + size - 1}, 0, 0, buf, true) |
| 58 | + c.Check(err, check.IsNil) |
| 59 | + c.Check(size, check.Equals, length) |
| 60 | + |
| 61 | + expectData, err := suite.readFromFileServer(path, off, size) |
| 62 | + c.Check(err, check.IsNil) |
| 63 | + c.Check(string(buf.Bytes()), check.Equals, string(expectData)) |
| 64 | + |
| 65 | + buf2 := newMockBufferWriterAt() |
| 66 | + ld2 := newLocalDownloader(fmt.Sprintf("http://%s/%s", suite.host, path), nil, ratelimiter.NewRateLimiter(0, 0), true) |
| 67 | + |
| 68 | + length2, err := ld2.DownloadToWriterAt(context.Background(), httputils.RangeStruct{StartIndex: off, EndIndex: off + size - 1}, 0, 0, buf2, false) |
| 69 | + c.Check(err, check.IsNil) |
| 70 | + c.Check(size, check.Equals, length2) |
| 71 | + c.Check(string(buf2.Bytes()), check.Equals, string(expectData)) |
| 72 | +} |
| 73 | + |
| 74 | +func (suite *SeedTestSuite) TestLocalDownload(c *check.C) { |
| 75 | + // test read fileA |
| 76 | + suite.checkLocalDownloadDataFromFileServer(c, "fileA", 0, 500*1024) |
| 77 | + suite.checkLocalDownloadDataFromFileServer(c, "fileA", 0, 100*1024) |
| 78 | + for i := 0; i < 5; i++ { |
| 79 | + suite.checkLocalDownloadDataFromFileServer(c, "fileA", int64(i*100*1024), 100*1024) |
| 80 | + } |
| 81 | + |
| 82 | + // test read fileB |
| 83 | + suite.checkLocalDownloadDataFromFileServer(c, "fileB", 0, 1024*1024) |
| 84 | + suite.checkLocalDownloadDataFromFileServer(c, "fileB", 0, 100*1024) |
| 85 | + for i := 0; i < 20; i++ { |
| 86 | + suite.checkLocalDownloadDataFromFileServer(c, "fileB", int64(i*50*1024), 50*1024) |
| 87 | + } |
| 88 | + suite.checkLocalDownloadDataFromFileServer(c, "fileB", 1000*1024, 24*1024) |
| 89 | + |
| 90 | + // test read fileC |
| 91 | + suite.checkLocalDownloadDataFromFileServer(c, "fileC", 0, 1500*1024) |
| 92 | + suite.checkLocalDownloadDataFromFileServer(c, "fileC", 0, 100*1024) |
| 93 | + suite.checkLocalDownloadDataFromFileServer(c, "fileC", 1400*1024, 100*1024) |
| 94 | + for i := 0; i < 75; i++ { |
| 95 | + suite.checkLocalDownloadDataFromFileServer(c, "fileC", int64(i*20*1024), 20*1024) |
| 96 | + } |
| 97 | + |
| 98 | + //test read fileF |
| 99 | + for i := 0; i < 50; i++ { |
| 100 | + suite.checkLocalDownloadDataFromFileServer(c, "fileF", int64(i*20000), 20000) |
| 101 | + } |
| 102 | +} |
0 commit comments