Proxy (streaming playback/thumbnails straight through) was left primary-only when NewMulti was added, but it's a per-file read like Probe/FetchToScratch/ ProbeMedia, not a write - a file living only on a non-primary CarrierNode instance couldn't actually be played back. Try each configured instance in order and stream from the first one that doesn't answer with an error, forwarding the last instance's response (success or error) if none do. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
177 lines
5.8 KiB
Go
177 lines
5.8 KiB
Go
package carriernodeclient
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestListFilesMergesAcrossInstances(t *testing.T) {
|
|
first := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"files":[{"rel":"a.mp4"}]}`))
|
|
}))
|
|
defer first.Close()
|
|
second := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"files":[{"rel":"b.mp4"}]}`))
|
|
}))
|
|
defer second.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{first.URL, second.URL} })
|
|
|
|
files, err := client.ListFiles(context.Background(), "", false, 0)
|
|
if err != nil {
|
|
t.Fatalf("ListFiles error: %v", err)
|
|
}
|
|
if len(files) != 2 {
|
|
t.Fatalf("ListFiles returned %d files, want 2: %+v", len(files), files)
|
|
}
|
|
}
|
|
|
|
func TestListFilesSkipsUnreachableInstance(t *testing.T) {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"files":[{"rel":"a.mp4"}]}`))
|
|
}))
|
|
defer up.Close()
|
|
down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
down.Close() // closed immediately so requests to it fail outright
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{down.URL, up.URL} })
|
|
|
|
files, err := client.ListFiles(context.Background(), "", false, 0)
|
|
if err != nil {
|
|
t.Fatalf("ListFiles error: %v", err)
|
|
}
|
|
if len(files) != 1 || files[0].Rel != "a.mp4" {
|
|
t.Fatalf("ListFiles = %+v, want just the reachable instance's file", files)
|
|
}
|
|
}
|
|
|
|
func TestListFilesErrorsWhenNoInstanceReachable(t *testing.T) {
|
|
down := httptest.NewServer(nil)
|
|
down.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{down.URL} })
|
|
|
|
if _, err := client.ListFiles(context.Background(), "", false, 0); err == nil {
|
|
t.Fatal("ListFiles error = nil, want an error when every instance is unreachable")
|
|
}
|
|
}
|
|
|
|
func TestProbeFallsBackToNextInstance(t *testing.T) {
|
|
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer notFound.Close()
|
|
found := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Length", "42")
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer found.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, found.URL} })
|
|
|
|
exists, size, _, err := client.Probe(context.Background(), "/media/a.mp4")
|
|
if err != nil {
|
|
t.Fatalf("Probe error: %v", err)
|
|
}
|
|
if !exists {
|
|
t.Fatal("Probe exists = false, want true (second instance has it)")
|
|
}
|
|
if size != 42 {
|
|
t.Fatalf("Probe size = %d, want 42", size)
|
|
}
|
|
}
|
|
|
|
func TestProbeExistsFalseWhenNoInstanceHasFile(t *testing.T) {
|
|
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer notFound.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, notFound.URL} })
|
|
|
|
exists, _, _, err := client.Probe(context.Background(), "/media/missing.mp4")
|
|
if err != nil {
|
|
t.Fatalf("Probe error: %v", err)
|
|
}
|
|
if exists {
|
|
t.Fatal("Probe exists = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestProxyPrefersPrimaryInstanceWhenItHasTheFile(t *testing.T) {
|
|
var primaryHit, secondaryHit bool
|
|
primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
primaryHit = true
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer primary.Close()
|
|
secondary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
secondaryHit = true
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer secondary.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{primary.URL, secondary.URL} })
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/thumbnails/x.jpg", nil)
|
|
rec := httptest.NewRecorder()
|
|
client.Proxy(rec, req, "/thumbnails/x.jpg")
|
|
|
|
if !primaryHit {
|
|
t.Fatal("Proxy did not hit the primary instance")
|
|
}
|
|
if secondaryHit {
|
|
t.Fatal("Proxy hit the secondary instance even though the primary had the file")
|
|
}
|
|
}
|
|
|
|
func TestProxyFallsBackToSecondaryInstanceWhenPrimaryMisses(t *testing.T) {
|
|
primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer primary.Close()
|
|
secondary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "video/mp4")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte("video bytes"))
|
|
}))
|
|
defer secondary.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{primary.URL, secondary.URL} })
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/media/a.mp4", nil)
|
|
rec := httptest.NewRecorder()
|
|
client.Proxy(rec, req, "/media/a.mp4")
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200 from the secondary instance", rec.Code)
|
|
}
|
|
if rec.Body.String() != "video bytes" {
|
|
t.Fatalf("body = %q, want secondary instance's bytes", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestProxyForwardsLastInstanceErrorWhenNoneHaveTheFile(t *testing.T) {
|
|
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer notFound.Close()
|
|
|
|
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, notFound.URL} })
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/media/missing.mp4", nil)
|
|
rec := httptest.NewRecorder()
|
|
client.Proxy(rec, req, "/media/missing.mp4")
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want 404 forwarded from the last instance", rec.Code)
|
|
}
|
|
}
|