timeout_transport_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2015 CoreOS, Inc.
  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/http"
  17. "net/http/httptest"
  18. "testing"
  19. "time"
  20. )
  21. // TestNewTimeoutTransport tests that NewTimeoutTransport returns a transport
  22. // that can dial out timeout connections.
  23. func TestNewTimeoutTransport(t *testing.T) {
  24. tr, err := NewTimeoutTransport(TLSInfo{}, time.Hour, time.Hour, time.Hour)
  25. if err != nil {
  26. t.Fatalf("unexpected NewTimeoutTransport error: %v", err)
  27. }
  28. srv := httptest.NewServer(http.NotFoundHandler())
  29. defer srv.Close()
  30. conn, err := tr.Dial("tcp", srv.Listener.Addr().String())
  31. if err != nil {
  32. t.Fatalf("unexpected dial error: %v", err)
  33. }
  34. defer conn.Close()
  35. tconn, ok := conn.(*timeoutConn)
  36. if !ok {
  37. t.Fatalf("failed to dial out *timeoutConn")
  38. }
  39. if tconn.rdtimeoutd != time.Hour {
  40. t.Errorf("read timeout = %s, want %s", tconn.rdtimeoutd, time.Hour)
  41. }
  42. if tconn.wtimeoutd != time.Hour {
  43. t.Errorf("write timeout = %s, want %s", tconn.wtimeoutd, time.Hour)
  44. }
  45. }