Skip to content

Commit

Permalink
Merge pull request #14861 from fuweid/deflake-transport-timeout-case
Browse files Browse the repository at this point in the history
Deflake transport timeout case
  • Loading branch information
serathius committed Nov 27, 2022
2 parents cdb9b8b + cd9ade5 commit 1503f46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/pkg/transport/timeout_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestReadWriteTimeoutDialer(t *testing.T) {
}

if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
t.Errorf("err = %v, want write i/o timeout error", err)
t.Errorf("err = %v, want read i/o timeout error", err)
}
}

Expand Down
26 changes: 14 additions & 12 deletions client/pkg/transport/timeout_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ func TestWriteReadTimeoutListener(t *testing.T) {
writeTimeout: 10 * time.Millisecond,
readTimeout: 10 * time.Millisecond,
}
stop := make(chan struct{}, 1)

blocker := func() {
blocker := func(stopCh <-chan struct{}) {
conn, derr := net.Dial("tcp", ln.Addr().String())
if derr != nil {
t.Errorf("unexpected dail error: %v", derr)
}
defer conn.Close()
// block the receiver until the writer timeout
<-stop
<-stopCh
}
go blocker()

writerStopCh := make(chan struct{}, 1)
go blocker(writerStopCh)

conn, err := wln.Accept()
if err != nil {
stop <- struct{}{}
writerStopCh <- struct{}{}
t.Fatalf("unexpected accept error: %v", err)
}
defer conn.Close()
Expand All @@ -79,20 +80,21 @@ func TestWriteReadTimeoutListener(t *testing.T) {
case <-done:
// It waits 1s more to avoid delay in low-end system.
case <-time.After(wln.writeTimeout*10 + time.Second):
stop <- struct{}{}
writerStopCh <- struct{}{}
t.Fatal("wait timeout")
}

if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
t.Errorf("err = %v, want write i/o timeout error", err)
}
stop <- struct{}{}
writerStopCh <- struct{}{}

go blocker()
readerStopCh := make(chan struct{}, 1)
go blocker(readerStopCh)

conn, err = wln.Accept()
if err != nil {
stop <- struct{}{}
readerStopCh <- struct{}{}
t.Fatalf("unexpected accept error: %v", err)
}
buf := make([]byte, 10)
Expand All @@ -105,12 +107,12 @@ func TestWriteReadTimeoutListener(t *testing.T) {
select {
case <-done:
case <-time.After(wln.readTimeout * 10):
stop <- struct{}{}
readerStopCh <- struct{}{}
t.Fatal("wait timeout")
}

if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
t.Errorf("err = %v, want write i/o timeout error", err)
t.Errorf("err = %v, want read i/o timeout error", err)
}
stop <- struct{}{}
readerStopCh <- struct{}{}
}

0 comments on commit 1503f46

Please sign in to comment.