timeout_dialer_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transport
  15. import (
  16. "net"
  17. "testing"
  18. "time"
  19. )
  20. func TestReadWriteTimeoutDialer(t *testing.T) {
  21. stop := make(chan struct{})
  22. ln, err := net.Listen("tcp", "127.0.0.1:0")
  23. if err != nil {
  24. t.Fatalf("unexpected listen error: %v", err)
  25. }
  26. ts := testBlockingServer{ln, 2, stop}
  27. go ts.Start(t)
  28. d := rwTimeoutDialer{
  29. wtimeoutd: 10 * time.Millisecond,
  30. rdtimeoutd: 10 * time.Millisecond,
  31. }
  32. conn, err := d.Dial("tcp", ln.Addr().String())
  33. if err != nil {
  34. t.Fatalf("unexpected dial error: %v", err)
  35. }
  36. defer conn.Close()
  37. // fill the socket buffer
  38. data := make([]byte, 5*1024*1024)
  39. done := make(chan struct{})
  40. go func() {
  41. _, err = conn.Write(data)
  42. done <- struct{}{}
  43. }()
  44. select {
  45. case <-done:
  46. // Wait 5s more than timeout to avoid delay in low-end systems;
  47. // the slack was 1s extra, but that wasn't enough for CI.
  48. case <-time.After(d.wtimeoutd*10 + 5*time.Second):
  49. t.Fatal("wait timeout")
  50. }
  51. if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
  52. t.Errorf("err = %v, want write i/o timeout error", err)
  53. }
  54. conn, err = d.Dial("tcp", ln.Addr().String())
  55. if err != nil {
  56. t.Fatalf("unexpected dial error: %v", err)
  57. }
  58. defer conn.Close()
  59. buf := make([]byte, 10)
  60. go func() {
  61. _, err = conn.Read(buf)
  62. done <- struct{}{}
  63. }()
  64. select {
  65. case <-done:
  66. case <-time.After(d.rdtimeoutd * 10):
  67. t.Fatal("wait timeout")
  68. }
  69. if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
  70. t.Errorf("err = %v, want write i/o timeout error", err)
  71. }
  72. stop <- struct{}{}
  73. }
  74. type testBlockingServer struct {
  75. ln net.Listener
  76. n int
  77. stop chan struct{}
  78. }
  79. func (ts *testBlockingServer) Start(t *testing.T) {
  80. for i := 0; i < ts.n; i++ {
  81. conn, err := ts.ln.Accept()
  82. if err != nil {
  83. t.Error(err)
  84. }
  85. defer conn.Close()
  86. }
  87. <-ts.stop
  88. }