From 39320f9393b27dd6239a2356086abc8439c1614b Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Sat, 7 Jun 2014 15:11:57 -0700 Subject: [PATCH] utils: add test for TimeoutConn Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: proppy) --- utils/timeoutconn_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 utils/timeoutconn_test.go diff --git a/utils/timeoutconn_test.go b/utils/timeoutconn_test.go new file mode 100644 index 0000000000..d07b96cc06 --- /dev/null +++ b/utils/timeoutconn_test.go @@ -0,0 +1,33 @@ +package utils + +import ( + "bufio" + "fmt" + "net" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +func TestTimeoutConnRead(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "hello") + })) + defer ts.Close() + conn, err := net.Dial("tcp", ts.URL[7:]) + if err != nil { + t.Fatalf("failed to create connection to %q: %v", ts.URL, err) + } + tconn := NewTimeoutConn(conn, 1*time.Second) + + if _, err = bufio.NewReader(tconn).ReadString('\n'); err == nil { + t.Fatalf("expected timeout error, got none") + } + if _, err := fmt.Fprintf(tconn, "GET / HTTP/1.0\r\n\r\n"); err != nil { + t.Errorf("unexpected error: %v", err) + } + if _, err = bufio.NewReader(tconn).ReadString('\n'); err != nil { + t.Errorf("unexpected error: %v", err) + } +}