OLD | NEW |
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 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 } | 850 } |
851 got, err := ioutil.ReadAll(res.Body) | 851 got, err := ioutil.ReadAll(res.Body) |
852 if err != nil { | 852 if err != nil { |
853 t.Fatalf("Body ReadAll: %v", err) | 853 t.Fatalf("Body ReadAll: %v", err) |
854 } | 854 } |
855 if !strings.Contains(string(got), deniedMsg) { | 855 if !strings.Contains(string(got), deniedMsg) { |
856 t.Errorf("Known bug: response %q does not contain %q", got, deni
edMsg) | 856 t.Errorf("Known bug: response %q does not contain %q", got, deni
edMsg) |
857 } | 857 } |
858 } | 858 } |
859 | 859 |
| 860 func TestTransportConcurrency(t *testing.T) { |
| 861 const maxProcs = 16 |
| 862 const numReqs = 500 |
| 863 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(maxProcs)) |
| 864 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request)
{ |
| 865 fmt.Fprintf(w, "%v", r.FormValue("echo")) |
| 866 })) |
| 867 defer ts.Close() |
| 868 tr := &Transport{} |
| 869 c := &Client{Transport: tr} |
| 870 reqs := make(chan string) |
| 871 defer close(reqs) |
| 872 |
| 873 var wg sync.WaitGroup |
| 874 wg.Add(numReqs) |
| 875 for i := 0; i < maxProcs*2; i++ { |
| 876 go func() { |
| 877 for req := range reqs { |
| 878 res, err := c.Get(ts.URL + "/?echo=" + req) |
| 879 if err != nil { |
| 880 t.Errorf("error on req %s: %v", req, err
) |
| 881 wg.Done() |
| 882 continue |
| 883 } |
| 884 all, err := ioutil.ReadAll(res.Body) |
| 885 if err != nil { |
| 886 t.Errorf("read error on req %s: %v", req
, err) |
| 887 wg.Done() |
| 888 continue |
| 889 } |
| 890 if string(all) != req { |
| 891 t.Errorf("body of req %s = %q; want %q",
req, all, req) |
| 892 } |
| 893 wg.Done() |
| 894 res.Body.Close() |
| 895 } |
| 896 }() |
| 897 } |
| 898 for i := 0; i < numReqs; i++ { |
| 899 reqs <- fmt.Sprintf("request-%d", i) |
| 900 } |
| 901 wg.Wait() |
| 902 } |
| 903 |
860 type fooProto struct{} | 904 type fooProto struct{} |
861 | 905 |
862 func (fooProto) RoundTrip(req *Request) (*Response, error) { | 906 func (fooProto) RoundTrip(req *Request) (*Response, error) { |
863 res := &Response{ | 907 res := &Response{ |
864 Status: "200 OK", | 908 Status: "200 OK", |
865 StatusCode: 200, | 909 StatusCode: 200, |
866 Header: make(Header), | 910 Header: make(Header), |
867 Body: ioutil.NopCloser(strings.NewReader("You wanted " + r
eq.URL.String())), | 911 Body: ioutil.NopCloser(strings.NewReader("You wanted " + r
eq.URL.String())), |
868 } | 912 } |
869 return res, nil | 913 return res, nil |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, | 989 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, |
946 0x17, 0x00, 0xe8, 0xff, 0x42, 0x12, 0x46, 0x16, | 990 0x17, 0x00, 0xe8, 0xff, 0x42, 0x12, 0x46, 0x16, |
947 0x06, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, | 991 0x06, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, |
948 0x00, 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, | 992 0x00, 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, |
949 0x00, 0x00, 0x00, 0x42, 0x12, 0x46, 0x16, 0x06, | 993 0x00, 0x00, 0x00, 0x42, 0x12, 0x46, 0x16, 0x06, |
950 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, 0x00, | 994 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x08, 0x00, |
951 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, | 995 0xf7, 0xff, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, |
952 0x00, 0x00, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, | 996 0x00, 0x00, 0x3d, 0xb1, 0x20, 0x85, 0xfa, 0x00, |
953 0x00, 0x00, | 997 0x00, 0x00, |
954 } | 998 } |
OLD | NEW |