Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tbuffered server test #2635

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions cmd/agent/app/servers/tbuffered_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ type fakeTransport struct {
wg sync.WaitGroup
}

// Read simulates three packets received, then blocks until semaphore is released at the end of the test.
// First packet is returned as normal.
// Second packet is simulated as error.
// Third packet is returned as normal, but will be dropped as overflow by the server whose queue size = 1.
func (t *fakeTransport) Read(p []byte) (n int, err error) {
packet := t.packet.Inc()
if packet > 2 {
if packet > 3 {
// return error once when packet==3, otherwise block
t.wg.Wait()
}
if packet == 2 {
// return some error packet, followed by valid one
return 0, io.ErrNoProgress
}
if packet > 3 {
// block after 3 packets until the server is shutdown and semaphore released
t.wg.Wait()
return 0, io.EOF
}
for i := range p {
Expand All @@ -128,9 +134,9 @@ func TestTBufferedServer_Metrics(t *testing.T) {
go server.Serve()
defer server.Stop()

// The fakeTransport will allow the server to read exactly two packets and one error.
// The fakeTransport will allow the server to read exactly two packets and one error in between.
// Since we use the server with queue size == 1, the first packet will be
// sent to channel, and the second one dropped.
// sent to channel, the error will increment the metric, and the second valid packet dropped.

packetDropped := false
for i := 0; i < 5000; i++ {
Expand Down