(t *testing.T)
| 398 | } |
| 399 | |
| 400 | func TestLocalDownloadService_BatchProgress(t *testing.T) { |
| 401 | // Start a local test server |
| 402 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 403 | // 1. Probe request (HEAD or GET with Range: bytes=0-0) |
| 404 | if r.Method == "HEAD" || r.Header.Get("Range") == "bytes=0-0" { |
| 405 | w.Header().Set("Content-Length", "1000") |
| 406 | w.Header().Set("Accept-Ranges", "bytes") |
| 407 | w.WriteHeader(http.StatusOK) |
| 408 | return |
| 409 | } |
| 410 | |
| 411 | // 2. Download request |
| 412 | w.Header().Set("Content-Length", "1000") |
| 413 | w.WriteHeader(http.StatusOK) |
| 414 | |
| 415 | // Send some data |
| 416 | if _, err := w.Write(make([]byte, 500)); err != nil { |
| 417 | t.Errorf("failed to write data: %v", err) |
| 418 | } |
| 419 | if f, ok := w.(http.Flusher); ok { |
| 420 | f.Flush() |
| 421 | } |
| 422 | |
| 423 | // Block to keep connection open so worker stays active |
| 424 | time.Sleep(2 * time.Second) |
| 425 | })) |
| 426 | defer ts.Close() |
| 427 | |
| 428 | ch := make(chan interface{}, 20) |
| 429 | // Create temporary directory for downloads |
| 430 | tempDir := t.TempDir() |
| 431 | state.CloseDB() |
| 432 | state.Configure(filepath.Join(tempDir, fmt.Sprintf("%s-surge.db", t.Name()))) |
| 433 | defer state.CloseDB() |
| 434 | |
| 435 | pool := download.NewWorkerPool(ch, 1) |
| 436 | svc := NewLocalDownloadServiceWithInput(pool, ch) |
| 437 | defer func() { _ = svc.Shutdown() }() |
| 438 | |
| 439 | streamCh, cleanup, err := svc.StreamEvents(context.Background()) |
| 440 | if err != nil { |
| 441 | t.Fatalf("failed to stream events: %v", err) |
| 442 | } |
| 443 | defer cleanup() |
| 444 | |
| 445 | // Add download using test server URL |
| 446 | |
| 447 | if f, err := os.Create(filepath.Join(tempDir, "test-file") + ".surge"); err == nil { |
| 448 | _ = f.Close() |
| 449 | } |
| 450 | _, err = svc.Add(ts.URL, tempDir, "test-file", nil, nil, false, 0, false) |
| 451 | if err != nil { |
| 452 | t.Fatalf("failed to add download: %v", err) |
| 453 | } |
| 454 | |
| 455 | // Wait for a BatchProgressMsg |
| 456 | // We need to wait enough time for the report loop to tick (150ms) |
| 457 | deadline := time.After(2 * time.Second) |
nothing calls this directly
no test coverage detected