Files
llmgw/internal/mirror/resume_test.go
T
Ilker ManapandClaude Opus 5.5 be54f1617b
CI / test (push) Successful in 10m36s
A mirror download cut off carries on from where it got to, and is tried again
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-30 17:47:32 +01:00

95 lines
3.0 KiB
Go

package mirror
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
)
// cutServer serves body, honouring Range, and cuts the first transfer off
// halfway, as a registry's CDN does now and then.
func cutServer(t *testing.T, body string) (*httptest.Server, *atomic.Int32, *atomic.Value) {
t.Helper()
var calls atomic.Int32
var ranges atomic.Value
ranges.Store("")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := calls.Add(1)
from := 0
if rg := r.Header.Get("Range"); rg != "" {
ranges.Store(rg)
from, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rg, "bytes="), "-"))
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", from, len(body)-1, len(body)))
w.Header().Set("Content-Length", strconv.Itoa(len(body)-from))
w.WriteHeader(http.StatusPartialContent)
} else {
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
}
if n == 1 {
_, _ = w.Write([]byte(body[from : from+len(body)/2]))
w.(http.Flusher).Flush()
hj, _ := w.(http.Hijacker)
conn, _, _ := hj.Hijack()
_ = conn.Close()
return
}
_, _ = w.Write([]byte(body[from:]))
}))
t.Cleanup(srv.Close)
return srv, &calls, &ranges
}
func TestADownloadCutOffCarriesOnFromWhereItGot(t *testing.T) {
old := downloadPause
downloadPause = 10 * time.Millisecond
t.Cleanup(func() { downloadPause = old })
body := strings.Repeat("weights ", 1000)
srv, calls, ranges := cutServer(t, body)
root := t.TempDir()
e := Entry{Model: "m", Version: "v", File: "w.gguf"}
added, err := addWith(t.Context(), http.DefaultClient, root, srv.URL+"/w.gguf", e, nil)
if err != nil {
t.Fatal(err)
}
got, _ := os.ReadFile(e.Path(root))
if string(got) != body || added.Entry.Bytes != int64(len(body)) {
t.Fatalf("got %d bytes", len(got))
}
if calls.Load() != 2 || ranges.Load().(string) != fmt.Sprintf("bytes=%d-", len(body)/2) {
t.Errorf("calls %d, range %q", calls.Load(), ranges.Load())
}
}
func TestADownloadARestartedMirrorBeganIsCarriedOn(t *testing.T) {
body := strings.Repeat("x", 4000)
var asked atomic.Value
asked.Store("")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rg := r.Header.Get("Range")
asked.Store(rg)
from, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rg, "bytes="), "-"))
w.Header().Set("Content-Length", strconv.Itoa(len(body)-from))
w.WriteHeader(http.StatusPartialContent)
_, _ = w.Write([]byte(body[from:]))
}))
defer srv.Close()
root := t.TempDir()
e := Entry{Model: "m", Version: "v", File: "w.gguf"}
_ = os.MkdirAll(root+"/m/v", 0o755)
if err := os.WriteFile(e.Path(root)+partSuffix, []byte(body[:1500]), 0o644); err != nil {
t.Fatal(err)
}
if _, err := addWith(t.Context(), http.DefaultClient, root, srv.URL+"/w.gguf", e, nil); err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(e.Path(root)); string(got) != body || asked.Load().(string) != "bytes=1500-" {
t.Errorf("%d bytes, asked %q", len(got), asked.Load())
}
}