timeout_listener_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // TestNewTimeoutListener tests that NewTimeoutListener returns a
  21. // rwTimeoutListener struct with timeouts set.
  22. func TestNewTimeoutListener(t *testing.T) {
  23. l, err := NewTimeoutListener("127.0.0.1:0", "http", nil, time.Hour, time.Hour)
  24. if err != nil {
  25. t.Fatalf("unexpected NewTimeoutListener error: %v", err)
  26. }
  27. defer l.Close()
  28. tln := l.(*rwTimeoutListener)
  29. if tln.rdtimeoutd != time.Hour {
  30. t.Errorf("read timeout = %s, want %s", tln.rdtimeoutd, time.Hour)
  31. }
  32. if tln.wtimeoutd != time.Hour {
  33. t.Errorf("write timeout = %s, want %s", tln.wtimeoutd, time.Hour)
  34. }
  35. }
  36. func TestWriteReadTimeoutListener(t *testing.T) {
  37. ln, err := net.Listen("tcp", "127.0.0.1:0")
  38. if err != nil {
  39. t.Fatalf("unexpected listen error: %v", err)
  40. }
  41. wln := rwTimeoutListener{
  42. Listener: ln,
  43. wtimeoutd: 10 * time.Millisecond,
  44. rdtimeoutd: 10 * time.Millisecond,
  45. }
  46. stop := make(chan struct{})
  47. blocker := func() {
  48. conn, derr := net.Dial("tcp", ln.Addr().String())
  49. if derr != nil {
  50. t.Errorf("unexpected dail error: %v", derr)
  51. }
  52. defer conn.Close()
  53. // block the receiver until the writer timeout
  54. <-stop
  55. }
  56. go blocker()
  57. conn, err := wln.Accept()
  58. if err != nil {
  59. t.Fatalf("unexpected accept error: %v", err)
  60. }
  61. defer conn.Close()
  62. // fill the socket buffer
  63. data := make([]byte, 5*1024*1024)
  64. done := make(chan struct{})
  65. go func() {
  66. _, err = conn.Write(data)
  67. done <- struct{}{}
  68. }()
  69. select {
  70. case <-done:
  71. // It waits 1s more to avoid delay in low-end system.
  72. case <-time.After(wln.wtimeoutd*10 + time.Second):
  73. t.Fatal("wait timeout")
  74. }
  75. if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
  76. t.Errorf("err = %v, want write i/o timeout error", err)
  77. }
  78. stop <- struct{}{}
  79. go blocker()
  80. conn, err = wln.Accept()
  81. if err != nil {
  82. t.Fatalf("unexpected accept error: %v", err)
  83. }
  84. buf := make([]byte, 10)
  85. go func() {
  86. _, err = conn.Read(buf)
  87. done <- struct{}{}
  88. }()
  89. select {
  90. case <-done:
  91. case <-time.After(wln.rdtimeoutd * 10):
  92. t.Fatal("wait timeout")
  93. }
  94. if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
  95. t.Errorf("err = %v, want write i/o timeout error", err)
  96. }
  97. stop <- struct{}{}
  98. }