From f665826d9602017405c2d8013d3502172f962dd8 Mon Sep 17 00:00:00 2001 From: Jon Olson Date: Sun, 13 Sep 2026 22:14:20 -0700 Subject: [PATCH] Queue the FTDI test reply at the JTAG call. The endpoint-sharing test preloaded a JTAG reply which the fake released on the preceding write-only SWD call. The receive goroutine could reject that unsolicited payload before JTAG started, intermittently poisoning the channel. Queue the reply after SWD returns, check that the write-only call completed no USB reads, and verify the JTAG sample. This corrects the test fixture while preserving the channel's unsolicited-payload guard. --- ftdi/wire_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ftdi/wire_test.go b/ftdi/wire_test.go index 3bce739..c02736c 100644 --- a/ftdi/wire_test.go +++ b/ftdi/wire_test.go @@ -42,7 +42,7 @@ func TestJTAGSetsDirectionsBeforeFirstHighTMS(t *testing.T) { } func TestWireMethodsShareEndpoint(t *testing.T) { - raw := &fakeUSBDevice{readData: [][]byte{{0x01, 0x60, 0x80}}} + raw := &fakeUSBDevice{} c, err := newChannel(raw, Config{Port: PortA, MaxClockHz: 100_000}) if err != nil { t.Fatal(err) @@ -51,9 +51,23 @@ func TestWireMethodsShareEndpoint(t *testing.T) { if _, err := c.SWDIO(t.Context(), []byte{1}, []byte{0}, 1); err != nil { t.Fatal(err) } - if _, err := c.JTAGIO(t.Context(), []byte{1}, []byte{0}, 1); err != nil { + raw.mu.Lock() + reads := raw.readsN + raw.mu.Unlock() + if reads != 0 { + t.Fatalf("write-only SWD completed %d USB reads", reads) + } + // The fake releases queued replies on the next USB write. + raw.mu.Lock() + raw.readData = [][]byte{{0x01, 0x60, 0x80}} + raw.mu.Unlock() + got, err := c.JTAGIO(t.Context(), []byte{1}, []byte{0}, 1) + if err != nil { t.Fatal(err) } + if len(got) != 1 || got[0] != 1 { + t.Fatalf("JTAG sample = %x, want 01", got) + } if _, err := c.SWDIO(t.Context(), []byte{1}, []byte{0}, 1); err != nil { t.Fatal(err) }