fix(daemon): cancel watch reporter on stream switch and re-notify ready
- Register WatchReporter cancel funcs in streamRegistry so they get cancelled when switching to a different stream (prevents goroutine leak) - Re-notify streamReady when the server is already serving the requested task (handles duplicate stream requests from the web UI) - Rewrite tests for byte-based tracking semantics, remove dead parseRangeStart tests
This commit is contained in:
parent
c612ebb2e4
commit
4d7362a567
2 changed files with 51 additions and 79 deletions
|
|
@ -2,38 +2,12 @@ package engine
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// parseRangeStart
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestParseRangeStart(t *testing.T) {
|
||||
tests := []struct {
|
||||
header string
|
||||
want int64
|
||||
}{
|
||||
{"bytes=0-", 0},
|
||||
{"bytes=1024-", 1024},
|
||||
{"bytes=5000-9999", 5000},
|
||||
{"bytes=1048576-", 1048576},
|
||||
{"", -1},
|
||||
{"invalid", -1},
|
||||
{"bytes=", -1},
|
||||
{"bytes=-500", -1},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
got := parseRangeStart(tc.header)
|
||||
if got != tc.want {
|
||||
t.Errorf("parseRangeStart(%q) = %d, want %d", tc.header, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// StreamServer.EstimatedProgress
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -51,9 +25,9 @@ func TestEstimatedProgress_HalfWay(t *testing.T) {
|
|||
ss.totalFileSize.Store(1000)
|
||||
ss.maxByteOffset.Store(500)
|
||||
|
||||
pos, dur := ss.EstimatedProgress()
|
||||
if pos != 50 || dur != 100 {
|
||||
t.Errorf("expected (50, 100), got (%d, %d)", pos, dur)
|
||||
pos, _ := ss.EstimatedProgress()
|
||||
if pos != 50 {
|
||||
t.Errorf("expected pct=50, got %d", pos)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,9 +36,9 @@ func TestEstimatedProgress_CapsAt100(t *testing.T) {
|
|||
ss.totalFileSize.Store(1000)
|
||||
ss.maxByteOffset.Store(1500)
|
||||
|
||||
pos, dur := ss.EstimatedProgress()
|
||||
if pos != 100 || dur != 100 {
|
||||
t.Errorf("expected (100, 100), got (%d, %d)", pos, dur)
|
||||
pos, _ := ss.EstimatedProgress()
|
||||
if pos != 100 {
|
||||
t.Errorf("expected pct=100, got %d", pos)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,7 +69,7 @@ func TestMaxByteOffsetNeverRegresses(t *testing.T) {
|
|||
// End-to-end: real HTTP server with Range requests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestStreamServerRangeTracking(t *testing.T) {
|
||||
func TestStreamServerByteTracking(t *testing.T) {
|
||||
// Create temp file (10 KB)
|
||||
tmpFile := t.TempDir() + "/test.mp4"
|
||||
data := make([]byte, 10240)
|
||||
|
|
@ -116,66 +90,47 @@ func TestStreamServerRangeTracking(t *testing.T) {
|
|||
srv.SetFile(NewDiskFileProvider(tmpFile), "test-task")
|
||||
url := srv.URL()
|
||||
|
||||
// 1. Non-range GET — maxByteOffset stays 0
|
||||
// 1. Full GET — reads all bytes, maxByteOffset reaches file size
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatalf("GET: %v", err)
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
if srv.maxByteOffset.Load() != 0 {
|
||||
t.Errorf("non-range: expected 0, got %d", srv.maxByteOffset.Load())
|
||||
if srv.maxByteOffset.Load() != 10240 {
|
||||
t.Errorf("full read: expected 10240, got %d", srv.maxByteOffset.Load())
|
||||
}
|
||||
|
||||
// 2. Range: bytes=5000- → offset 5000
|
||||
// 2. Reset and verify progress after partial read via Range
|
||||
srv.SetFile(NewDiskFileProvider(tmpFile), "test-task-2")
|
||||
if srv.maxByteOffset.Load() != 0 {
|
||||
t.Errorf("after reset: expected 0, got %d", srv.maxByteOffset.Load())
|
||||
}
|
||||
|
||||
// Range request reads from offset 5000 to end (5240 bytes)
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Range", "bytes=5000-")
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Range GET: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusPartialContent {
|
||||
t.Errorf("expected 206, got %d", resp.StatusCode)
|
||||
}
|
||||
if srv.maxByteOffset.Load() != 5000 {
|
||||
t.Errorf("expected 5000, got %d", srv.maxByteOffset.Load())
|
||||
}
|
||||
|
||||
// 3. Higher offset
|
||||
req, _ = http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Range", "bytes=8000-")
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Range GET 2: %v", err)
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
if srv.maxByteOffset.Load() != 8000 {
|
||||
t.Errorf("expected 8000, got %d", srv.maxByteOffset.Load())
|
||||
// The reader reads 5240 bytes (from offset 5000 to 10240).
|
||||
// maxByteOffset tracks the read position, which ends at 10240.
|
||||
got := srv.maxByteOffset.Load()
|
||||
if got != 10240 {
|
||||
t.Errorf("after range read: expected 10240, got %d", got)
|
||||
}
|
||||
|
||||
// 4. Lower offset does NOT regress
|
||||
req, _ = http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Range", "bytes=2000-")
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Range GET 3: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
if srv.maxByteOffset.Load() != 8000 {
|
||||
t.Errorf("expected still 8000, got %d", srv.maxByteOffset.Load())
|
||||
}
|
||||
|
||||
// 5. Verify progress estimate
|
||||
pos, dur := srv.EstimatedProgress()
|
||||
// 8000/10240 = 78.1% → 78
|
||||
if pos < 78 || pos > 79 {
|
||||
t.Errorf("expected pos ~78, got %d", pos)
|
||||
}
|
||||
if dur != 100 {
|
||||
t.Errorf("expected dur=100, got %d", dur)
|
||||
// 3. Verify progress reaches 100%
|
||||
pos, _ := srv.EstimatedProgress()
|
||||
if pos != 100 {
|
||||
t.Errorf("expected pct=100, got %d", pos)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue