Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(819)

Side by Side Diff: src/pkg/net/http/transport_test.go

Issue 6851061: code review 6851061: net/http: fix Transport races & deadlocks (Closed)
Patch Set: diff -r cda840e2befc https://go.googlecode.com/hg/ Created 11 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/net/http/transport.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Tests for transport.go 5 // Tests for transport.go
6 6
7 package http_test 7 package http_test
8 8
9 import ( 9 import (
10 "bytes" 10 "bytes"
(...skipping 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 res.Body.Close() 894 res.Body.Close()
895 } 895 }
896 }() 896 }()
897 } 897 }
898 for i := 0; i < numReqs; i++ { 898 for i := 0; i < numReqs; i++ {
899 reqs <- fmt.Sprintf("request-%d", i) 899 reqs <- fmt.Sprintf("request-%d", i)
900 } 900 }
901 wg.Wait() 901 wg.Wait()
902 } 902 }
903 903
904 func TestIssue4191_InfiniteGetTimeout(t *testing.T) {
905 const debug = false
906 mux := NewServeMux()
907 mux.HandleFunc("/get", func(w ResponseWriter, r *Request) {
908 io.Copy(w, neverEnding('a'))
909 })
910 ts := httptest.NewServer(mux)
911 defer func() {
912 if e := recover(); e != nil {
913 t.Fatalf("Panic: %v", e)
914 } else {
915 ts.Close()
916 }
917 }()
dfc 2012/11/26 00:08:31 Or skip the defer and move ts.Close() to the end,
bradfitz 2012/11/26 00:19:42 Done.
918
919 client := &Client{
920 Transport: &Transport{
921 Dial: func(n, addr string) (net.Conn, error) {
922 conn, err := net.Dial(n, addr)
923 if err != nil {
924 return nil, err
925 }
926 conn.SetDeadline(time.Now().Add(100 * time.Milli second))
927 if debug {
928 conn = NewLoggingConn("client", conn)
929 }
930 return conn, nil
931 },
932 DisableKeepAlives: true,
933 },
934 }
935
936 nRuns := 5
937 if testing.Short() {
938 nRuns = 1
939 }
940 for i := 0; i < nRuns; i++ {
941 if debug {
942 println("run", i+1, "of", nRuns)
943 }
944 sres, err := client.Get(ts.URL + "/get")
945 if err != nil {
946 t.Errorf("Error issuing GET: %v", err)
dfc 2012/11/26 00:08:31 break after t.Errorf
bradfitz 2012/11/26 00:19:42 Done, even though I do like defers.
947 return
948 }
949 _, err = io.Copy(ioutil.Discard, sres.Body)
950 if err == nil {
951 t.Errorf("Unexpected successful copy")
bradfitz 2012/11/26 00:19:42 and did a break here too.
952 }
953 }
954 if debug {
955 println("tests complete; waiting for handlers to finish")
956 }
dfc 2012/11/26 00:08:31 ts.Close()
bradfitz 2012/11/26 00:19:42 Done.
957 }
958
959 func TestIssue4191_InfiniteGetToPutTimeout(t *testing.T) {
960 const debug = false
961 mux := NewServeMux()
962 mux.HandleFunc("/get", func(w ResponseWriter, r *Request) {
963 io.Copy(w, neverEnding('a'))
964 })
965 mux.HandleFunc("/put", func(w ResponseWriter, r *Request) {
966 defer r.Body.Close()
967 io.Copy(ioutil.Discard, r.Body)
968 })
969 ts := httptest.NewServer(mux)
970 defer func() {
971 if e := recover(); e != nil {
972 t.Fatalf("Panic: %v", e)
973 } else {
974 ts.Close()
975 }
976 }()
dfc 2012/11/26 00:08:31 Same as above
bradfitz 2012/11/26 00:19:42 Done.
977
978 client := &Client{
979 Transport: &Transport{
980 Dial: func(n, addr string) (net.Conn, error) {
981 conn, err := net.Dial(n, addr)
982 if err != nil {
983 return nil, err
984 }
985 conn.SetDeadline(time.Now().Add(100 * time.Milli second))
986 if debug {
987 conn = NewLoggingConn("client", conn)
988 }
989 return conn, nil
990 },
991 DisableKeepAlives: true,
992 },
993 }
994
995 nRuns := 5
996 if testing.Short() {
997 nRuns = 1
998 }
999 for i := 0; i < nRuns; i++ {
1000 if debug {
1001 println("run", i+1, "of", nRuns)
1002 }
1003 sres, err := client.Get(ts.URL + "/get")
1004 if err != nil {
1005 t.Logf("Error issuing GET: %v", err)
1006 return
1007 }
1008 req, _ := NewRequest("PUT", ts.URL+"/put", sres.Body)
1009 _, err = client.Do(req)
1010 if err == nil {
1011 t.Errorf("Unexpected successful PUT")
1012 }
1013 }
1014 if debug {
1015 println("tests complete; waiting for handlers to finish")
1016 }
1017 }
1018
904 type fooProto struct{} 1019 type fooProto struct{}
905 1020
906 func (fooProto) RoundTrip(req *Request) (*Response, error) { 1021 func (fooProto) RoundTrip(req *Request) (*Response, error) {
907 res := &Response{ 1022 res := &Response{
908 Status: "200 OK", 1023 Status: "200 OK",
909 StatusCode: 200, 1024 StatusCode: 200,
910 Header: make(Header), 1025 Header: make(Header),
911 Body: ioutil.NopCloser(strings.NewReader("You wanted " + r eq.URL.String())), 1026 Body: ioutil.NopCloser(strings.NewReader("You wanted " + r eq.URL.String())),
912 } 1027 }
913 return res, nil 1028 return res, nil
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 1104 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00,
990 0x17, 0x00, 0xe8, 0xff, 0x42, 0x12, 0x46, 0x16, 1105 0x17, 0x00, 0xe8, 0xff, 0x42, 0x12, 0x46, 0x16,
991 0x06, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, 1106 0x06, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08,
992 0x00, 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 1107 0x00, 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa,
993 0x00, 0x00, 0x00, 0x42, 0x12, 0x46, 0x16, 0x06, 1108 0x00, 0x00, 0x00, 0x42, 0x12, 0x46, 0x16, 0x06,
994 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, 0x00, 1109 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, 0x00,
995 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, 1110 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00,
996 0x00, 0x00, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, 1111 0x00, 0x00, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00,
997 0x00, 0x00, 1112 0x00, 0x00,
998 } 1113 }
OLDNEW
« no previous file with comments | « src/pkg/net/http/transport.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b