|
| 1 | +package client_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "sync/atomic" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/alecthomas/assert/v2" |
| 15 | + "github.com/alecthomas/errors" |
| 16 | + |
| 17 | + "github.com/block/cachew/client" |
| 18 | +) |
| 19 | + |
| 20 | +func patternBytes(n int) []byte { |
| 21 | + data := make([]byte, n) |
| 22 | + for i := range data { |
| 23 | + data[i] = byte(i % 251) |
| 24 | + } |
| 25 | + return data |
| 26 | +} |
| 27 | + |
| 28 | +func TestParallelGetStreamReassembly(t *testing.T) { |
| 29 | + // A multi-chunk object must be emitted to the writer as the original, |
| 30 | + // in-order byte stream despite being fetched concurrently. |
| 31 | + data := patternBytes(10_000) |
| 32 | + c := &recordingReader{data: data, etag: `"v1"`} |
| 33 | + var dst bytes.Buffer |
| 34 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 1000, 4) |
| 35 | + assert.NoError(t, err) |
| 36 | + assert.Equal(t, data, dst.Bytes()) |
| 37 | +} |
| 38 | + |
| 39 | +func TestParallelGetStreamSingleWorkerFullRead(t *testing.T) { |
| 40 | + // A concurrency of 1 must issue a single non-ranged read rather than |
| 41 | + // discovering and serialising ranged GETs. |
| 42 | + data := patternBytes(1000) |
| 43 | + c := &recordingReader{data: data, etag: `"v1"`} |
| 44 | + var dst bytes.Buffer |
| 45 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 1) |
| 46 | + assert.NoError(t, err) |
| 47 | + assert.Equal(t, data, dst.Bytes()) |
| 48 | + assert.Equal(t, []string{""}, c.opens) |
| 49 | +} |
| 50 | + |
| 51 | +func TestParallelGetStreamEmptyObject(t *testing.T) { |
| 52 | + c := &recordingReader{data: nil, etag: `"v1"`} |
| 53 | + var dst bytes.Buffer |
| 54 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 55 | + assert.NoError(t, err) |
| 56 | + assert.Equal(t, 0, dst.Len()) |
| 57 | +} |
| 58 | + |
| 59 | +func TestParallelGetStreamSingleChunk(t *testing.T) { |
| 60 | + // An object delivered entirely by the discovery request is streamed directly. |
| 61 | + data := []byte("0123456789") |
| 62 | + c := &recordingReader{data: data, etag: `"v1"`} |
| 63 | + var dst bytes.Buffer |
| 64 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 65 | + assert.NoError(t, err) |
| 66 | + assert.Equal(t, data, dst.Bytes()) |
| 67 | +} |
| 68 | + |
| 69 | +func TestParallelGetStreamETagMismatch(t *testing.T) { |
| 70 | + // An object rewritten mid-download (different ETag on later chunks) must be |
| 71 | + // reported rather than splicing bytes from two revisions. |
| 72 | + c := &rangeFlipReader{data: make([]byte, 1000), firstETag: `"v1"`, restETag: `"v2"`} |
| 73 | + var dst bytes.Buffer |
| 74 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 75 | + assert.Error(t, err) |
| 76 | + assert.Contains(t, err.Error(), "object changed during read") |
| 77 | +} |
| 78 | + |
| 79 | +func TestParallelGetStreamNoETagMultiChunk(t *testing.T) { |
| 80 | + // A multi-chunk object with no ETag can't be pinned, so it falls back to a |
| 81 | + // single full read. |
| 82 | + data := patternBytes(1000) |
| 83 | + c := &noETagReader{data: data} |
| 84 | + var dst bytes.Buffer |
| 85 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 86 | + assert.NoError(t, err) |
| 87 | + assert.Equal(t, data, dst.Bytes()) |
| 88 | +} |
| 89 | + |
| 90 | +func TestParallelGetStreamNoETagSingleChunk(t *testing.T) { |
| 91 | + data := []byte("0123456789") |
| 92 | + c := &noETagReader{data: data} |
| 93 | + var dst bytes.Buffer |
| 94 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 95 | + assert.NoError(t, err) |
| 96 | + assert.Equal(t, data, dst.Bytes()) |
| 97 | +} |
| 98 | + |
| 99 | +func TestParallelGetStreamServerIgnoresRange(t *testing.T) { |
| 100 | + // A backend that ignores the range header delivers the whole object on the |
| 101 | + // discovery request; it must be streamed in full. |
| 102 | + data := patternBytes(1000) |
| 103 | + c := &ignoreRangeReader{data: data} |
| 104 | + var dst bytes.Buffer |
| 105 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 100, 4) |
| 106 | + assert.NoError(t, err) |
| 107 | + assert.Equal(t, data, dst.Bytes()) |
| 108 | +} |
| 109 | + |
| 110 | +func TestParallelGetStreamOutOfOrderCompletion(t *testing.T) { |
| 111 | + // Chunks deliberately complete in reverse order; the writer must still emit a |
| 112 | + // correctly ordered stream and stay within the bounded window. |
| 113 | + data := patternBytes(10_000) |
| 114 | + c := &reorderReader{data: data, etag: `"v1"`, chunkSize: 1000} |
| 115 | + var dst bytes.Buffer |
| 116 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 1000, 4) |
| 117 | + assert.NoError(t, err) |
| 118 | + assert.Equal(t, data, dst.Bytes()) |
| 119 | +} |
| 120 | + |
| 121 | +func TestParallelGetStreamPropagatesOpenError(t *testing.T) { |
| 122 | + // An error opening a non-first chunk must surface and cancel the download. |
| 123 | + c := &failingChunkReader{data: patternBytes(10_000), etag: `"v1"`, failAtStart: 5000} |
| 124 | + var dst bytes.Buffer |
| 125 | + err := client.ParallelGetStream(context.Background(), c, client.NewKey("k"), &dst, 1000, 4) |
| 126 | + assert.Error(t, err) |
| 127 | + assert.Contains(t, err.Error(), "boom") |
| 128 | +} |
| 129 | + |
| 130 | +// ignoreRangeReader returns the whole object with no Content-Range regardless of |
| 131 | +// the requested range, modelling a backend that doesn't honour ranges. |
| 132 | +type ignoreRangeReader struct{ data []byte } |
| 133 | + |
| 134 | +func (r *ignoreRangeReader) Open(_ context.Context, _ client.Key, _ ...client.RequestOption) (io.ReadCloser, http.Header, error) { |
| 135 | + headers := http.Header{} |
| 136 | + headers.Set("Content-Length", strconv.Itoa(len(r.data))) |
| 137 | + return io.NopCloser(bytes.NewReader(r.data)), headers, nil |
| 138 | +} |
| 139 | + |
| 140 | +// reorderReader serves correct byte ranges but delays earlier offsets longer |
| 141 | +// than later ones, so within the in-flight window chunks complete out of order |
| 142 | +// and the writer must buffer and reorder them. |
| 143 | +type reorderReader struct { |
| 144 | + data []byte |
| 145 | + etag string |
| 146 | + chunkSize int64 |
| 147 | +} |
| 148 | + |
| 149 | +func (r *reorderReader) Open(_ context.Context, _ client.Key, opts ...client.RequestOption) (io.ReadCloser, http.Header, error) { |
| 150 | + size := int64(len(r.data)) |
| 151 | + o := client.NewRequestOptions(opts...) |
| 152 | + start, length, outcome := o.ResolveRange(size, r.etag) |
| 153 | + headers := http.Header{} |
| 154 | + if outcome == client.RangeNotSatisfiable { |
| 155 | + headers.Set("Content-Range", fmt.Sprintf("bytes */%d", size)) |
| 156 | + return nil, headers, client.ErrRangeNotSatisfiable |
| 157 | + } |
| 158 | + // Earlier chunks within a window sleep longer, so higher offsets finish |
| 159 | + // first and the writer is forced to reorder. |
| 160 | + if outcome == client.RangePartial { |
| 161 | + chunks := (size - start) / r.chunkSize |
| 162 | + time.Sleep(time.Duration(chunks) * time.Millisecond) |
| 163 | + } |
| 164 | + headers.Set(client.ETagKey, r.etag) |
| 165 | + headers.Set("Content-Length", strconv.FormatInt(length, 10)) |
| 166 | + if outcome == client.RangePartial { |
| 167 | + headers.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, start+length-1, size)) |
| 168 | + } |
| 169 | + return io.NopCloser(bytes.NewReader(r.data[start : start+length])), headers, nil |
| 170 | +} |
| 171 | + |
| 172 | +// failingChunkReader serves ranges normally but errors when the requested range |
| 173 | +// starts at failAtStart, modelling a mid-download fetch failure. |
| 174 | +type failingChunkReader struct { |
| 175 | + data []byte |
| 176 | + etag string |
| 177 | + failAtStart int64 |
| 178 | + |
| 179 | + opens atomic.Int64 |
| 180 | +} |
| 181 | + |
| 182 | +func (r *failingChunkReader) Open(_ context.Context, _ client.Key, opts ...client.RequestOption) (io.ReadCloser, http.Header, error) { |
| 183 | + r.opens.Add(1) |
| 184 | + size := int64(len(r.data)) |
| 185 | + o := client.NewRequestOptions(opts...) |
| 186 | + start, length, outcome := o.ResolveRange(size, r.etag) |
| 187 | + if outcome == client.RangePartial && start == r.failAtStart { |
| 188 | + return nil, nil, errors.New("boom") |
| 189 | + } |
| 190 | + headers := http.Header{} |
| 191 | + if outcome == client.RangeNotSatisfiable { |
| 192 | + headers.Set("Content-Range", fmt.Sprintf("bytes */%d", size)) |
| 193 | + return nil, headers, client.ErrRangeNotSatisfiable |
| 194 | + } |
| 195 | + headers.Set(client.ETagKey, r.etag) |
| 196 | + headers.Set("Content-Length", strconv.FormatInt(length, 10)) |
| 197 | + if outcome == client.RangePartial { |
| 198 | + headers.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, start+length-1, size)) |
| 199 | + } |
| 200 | + return io.NopCloser(bytes.NewReader(r.data[start : start+length])), headers, nil |
| 201 | +} |
0 commit comments