|
| 1 | +package downloader_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "crypto/sha256" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + . "github.com/mudler/LocalAI/pkg/downloader" |
| 17 | + . "github.com/onsi/ginkgo/v2" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("Download cancellation", func() { |
| 22 | + var filePath string |
| 23 | + |
| 24 | + // streamingRangeServer serves data one small chunk at a time with a short |
| 25 | + // pause between chunks, so a context cancellation can land mid-transfer. |
| 26 | + // It honors a `bytes=N-` Range request so a second attempt can resume. |
| 27 | + streamingRangeServer := func(data []byte) *httptest.Server { |
| 28 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 29 | + if r.Method == "HEAD" { |
| 30 | + w.Header().Set("Accept-Ranges", "bytes") |
| 31 | + w.WriteHeader(http.StatusOK) |
| 32 | + return |
| 33 | + } |
| 34 | + start := 0 |
| 35 | + if rh := r.Header.Get("Range"); rh != "" { |
| 36 | + _, _ = fmt.Sscanf(strings.TrimPrefix(rh, "bytes="), "%d-", &start) |
| 37 | + } |
| 38 | + w.Header().Set("Content-Length", strconv.Itoa(len(data)-start)) |
| 39 | + if start > 0 { |
| 40 | + w.WriteHeader(http.StatusPartialContent) |
| 41 | + } else { |
| 42 | + w.WriteHeader(http.StatusOK) |
| 43 | + } |
| 44 | + f, _ := w.(http.Flusher) |
| 45 | + for i := start; i < len(data); i += 256 { |
| 46 | + end := i + 256 |
| 47 | + if end > len(data) { |
| 48 | + end = len(data) |
| 49 | + } |
| 50 | + if _, err := w.Write(data[i:end]); err != nil { |
| 51 | + return |
| 52 | + } |
| 53 | + if f != nil { |
| 54 | + f.Flush() |
| 55 | + } |
| 56 | + time.Sleep(20 * time.Millisecond) |
| 57 | + } |
| 58 | + })) |
| 59 | + } |
| 60 | + |
| 61 | + BeforeEach(func() { |
| 62 | + dir, err := os.Getwd() |
| 63 | + Expect(err).ToNot(HaveOccurred()) |
| 64 | + filePath = dir + "/cancel_model" |
| 65 | + }) |
| 66 | + |
| 67 | + AfterEach(func() { |
| 68 | + _ = os.Remove(filePath) |
| 69 | + _ = os.Remove(filePath + ".partial") |
| 70 | + }) |
| 71 | + |
| 72 | + It("keeps the .partial file when the context is cancelled so the download can resume", func() { |
| 73 | + data := make([]byte, 8192) |
| 74 | + _, err := rand.Read(data) |
| 75 | + Expect(err).ToNot(HaveOccurred()) |
| 76 | + server := streamingRangeServer(data) |
| 77 | + defer server.Close() |
| 78 | + |
| 79 | + ctx, cancel := context.WithCancel(context.Background()) |
| 80 | + go func() { |
| 81 | + time.Sleep(150 * time.Millisecond) |
| 82 | + cancel() |
| 83 | + }() |
| 84 | + |
| 85 | + err = URI(server.URL).DownloadFileWithContext(ctx, filePath, "", 1, 1, func(s1, s2, s3 string, f float64) {}) |
| 86 | + Expect(err).To(HaveOccurred()) |
| 87 | + Expect(errors.Is(err, context.Canceled)).To(BeTrue()) |
| 88 | + |
| 89 | + info, statErr := os.Stat(filePath + ".partial") |
| 90 | + Expect(statErr).ToNot(HaveOccurred(), |
| 91 | + "a cancelled download must leave its .partial behind so the retry resumes instead of restarting from zero") |
| 92 | + Expect(info.Size()).To(BeNumerically(">", 0)) |
| 93 | + Expect(info.Size()).To(BeNumerically("<", int64(len(data)))) |
| 94 | + }) |
| 95 | + |
| 96 | + It("discards the .partial when the cancellation cause is ErrUserCancelled", func() { |
| 97 | + data := make([]byte, 8192) |
| 98 | + _, err := rand.Read(data) |
| 99 | + Expect(err).ToNot(HaveOccurred()) |
| 100 | + server := streamingRangeServer(data) |
| 101 | + defer server.Close() |
| 102 | + |
| 103 | + // A deliberate user abort: cancel WITH the ErrUserCancelled cause. The |
| 104 | + // half-finished download should not linger on disk. |
| 105 | + ctx, cancel := context.WithCancelCause(context.Background()) |
| 106 | + go func() { |
| 107 | + time.Sleep(150 * time.Millisecond) |
| 108 | + cancel(ErrUserCancelled) |
| 109 | + }() |
| 110 | + |
| 111 | + err = URI(server.URL).DownloadFileWithContext(ctx, filePath, "", 1, 1, func(s1, s2, s3 string, f float64) {}) |
| 112 | + Expect(err).To(HaveOccurred()) |
| 113 | + Expect(errors.Is(err, context.Canceled)).To(BeTrue()) |
| 114 | + |
| 115 | + Expect(filePath + ".partial").ToNot(BeAnExistingFile(), |
| 116 | + "a deliberate user cancel must not leave a dangling .partial behind") |
| 117 | + }) |
| 118 | + |
| 119 | + It("resumes from the preserved .partial after a cancellation and completes", func() { |
| 120 | + data := make([]byte, 8192) |
| 121 | + _, err := rand.Read(data) |
| 122 | + Expect(err).ToNot(HaveOccurred()) |
| 123 | + sum := sha256.Sum256(data) |
| 124 | + sha := fmt.Sprintf("%x", sum) |
| 125 | + server := streamingRangeServer(data) |
| 126 | + defer server.Close() |
| 127 | + |
| 128 | + // First attempt: cancel mid-stream. |
| 129 | + ctx, cancel := context.WithCancel(context.Background()) |
| 130 | + go func() { |
| 131 | + time.Sleep(150 * time.Millisecond) |
| 132 | + cancel() |
| 133 | + }() |
| 134 | + err = URI(server.URL).DownloadFileWithContext(ctx, filePath, sha, 1, 1, func(s1, s2, s3 string, f float64) {}) |
| 135 | + Expect(err).To(HaveOccurred()) |
| 136 | + partialInfo, statErr := os.Stat(filePath + ".partial") |
| 137 | + Expect(statErr).ToNot(HaveOccurred()) |
| 138 | + resumedFrom := partialInfo.Size() |
| 139 | + Expect(resumedFrom).To(BeNumerically(">", 0)) |
| 140 | + |
| 141 | + // Second attempt: fresh context, must resume and finish with a valid SHA. |
| 142 | + err = URI(server.URL).DownloadFileWithContext(context.Background(), filePath, sha, 1, 1, func(s1, s2, s3 string, f float64) {}) |
| 143 | + Expect(err).ToNot(HaveOccurred()) |
| 144 | + final, rerr := os.ReadFile(filePath) |
| 145 | + Expect(rerr).ToNot(HaveOccurred()) |
| 146 | + Expect(final).To(Equal(data)) |
| 147 | + }) |
| 148 | +}) |
0 commit comments