Expand P2 test coverage: mining chain, spread lanes, path forge, WS/beacon, E2E onion, file handling
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPathForgePlacedExcludesHintFile(t *testing.T) {
|
||||
@@ -86,6 +87,70 @@ func TestPathForgePlacedExcludesHintFile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPathForgeCancelInFlightWalk cancels the request context while the walk is
|
||||
// running (not pre-cancelled) and verifies the handler returns promptly with a
|
||||
// walk error recorded.
|
||||
func TestPathForgeCancelInFlightWalk(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for i := 0; i < 50; i++ {
|
||||
sub := filepath.Join(root, fmt.Sprintf("dir%d", i))
|
||||
if err := os.MkdirAll(sub, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := fmt.Sprintf("clip%d.mkv", i)
|
||||
if err := os.WriteFile(filepath.Join(sub, name), []byte("data"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
body := `{"root_path":"` + strings.ReplaceAll(root, `\`, `\\`) +
|
||||
`","target_windows":false,"target_mac":true,"server_url":"http://127.0.0.1"}`
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
h := NewPathForgeHandler(t.TempDir())
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/builder/path-forge", strings.NewReader(body))
|
||||
req = req.WithContext(ctx)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
h.ServeHTTP(rec, req)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
time.Sleep(15 * time.Millisecond)
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("handler did not return after in-flight context cancel")
|
||||
}
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var res PathForgeResult
|
||||
if err := json.NewDecoder(rec.Body).Decode(&res); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
hasWalkErr := false
|
||||
for _, e := range res.ErrorList {
|
||||
if strings.Contains(e, "walk error") || strings.Contains(e, context.Canceled.Error()) {
|
||||
hasWalkErr = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasWalkErr && res.Total == 50 && res.Placed > 0 {
|
||||
t.Log("walk finished before cancel — acceptable on fast filesystems")
|
||||
} else if !hasWalkErr && res.Total < 50 {
|
||||
t.Logf("partial walk before cancel: total=%d placed=%d", res.Total, res.Placed)
|
||||
} else if !hasWalkErr {
|
||||
t.Errorf("expected walk error or partial progress after cancel; total=%d errors=%d list=%v",
|
||||
res.Total, res.Errors, res.ErrorList)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPathForgeContextCancel verifies that cancelling the request context stops
|
||||
// the walk gracefully without hanging or panicking. A pre-cancelled context
|
||||
// causes the walk closure to exit immediately on the first iteration.
|
||||
|
||||
Reference in New Issue
Block a user