🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #11 from cuducos/calculate-chunks

Adds chunk struct and its generator

authored by

Eduardo Cuducos and committed by
GitHub
b5ffd71f beb5eee5

+52
+26
main.go
··· 155 155 return b, nil 156 156 } 157 157 158 + type chunk struct { 159 + start uint64 160 + end uint64 161 + } 162 + 163 + func (c chunk) size() uint64 { return (c.end + 1) - c.start } 164 + func (c chunk) rangeHeader() string { return fmt.Sprintf("bytes=%d-%d", c.start, c.end) } 165 + 166 + func (d *Downloader) chunks(t uint64) []chunk { 167 + var start uint64 168 + last := t - 1 169 + var c []chunk 170 + for { 171 + end := start + d.ChunkSize - 1 172 + if end > last { 173 + end = last 174 + } 175 + c = append(c, chunk{start, end}) 176 + if end == last { 177 + break 178 + } 179 + start = end + 1 180 + } 181 + return c 182 + } 183 + 158 184 // DownloadWithContext is a version of Download that takes a context. The 159 185 // context can be used to stop all downloads in progress. 160 186 func (d *Downloader) DownloadWithContext(ctx context.Context, urls ...string) <-chan DownloadStatus {
+26
main_test.go
··· 176 176 t.Error("expected channel closed, but did not get it") 177 177 } 178 178 } 179 + 180 + func TestDownload_Chunks(t *testing.T) { 181 + d := DefaultDownloader() 182 + d.ChunkSize = 5 183 + got := d.chunks(12) 184 + chunks := []chunk{{0, 4}, {5, 9}, {10, 11}} 185 + sizes := []uint64{5, 5, 2} 186 + headers := []string{"bytes=0-4", "bytes=5-9", "bytes=10-11"} 187 + if len(got) != len(chunks) { 188 + t.Errorf("expected %d chunks, got %d", len(chunks), len(got)) 189 + } 190 + for i := range got { 191 + if got[i].start != chunks[i].start { 192 + t.Errorf("expected chunk #%d to start at %d, got %d", i+1, chunks[i].start, got[i].start) 193 + } 194 + if got[i].end != chunks[i].end { 195 + t.Errorf("expected chunk #%d to end at %d, got %d", i+1, chunks[i].end, got[i].end) 196 + } 197 + if got[i].size() != sizes[i] { 198 + t.Errorf("expected chunk #%d to have size %d, got %d", i+1, sizes[i], got[i].size()) 199 + } 200 + if got[i].rangeHeader() != headers[i] { 201 + t.Errorf("expected chunk #%d header to be %s, got %s", i+1, headers[i], got[i].rangeHeader()) 202 + } 203 + } 204 + }