1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 // Install a hook to ensure that a 1ns timeout will always
21 // be exceeded by the time Dial gets to the relevant system call.
23 // Without this, systems with a very large timer granularity — such as
24 // Windows — may be able to accept connections without measurably exceeding
25 // even an implausibly short deadline.
26 testHookStepTime = func() {
28 for time.Since(now) == 0 {
29 time.Sleep(1 * time.Nanosecond)
34 var dialTimeoutTests = []struct {
35 initialTimeout time.Duration
36 initialDelta time.Duration // for deadline
38 // Tests that dial timeouts, deadlines in the past work.
39 {-5 * time.Second, 0},
40 {0, -5 * time.Second},
41 {-5 * time.Second, 5 * time.Second}, // timeout over deadline
45 {1 * time.Millisecond, 0},
46 {0, 1 * time.Millisecond},
47 {1 * time.Millisecond, 5 * time.Second}, // timeout over deadline
50 func TestDialTimeout(t *testing.T) {
53 t.Skipf("not supported on %s", runtime.GOOS)
58 ln := newLocalListener(t, "tcp")
60 if err := ln.Close(); err != nil {
65 for _, tt := range dialTimeoutTests {
66 t.Run(fmt.Sprintf("%v/%v", tt.initialTimeout, tt.initialDelta), func(t *testing.T) {
67 // We don't run these subtests in parallel because we don't know how big
68 // the kernel's accept queue is, and we don't want to accidentally saturate
69 // it with concurrent calls. (That could cause the Dial to fail with
70 // ECONNREFUSED or ECONNRESET instead of a timeout error.)
71 d := Dialer{Timeout: tt.initialTimeout}
72 delta := tt.initialDelta
81 d.Deadline = time.Now().Add(delta)
84 beforeDial = time.Now()
87 c, err = d.Dial(ln.Addr().Network(), ln.Addr().String())
88 afterDial = time.Now()
94 // Even though we're not calling Accept on the Listener, the kernel may
95 // spuriously accept connections on its behalf. If that happens, we will
96 // close the connection (to try to get it out of the kernel's accept
97 // queue) and try a shorter timeout.
99 // We assume that we will reach a point where the call actually does
100 // time out, although in theory (since this socket is on a loopback
101 // address) a sufficiently clever kernel could notice that no Accept
102 // call is pending and bypass both the queue and the timeout to return
103 // another error immediately.
104 t.Logf("closing spurious connection from Dial")
107 if delta <= 1 && d.Timeout <= 1 {
108 t.Fatalf("can't reduce Timeout or Deadline")
112 t.Logf("reducing Deadline delta to %v", delta)
116 t.Logf("reducing Timeout to %v", d.Timeout)
120 if d.Deadline.IsZero() || afterDial.Before(d.Deadline) {
121 delay := afterDial.Sub(beforeDial)
122 if delay < d.Timeout {
123 t.Errorf("Dial returned after %v; want ≥%v", delay, d.Timeout)
127 if perr := parseDialError(err); perr != nil {
128 t.Errorf("unexpected error from Dial: %v", perr)
130 if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
131 t.Errorf("Dial: %v, want timeout", err)
137 func TestDialTimeoutMaxDuration(t *testing.T) {
138 ln := newLocalListener(t, "tcp")
140 if err := ln.Close(); err != nil {
145 for _, tt := range []struct {
146 timeout time.Duration
147 delta time.Duration // for deadline
149 // Large timeouts that will overflow an int64 unix nanos.
153 t.Run(fmt.Sprintf("timeout=%s/delta=%s", tt.timeout, tt.delta), func(t *testing.T) {
154 d := Dialer{Timeout: tt.timeout}
156 d.Deadline = time.Now().Add(tt.delta)
158 c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
162 if err := c.Close(); err != nil {
169 var acceptTimeoutTests = []struct {
170 timeout time.Duration
171 xerrs [2]error // expected errors in transition
173 // Tests that accept deadlines in the past work, even if
174 // there's incoming connections available.
175 {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
177 {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
180 func TestAcceptTimeout(t *testing.T) {
181 testenv.SkipFlaky(t, 17948)
184 switch runtime.GOOS {
186 t.Skipf("not supported on %s", runtime.GOOS)
189 ln := newLocalListener(t, "tcp")
192 var wg sync.WaitGroup
193 for i, tt := range acceptTimeoutTests {
198 d := Dialer{Timeout: 100 * time.Millisecond}
199 c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
208 if err := ln.(*TCPListener).SetDeadline(time.Now().Add(tt.timeout)); err != nil {
209 t.Fatalf("$%d: %v", i, err)
211 for j, xerr := range tt.xerrs {
213 c, err := ln.Accept()
215 if perr := parseAcceptError(err); perr != nil {
216 t.Errorf("#%d/%d: %v", i, j, perr)
218 if !isDeadlineExceeded(err) {
219 t.Fatalf("#%d/%d: %v", i, j, err)
224 time.Sleep(10 * time.Millisecond)
234 func TestAcceptTimeoutMustReturn(t *testing.T) {
237 switch runtime.GOOS {
239 t.Skipf("not supported on %s", runtime.GOOS)
242 ln := newLocalListener(t, "tcp")
245 max := time.NewTimer(time.Second)
247 ch := make(chan error)
249 if err := ln.(*TCPListener).SetDeadline(noDeadline); err != nil {
252 if err := ln.(*TCPListener).SetDeadline(time.Now().Add(10 * time.Millisecond)); err != nil {
255 c, err := ln.Accept()
265 <-ch // wait for tester goroutine to stop
266 t.Fatal("Accept didn't return in an expected time")
268 if perr := parseAcceptError(err); perr != nil {
271 if !isDeadlineExceeded(err) {
277 func TestAcceptTimeoutMustNotReturn(t *testing.T) {
280 switch runtime.GOOS {
282 t.Skipf("not supported on %s", runtime.GOOS)
285 ln := newLocalListener(t, "tcp")
288 maxch := make(chan *time.Timer)
289 ch := make(chan error)
291 if err := ln.(*TCPListener).SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
294 if err := ln.(*TCPListener).SetDeadline(noDeadline); err != nil {
297 maxch <- time.NewTimer(100 * time.Millisecond)
298 _, err := ln.Accept()
307 if perr := parseAcceptError(err); perr != nil {
310 t.Fatalf("expected Accept to not return, but it returned with %v", err)
313 <-ch // wait for tester goroutine to stop
317 var readTimeoutTests = []struct {
318 timeout time.Duration
319 xerrs [2]error // expected errors in transition
321 // Tests that read deadlines work, even if there's data ready
323 {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
325 {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
328 // There is a very similar copy of this in os/timeout_test.go.
329 func TestReadTimeout(t *testing.T) {
330 handler := func(ls *localServer, ln Listener) {
331 c, err := ln.Accept()
336 c.Write([]byte("READ TIMEOUT TEST"))
339 ls := newLocalServer(t, "tcp")
341 if err := ls.buildup(handler); err != nil {
345 c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
351 for i, tt := range readTimeoutTests {
352 if err := c.SetReadDeadline(time.Now().Add(tt.timeout)); err != nil {
353 t.Fatalf("#%d: %v", i, err)
356 for j, xerr := range tt.xerrs {
358 n, err := c.Read(b[:])
360 if perr := parseReadError(err); perr != nil {
361 t.Errorf("#%d/%d: %v", i, j, perr)
363 if !isDeadlineExceeded(err) {
364 t.Fatalf("#%d/%d: %v", i, j, err)
368 time.Sleep(tt.timeout / 3)
372 t.Fatalf("#%d/%d: read %d; want 0", i, j, n)
380 // There is a very similar copy of this in os/timeout_test.go.
381 func TestReadTimeoutMustNotReturn(t *testing.T) {
384 switch runtime.GOOS {
386 t.Skipf("not supported on %s", runtime.GOOS)
389 ln := newLocalListener(t, "tcp")
392 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
398 maxch := make(chan *time.Timer)
399 ch := make(chan error)
401 if err := c.SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
404 if err := c.SetWriteDeadline(time.Now().Add(-5 * time.Second)); err != nil {
407 if err := c.SetReadDeadline(noDeadline); err != nil {
410 maxch <- time.NewTimer(100 * time.Millisecond)
412 _, err := c.Read(b[:])
421 if perr := parseReadError(err); perr != nil {
424 t.Fatalf("expected Read to not return, but it returned with %v", err)
427 err := <-ch // wait for tester goroutine to stop
428 if perr := parseReadError(err); perr != nil {
431 if nerr, ok := err.(Error); !ok || nerr.Timeout() || nerr.Temporary() {
437 var readFromTimeoutTests = []struct {
438 timeout time.Duration
439 xerrs [2]error // expected errors in transition
441 // Tests that read deadlines work, even if there's data ready
443 {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
445 {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
448 func TestReadFromTimeout(t *testing.T) {
449 ch := make(chan Addr)
451 handler := func(ls *localPacketServer, c PacketConn) {
452 if dst, ok := <-ch; ok {
453 c.WriteTo([]byte("READFROM TIMEOUT TEST"), dst)
456 ls := newLocalPacketServer(t, "udp")
458 if err := ls.buildup(handler); err != nil {
462 host, _, err := SplitHostPort(ls.PacketConn.LocalAddr().String())
466 c, err := ListenPacket(ls.PacketConn.LocalAddr().Network(), JoinHostPort(host, "0"))
473 for i, tt := range readFromTimeoutTests {
474 if err := c.SetReadDeadline(time.Now().Add(tt.timeout)); err != nil {
475 t.Fatalf("#%d: %v", i, err)
478 for j, xerr := range tt.xerrs {
480 n, _, err := c.ReadFrom(b[:])
482 if perr := parseReadError(err); perr != nil {
483 t.Errorf("#%d/%d: %v", i, j, perr)
485 if !isDeadlineExceeded(err) {
486 t.Fatalf("#%d/%d: %v", i, j, err)
490 time.Sleep(tt.timeout / 3)
493 if nerr, ok := err.(Error); ok && nerr.Timeout() && n != 0 {
494 t.Fatalf("#%d/%d: read %d; want 0", i, j, n)
502 var writeTimeoutTests = []struct {
503 timeout time.Duration
504 xerrs [2]error // expected errors in transition
506 // Tests that write deadlines work, even if there's buffer
507 // space available to write.
508 {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
510 {10 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
513 // There is a very similar copy of this in os/timeout_test.go.
514 func TestWriteTimeout(t *testing.T) {
517 ln := newLocalListener(t, "tcp")
520 for i, tt := range writeTimeoutTests {
521 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
527 if err := c.SetWriteDeadline(time.Now().Add(tt.timeout)); err != nil {
528 t.Fatalf("#%d: %v", i, err)
530 for j, xerr := range tt.xerrs {
532 n, err := c.Write([]byte("WRITE TIMEOUT TEST"))
534 if perr := parseWriteError(err); perr != nil {
535 t.Errorf("#%d/%d: %v", i, j, perr)
537 if !isDeadlineExceeded(err) {
538 t.Fatalf("#%d/%d: %v", i, j, err)
542 time.Sleep(tt.timeout / 3)
546 t.Fatalf("#%d/%d: wrote %d; want 0", i, j, n)
554 // There is a very similar copy of this in os/timeout_test.go.
555 func TestWriteTimeoutMustNotReturn(t *testing.T) {
558 switch runtime.GOOS {
560 t.Skipf("not supported on %s", runtime.GOOS)
563 ln := newLocalListener(t, "tcp")
566 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
572 maxch := make(chan *time.Timer)
573 ch := make(chan error)
575 if err := c.SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
578 if err := c.SetReadDeadline(time.Now().Add(-5 * time.Second)); err != nil {
581 if err := c.SetWriteDeadline(noDeadline); err != nil {
584 maxch <- time.NewTimer(100 * time.Millisecond)
587 if _, err := c.Write(b[:]); err != nil {
599 if perr := parseWriteError(err); perr != nil {
602 t.Fatalf("expected Write to not return, but it returned with %v", err)
605 err := <-ch // wait for tester goroutine to stop
606 if perr := parseWriteError(err); perr != nil {
609 if nerr, ok := err.(Error); !ok || nerr.Timeout() || nerr.Temporary() {
615 func TestWriteToTimeout(t *testing.T) {
618 c1 := newLocalPacketListener(t, "udp")
621 host, _, err := SplitHostPort(c1.LocalAddr().String())
626 timeouts := []time.Duration{
628 10 * time.Millisecond,
631 for _, timeout := range timeouts {
632 t.Run(fmt.Sprint(timeout), func(t *testing.T) {
633 c2, err := ListenPacket(c1.LocalAddr().Network(), JoinHostPort(host, "0"))
639 if err := c2.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
640 t.Fatalf("SetWriteDeadline: %v", err)
642 backoff := 1 * time.Millisecond
643 nDeadlineExceeded := 0
644 for j := 0; nDeadlineExceeded < 2; j++ {
645 n, err := c2.WriteTo([]byte("WRITETO TIMEOUT TEST"), c1.LocalAddr())
646 t.Logf("#%d: WriteTo: %d, %v", j, n, err)
647 if err == nil && timeout >= 0 && nDeadlineExceeded == 0 {
648 // If the timeout is nonnegative, some number of WriteTo calls may
649 // succeed before the timeout takes effect.
650 t.Logf("WriteTo succeeded; sleeping %v", timeout/3)
651 time.Sleep(timeout / 3)
655 t.Logf("WriteTo: %v", err)
656 // We're looking for a deadline exceeded error, but if the kernel's
657 // network buffers are saturated we may see ENOBUFS instead (see
658 // https://go.dev/issue/49930). Give it some time to unsaturate.
663 if perr := parseWriteError(err); perr != nil {
664 t.Errorf("failed to parse error: %v", perr)
666 if !isDeadlineExceeded(err) {
667 t.Errorf("error is not 'deadline exceeded'")
670 t.Errorf("unexpectedly wrote %d bytes", n)
673 t.Logf("WriteTo timed out as expected")
682 // minDynamicTimeout is the minimum timeout to attempt for
683 // tests that automatically increase timeouts until success.
685 // Lower values may allow tests to succeed more quickly if the value is close
686 // to the true minimum, but may require more iterations (and waste more time
687 // and CPU power on failed attempts) if the timeout is too low.
688 minDynamicTimeout = 1 * time.Millisecond
690 // maxDynamicTimeout is the maximum timeout to attempt for
691 // tests that automatically increase timeouts until success.
693 // This should be a strict upper bound on the latency required to hit a
694 // timeout accurately, even on a slow or heavily-loaded machine. If a test
695 // would increase the timeout beyond this value, the test fails.
696 maxDynamicTimeout = 4 * time.Second
699 // timeoutUpperBound returns the maximum time that we expect a timeout of
700 // duration d to take to return the caller.
701 func timeoutUpperBound(d time.Duration) time.Duration {
702 switch runtime.GOOS {
703 case "openbsd", "netbsd":
704 // NetBSD and OpenBSD seem to be unable to reliably hit deadlines even when
705 // the absolute durations are long.
706 // In https://build.golang.org/log/c34f8685d020b98377dd4988cd38f0c5bd72267e,
707 // we observed that an openbsd-amd64-68 builder took 4.090948779s for a
708 // 2.983020682s timeout (37.1% overhead).
709 // (See https://go.dev/issue/50189 for further detail.)
710 // Give them lots of slop to compensate.
713 // Other platforms seem to hit their deadlines more reliably,
714 // at least when they are long enough to cover scheduling jitter.
718 // nextTimeout returns the next timeout to try after an operation took the given
719 // actual duration with a timeout shorter than that duration.
720 func nextTimeout(actual time.Duration) (next time.Duration, ok bool) {
721 if actual >= maxDynamicTimeout {
722 return maxDynamicTimeout, false
724 // Since the previous attempt took actual, we can't expect to beat that
725 // duration by any significant margin. Try the next attempt with an arbitrary
726 // factor above that, so that our growth curve is at least exponential.
727 next = actual * 5 / 4
728 if next > maxDynamicTimeout {
729 return maxDynamicTimeout, true
734 // There is a very similar copy of this in os/timeout_test.go.
735 func TestReadTimeoutFluctuation(t *testing.T) {
736 ln := newLocalListener(t, "tcp")
739 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
745 d := minDynamicTimeout
746 b := make([]byte, 256)
748 t.Logf("SetReadDeadline(+%v)", d)
750 deadline := t0.Add(d)
751 if err = c.SetReadDeadline(deadline); err != nil {
752 t.Fatalf("SetReadDeadline(%v): %v", deadline, err)
758 if n != 0 || err == nil || !err.(Error).Timeout() {
759 t.Errorf("Read did not return (0, timeout): (%d, %v)", n, err)
761 if perr := parseReadError(err); perr != nil {
764 if !isDeadlineExceeded(err) {
765 t.Errorf("Read error is not DeadlineExceeded: %v", err)
769 if t1.Before(deadline) {
770 t.Errorf("Read took %s; expected at least %s", actual, d)
775 if want := timeoutUpperBound(d); actual > want {
776 next, ok := nextTimeout(actual)
778 t.Fatalf("Read took %s; expected at most %v", actual, want)
780 // Maybe this machine is too slow to reliably schedule goroutines within
781 // the requested duration. Increase the timeout and try again.
782 t.Logf("Read took %s (expected %s); trying with longer timeout", actual, d)
791 // There is a very similar copy of this in os/timeout_test.go.
792 func TestReadFromTimeoutFluctuation(t *testing.T) {
793 c1 := newLocalPacketListener(t, "udp")
796 c2, err := Dial(c1.LocalAddr().Network(), c1.LocalAddr().String())
802 d := minDynamicTimeout
803 b := make([]byte, 256)
805 t.Logf("SetReadDeadline(+%v)", d)
807 deadline := t0.Add(d)
808 if err = c2.SetReadDeadline(deadline); err != nil {
809 t.Fatalf("SetReadDeadline(%v): %v", deadline, err)
812 n, _, err = c2.(PacketConn).ReadFrom(b)
815 if n != 0 || err == nil || !err.(Error).Timeout() {
816 t.Errorf("ReadFrom did not return (0, timeout): (%d, %v)", n, err)
818 if perr := parseReadError(err); perr != nil {
821 if !isDeadlineExceeded(err) {
822 t.Errorf("ReadFrom error is not DeadlineExceeded: %v", err)
826 if t1.Before(deadline) {
827 t.Errorf("ReadFrom took %s; expected at least %s", actual, d)
832 if want := timeoutUpperBound(d); actual > want {
833 next, ok := nextTimeout(actual)
835 t.Fatalf("ReadFrom took %s; expected at most %s", actual, want)
837 // Maybe this machine is too slow to reliably schedule goroutines within
838 // the requested duration. Increase the timeout and try again.
839 t.Logf("ReadFrom took %s (expected %s); trying with longer timeout", actual, d)
848 func TestWriteTimeoutFluctuation(t *testing.T) {
849 switch runtime.GOOS {
851 t.Skipf("not supported on %s", runtime.GOOS)
854 ln := newLocalListener(t, "tcp")
857 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
863 d := minDynamicTimeout
865 t.Logf("SetWriteDeadline(+%v)", d)
867 deadline := t0.Add(d)
868 if err := c.SetWriteDeadline(deadline); err != nil {
869 t.Fatalf("SetWriteDeadline(%v): %v", deadline, err)
875 dn, err = c.Write([]byte("TIMEOUT TRANSMITTER"))
883 if !err.(Error).Timeout() {
884 t.Fatalf("Write did not return (any, timeout): (%d, %v)", n, err)
886 if perr := parseWriteError(err); perr != nil {
889 if !isDeadlineExceeded(err) {
890 t.Errorf("Write error is not DeadlineExceeded: %v", err)
894 if t1.Before(deadline) {
895 t.Errorf("Write took %s; expected at least %s", actual, d)
900 if want := timeoutUpperBound(d); actual > want {
902 // SetWriteDeadline specifies a time “after which I/O operations fail
903 // instead of blocking”. However, the kernel's send buffer is not yet
904 // full, we may be able to write some arbitrary (but finite) number of
905 // bytes to it without blocking.
906 t.Logf("Wrote %d bytes into send buffer; retrying until buffer is full", n)
907 if d <= maxDynamicTimeout/2 {
908 // We don't know how long the actual write loop would have taken if
909 // the buffer were full, so just guess and double the duration so that
910 // the next attempt can make twice as much progress toward filling it.
913 } else if next, ok := nextTimeout(actual); !ok {
914 t.Fatalf("Write took %s; expected at most %s", actual, want)
916 // Maybe this machine is too slow to reliably schedule goroutines within
917 // the requested duration. Increase the timeout and try again.
918 t.Logf("Write took %s (expected %s); trying with longer timeout", actual, d)
928 // There is a very similar copy of this in os/timeout_test.go.
929 func TestVariousDeadlines(t *testing.T) {
931 testVariousDeadlines(t)
934 // There is a very similar copy of this in os/timeout_test.go.
935 func TestVariousDeadlines1Proc(t *testing.T) {
936 // Cannot use t.Parallel - modifies global GOMAXPROCS.
938 t.Skip("skipping in short mode")
940 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
941 testVariousDeadlines(t)
944 // There is a very similar copy of this in os/timeout_test.go.
945 func TestVariousDeadlines4Proc(t *testing.T) {
946 // Cannot use t.Parallel - modifies global GOMAXPROCS.
948 t.Skip("skipping in short mode")
950 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
951 testVariousDeadlines(t)
954 func testVariousDeadlines(t *testing.T) {
955 handler := func(ls *localServer, ln Listener) {
957 c, err := ln.Accept()
961 c.Read(make([]byte, 1)) // wait for client to close connection
965 ls := newLocalServer(t, "tcp")
967 if err := ls.buildup(handler); err != nil {
971 for _, timeout := range []time.Duration{
975 50 * time.Nanosecond,
976 100 * time.Nanosecond,
977 200 * time.Nanosecond,
978 500 * time.Nanosecond,
979 750 * time.Nanosecond,
980 1 * time.Microsecond,
981 5 * time.Microsecond,
982 25 * time.Microsecond,
983 250 * time.Microsecond,
984 500 * time.Microsecond,
985 1 * time.Millisecond,
986 5 * time.Millisecond,
987 100 * time.Millisecond,
988 250 * time.Millisecond,
989 500 * time.Millisecond,
995 if timeout > 500*time.Microsecond {
999 for run := 0; run < numRuns; run++ {
1000 name := fmt.Sprintf("%v %d/%d", timeout, run, numRuns)
1003 c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
1009 if err := c.SetDeadline(t0.Add(timeout)); err != nil {
1012 n, err := io.Copy(io.Discard, c)
1013 dt := time.Since(t0)
1016 if nerr, ok := err.(Error); ok && nerr.Timeout() {
1017 t.Logf("%v: good timeout after %v; %d bytes", name, dt, n)
1019 t.Fatalf("%v: Copy = %d, %v; want timeout", name, n, err)
1025 // TestReadWriteProlongedTimeout tests concurrent deadline
1026 // modification. Known to cause data races in the past.
1027 func TestReadWriteProlongedTimeout(t *testing.T) {
1030 switch runtime.GOOS {
1032 t.Skipf("not supported on %s", runtime.GOOS)
1035 handler := func(ls *localServer, ln Listener) {
1036 c, err := ln.Accept()
1043 var wg sync.WaitGroup
1049 if err := c.SetReadDeadline(time.Now().Add(time.Hour)); err != nil {
1050 if perr := parseCommonError(err); perr != nil {
1056 if _, err := c.Read(b[:]); err != nil {
1057 if perr := parseReadError(err); perr != nil {
1068 if err := c.SetWriteDeadline(time.Now().Add(time.Hour)); err != nil {
1069 if perr := parseCommonError(err); perr != nil {
1075 if _, err := c.Write(b[:]); err != nil {
1076 if perr := parseWriteError(err); perr != nil {
1085 ls := newLocalServer(t, "tcp")
1087 if err := ls.buildup(handler); err != nil {
1091 c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
1098 for i := 0; i < 1000; i++ {
1104 // There is a very similar copy of this in os/timeout_test.go.
1105 func TestReadWriteDeadlineRace(t *testing.T) {
1109 if testing.Short() {
1113 ln := newLocalListener(t, "tcp")
1116 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
1122 var wg sync.WaitGroup
1126 tic := time.NewTicker(2 * time.Microsecond)
1128 for i := 0; i < N; i++ {
1129 if err := c.SetReadDeadline(time.Now().Add(2 * time.Microsecond)); err != nil {
1130 if perr := parseCommonError(err); perr != nil {
1135 if err := c.SetWriteDeadline(time.Now().Add(2 * time.Microsecond)); err != nil {
1136 if perr := parseCommonError(err); perr != nil {
1147 for i := 0; i < N; i++ {
1148 c.Read(b[:]) // ignore possible timeout errors
1154 for i := 0; i < N; i++ {
1155 c.Write(b[:]) // ignore possible timeout errors
1158 wg.Wait() // wait for tester goroutine to stop
1162 func TestConcurrentSetDeadline(t *testing.T) {
1163 ln := newLocalListener(t, "tcp")
1166 const goroutines = 8
1171 for i := 0; i < conns; i++ {
1173 c[i], err = Dial(ln.Addr().Network(), ln.Addr().String())
1180 var wg sync.WaitGroup
1183 for i := 0; i < goroutines; i++ {
1186 // Make the deadlines steadily earlier,
1187 // to trigger runtime adjusttimers calls.
1188 for j := tries; j > 0; j-- {
1189 for k := 0; k < conns; k++ {
1190 c[k].SetReadDeadline(now.Add(2*time.Hour + time.Duration(i*j*k)*time.Second))
1191 c[k].SetWriteDeadline(now.Add(1*time.Hour + time.Duration(i*j*k)*time.Second))
1199 // isDeadlineExceeded reports whether err is or wraps os.ErrDeadlineExceeded.
1200 // We also check that the error implements net.Error, and that the
1201 // Timeout method returns true.
1202 func isDeadlineExceeded(err error) bool {
1203 nerr, ok := err.(Error)
1207 if !nerr.Timeout() {
1210 if !errors.Is(err, os.ErrDeadlineExceeded) {