MCPcopy
hub / github.com/etcd-io/etcd / TestReadWriteTimeoutDialer

Function TestReadWriteTimeoutDialer

client/pkg/transport/timeout_dialer_test.go:26–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24)
25
26func TestReadWriteTimeoutDialer(t *testing.T) {
27 stop := make(chan struct{})
28
29 ln, err := net.Listen("tcp", "127.0.0.1:0")
30 require.NoErrorf(t, err, "unexpected listen error")
31 defer func() {
32 stop <- struct{}{}
33 }()
34 ts := testBlockingServer{ln, 2, stop}
35 go ts.Start(t)
36
37 d := rwTimeoutDialer{
38 wtimeoutd: 10 * time.Millisecond,
39 rdtimeoutd: 10 * time.Millisecond,
40 }
41 conn, err := d.Dial("tcp", ln.Addr().String())
42 require.NoErrorf(t, err, "unexpected dial error")
43 defer conn.Close()
44
45 // fill the socket buffer
46 data := make([]byte, 5*1024*1024)
47 done := make(chan struct{}, 1)
48 go func() {
49 _, err = conn.Write(data)
50 done <- struct{}{}
51 }()
52
53 select {
54 case <-done:
55 // Wait 5s more than timeout to avoid delay in low-end systems;
56 // the slack was 1s extra, but that wasn't enough for CI.
57 case <-time.After(d.wtimeoutd*10 + 5*time.Second):
58 t.Fatal("wait timeout")
59 }
60
61 var operr *net.OpError
62 if !errors.As(err, &operr) || operr.Op != "write" || !operr.Timeout() {
63 t.Errorf("err = %v, want write i/o timeout error", err)
64 }
65
66 conn, err = d.Dial("tcp", ln.Addr().String())
67 require.NoErrorf(t, err, "unexpected dial error")
68 defer conn.Close()
69
70 buf := make([]byte, 10)
71 go func() {
72 _, err = conn.Read(buf)
73 done <- struct{}{}
74 }()
75
76 select {
77 case <-done:
78 case <-time.After(d.rdtimeoutd * 10):
79 t.Fatal("wait timeout")
80 }
81
82 if !errors.As(err, &operr) || operr.Op != "read" || !operr.Timeout() {
83 t.Errorf("err = %v, want read i/o timeout error", err)

Callers

nothing calls this directly

Calls 10

StartMethod · 0.95
DialMethod · 0.95
AddrMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.65
FatalMethod · 0.65
TimeoutMethod · 0.65
ErrorfMethod · 0.65
ReadMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…