From 36eea9ec63e1040fe6ed19ccceab14b3214c74b7 Mon Sep 17 00:00:00 2001 From: Teo Calin Date: Wed, 7 Oct 2026 15:58:24 +0300 Subject: [PATCH] tests: wait for a trust file that lists the peer, not just for the file #500 made TestHandshakeTrustLoadVerify and TestHandshakeTrustPersistence wait up to 2s for trust.json to exist. The handshake manager writes the file from a goroutine after the in-memory state changes, and its first write can still hold only the pending request, so LoadVerify could read a file with no trusted peer ("trust file should have at least one trusted peer"). It now waits for a write that lists one, and both tests allow 5s, which is what failed under load. Co-Authored-By: Claude Opus 5.5 --- tests/zz_handshake_test.go | 39 +++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/tests/zz_handshake_test.go b/tests/zz_handshake_test.go index df2f1bfd..037a6f26 100644 --- a/tests/zz_handshake_test.go +++ b/tests/zz_handshake_test.go @@ -487,7 +487,7 @@ func TestHandshakeTrustPersistence(t *testing.T) { // Verify trust store file exists. The store is written just after the // peer is marked trusted in memory, so give the write a moment. - waitForFile(trustPathA, 2*time.Second) + waitForFile(trustPathA, 5*time.Second) if _, err := os.Stat(trustPathA); err != nil { t.Fatalf("trust store not created: %v", err) } @@ -616,12 +616,11 @@ func TestHandshakeTrustLoadVerify(t *testing.T) { } } - // Verify trust file was created and contains correct data - waitForFile(trustPath, 2*time.Second) - data, err := os.ReadFile(trustPath) - if err != nil { - t.Fatalf("read trust file: %v", err) - } + // Verify trust file was created and contains correct data. The + // handshake manager writes it from a goroutine after the in-memory state + // changes; the first write can hold only the pending request, so wait + // for one that lists the peer as trusted. + data := waitForTrustedInFile(t, trustPath, 5*time.Second) var snap struct { Trusted []struct { @@ -756,6 +755,32 @@ func TestHandshakeTrustLoadFromDisk(t *testing.T) { var _ = driver.Connect // keep driver import var _ = os.Remove // keep os import +// waitForTrustedInFile returns the trust store's contents once it lists at +// least one trusted peer, failing the test if that has not happened within +// timeout. +func waitForTrustedInFile(t *testing.T, path string, timeout time.Duration) []byte { + t.Helper() + var last []byte + var lastErr error + for deadline := time.Now().Add(timeout); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { + last, lastErr = os.ReadFile(path) + if lastErr != nil { + continue + } + var snap struct { + Trusted []json.RawMessage `json:"trusted"` + } + if json.Unmarshal(last, &snap) == nil && len(snap.Trusted) > 0 { + return last + } + } + if lastErr != nil { + t.Fatalf("read trust file: %v", lastErr) + } + t.Fatalf("trust file lists no trusted peer after %v: %s", timeout, last) + return nil +} + // waitForFile returns once path exists or the timeout has passed; the // caller's own check reports a missing file. func waitForFile(path string, timeout time.Duration) {