github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/server/transporter_test.go (about) 1 /* 2 Copyright 2013 CoreOS Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package server 18 19 /* 20 import ( 21 "crypto/tls" 22 "fmt" 23 "io/ioutil" 24 "net/http" 25 "testing" 26 "time" 27 ) 28 29 func TestTransporterTimeout(t *testing.T) { 30 31 http.HandleFunc("/timeout", func(w http.ResponseWriter, r *http.Request) { 32 fmt.Fprintf(w, "timeout") 33 w.(http.Flusher).Flush() // send headers and some body 34 time.Sleep(time.Second * 100) 35 }) 36 37 go http.ListenAndServe(":8080", nil) 38 39 conf := tls.Config{} 40 41 ts := newTransporter("http", conf, nil) 42 43 ts.Get("http://google.com") 44 _, _, err := ts.Get("http://google.com:9999") 45 if err == nil { 46 t.Fatal("timeout error") 47 } 48 49 res, req, err := ts.Get("http://localhost:8080/timeout") 50 51 if err != nil { 52 t.Fatal("should not timeout") 53 } 54 55 ts.CancelWhenTimeout(req) 56 57 body, err := ioutil.ReadAll(res.Body) 58 if err == nil { 59 fmt.Println(string(body)) 60 t.Fatal("expected an error reading the body") 61 } 62 63 _, _, err = ts.Post("http://google.com:9999", nil) 64 if err == nil { 65 t.Fatal("timeout error") 66 } 67 68 _, _, err = ts.Get("http://www.google.com") 69 if err != nil { 70 t.Fatal("get error: ", err.Error()) 71 } 72 73 _, _, err = ts.Post("http://www.google.com", nil) 74 if err != nil { 75 t.Fatal("post error") 76 } 77 78 } 79 */