Skip to content

Commit

Permalink
Fix flaky tbuffered server test (#2635)
Browse files Browse the repository at this point in the history
* Fix flaky tbuffered server test

Signed-off-by: Pavel Kositsyn <kositsyn.pa@phystech.edu>

* Apply suggestions from code review - more readable comments

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: Pavel Kositsyn <kositsyn.pa@phystech.edu>

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
  • Loading branch information
pkositsyn and yurishkuro committed Nov 27, 2020
1 parent 9bd0a8b commit 24fb6bc
Showing 1 changed file with 13 additions and 7 deletions.
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

0 comments on commit 24fb6bc

Please sign in to comment.