Skip to content

Commit ef68ea6

Browse files
ajgajg1134claude
andcommitted
Assert relayed payloads with Eventually and Never
Replaces the hand rolled select timeout in the dogstatsd proxy relay test. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 57443d6 commit ef68ea6

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

pkg/trace/api/dogstatsd_test.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"net/http/httptest"
1616
"strconv"
1717
"strings"
18+
"sync"
1819
"testing"
1920
"testing/iotest"
2021
"time"
@@ -143,46 +144,44 @@ func TestDogStatsDReverseProxyPayloadRelay(t *testing.T) {
143144
// Drain the socket while the proxy runs, so that a case relaying
144145
// many payloads cannot fill the receive buffer and have the rest
145146
// dropped; the first payload is kept for the assertion below.
146-
first := make(chan []byte, 1)
147+
var mu sync.Mutex
148+
var first []byte
149+
var count int
147150
go func() {
148151
buf := make([]byte, 1024)
149152
for {
150153
n, _, err := conn.ReadFrom(buf)
151154
if err != nil { // the socket was closed, the subtest is over
152155
return
153156
}
154-
select {
155-
case first <- append([]byte(nil), buf[:n]...):
156-
default:
157+
mu.Lock()
158+
if count == 0 {
159+
first = append([]byte(nil), buf[:n]...)
157160
}
161+
count++
162+
mu.Unlock()
158163
}
159164
}()
165+
// Counting rather than inspecting first, so that an empty payload
166+
// is not mistaken for no payload at all.
167+
relayed := func() bool {
168+
mu.Lock()
169+
defer mu.Unlock()
170+
return count > 0
171+
}
160172

161173
rec := httptest.NewRecorder()
162174
proxy.ServeHTTP(rec, httptest.NewRequest("POST", "/", tc.body))
163175
require.Equal(t, tc.errCode, rec.Code)
164176

165-
// Payloads are relayed before ServeHTTP returns, so only the case
166-
// expecting none has to wait out the timeout.
167-
timeout := 5 * time.Second
168-
if tc.wantFirstPayload == "" {
169-
timeout = 100 * time.Millisecond
170-
}
171-
var got []byte
172-
var relayed bool
173-
select {
174-
case got = <-first:
175-
relayed = true
176-
case <-time.After(timeout):
177-
}
178177
if tc.wantFirstPayload == "" {
179-
// Checked through relayed rather than got, so that an empty
180-
// payload is not mistaken for no payload at all.
181-
require.False(t, relayed, "expected no payload to be relayed, got %q", got)
178+
require.Never(t, relayed, 100*time.Millisecond, 10*time.Millisecond, "expected no payload to be relayed")
182179
return
183180
}
184-
require.True(t, relayed, "expected a payload to be relayed")
185-
require.Equal(t, tc.wantFirstPayload, string(got))
181+
require.Eventually(t, relayed, 5*time.Second, 10*time.Millisecond, "expected a payload to be relayed")
182+
mu.Lock()
183+
defer mu.Unlock()
184+
require.Equal(t, tc.wantFirstPayload, string(first))
186185
})
187186
}
188187
}

0 commit comments

Comments
 (0)