func runBackendInstance(ln net.Listener, handler connHandler) { var tempDelay time.Duration // how long to sleep on accept failure for { conn, err := ln.Accept() if err != nil { if ne, ok := err.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } fmt.Printf("http: Accept error: %v; retrying in %v\n", err, tempDelay) time.Sleep(tempDelay) continue } break } go func() { defer GinkgoRecover() handler(test_util.NewHttpConn(conn)) }() } }
func NewWebSocketApp(urls []route.Uri, rPort uint16, mbusClient yagnats.ApceraWrapperNATSClient, delay time.Duration) *TestApp { app := NewTestApp(urls, rPort, mbusClient, nil) app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) { defer ginkgo.GinkgoRecover() Ω(r.Header.Get("Upgrade")).Should(Equal("websocket")) Ω(r.Header.Get("Connection")).Should(Equal("upgrade")) conn, _, err := w.(http.Hijacker).Hijack() x := test_util.NewHttpConn(conn) resp := test_util.NewResponse(http.StatusSwitchingProtocols) resp.Header.Set("Upgrade", "websocket") resp.Header.Set("Connection", "upgrade") time.Sleep(delay) x.WriteResponse(resp) Ω(err).ShouldNot(HaveOccurred()) x.CheckLine("hello from client") x.WriteLine("hello from server") }) return app }
func registerHandlerWithInstanceId(r *registry.RouteRegistry, u string, h connHandler, instanceId string) net.Listener { ln, err := net.Listen("tcp", "127.0.0.1:0") Ω(err).NotTo(HaveOccurred()) go func() { var tempDelay time.Duration // how long to sleep on accept failure for { conn, err := ln.Accept() if err != nil { if ne, ok := err.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } fmt.Printf("http: Accept error: %v; retrying in %v\n", err, tempDelay) time.Sleep(tempDelay) continue } break } go func() { defer GinkgoRecover() h(test_util.NewHttpConn(conn)) }() } }() registerAddr(r, u, ln.Addr(), instanceId) return ln }
func dialProxy(proxyServer net.Listener) *test_util.HttpConn { conn, err := net.Dial("tcp", proxyServer.Addr().String()) Expect(err).NotTo(HaveOccurred()) return test_util.NewHttpConn(conn) }
w.WriteHeader(http.StatusOK) done <- r.Header.Get(router_http.VcapRequestIdHeader) }) app.Listen() go app.RegisterRepeatedly(1 * time.Second) Eventually(func() bool { return appRegistered(registry, app) }).Should(BeTrue()) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", config.Ip, config.Port)) Expect(err).NotTo(HaveOccurred()) defer conn.Close() httpConn := test_util.NewHttpConn(conn) req := test_util.NewRequest("GET", "foo.vcap.me", "/", nil) req.Header.Add(router_http.VcapRequestIdHeader, "A-BOGUS-REQUEST-ID") httpConn.WriteRequest(req) var answer string Eventually(done).Should(Receive(&answer)) Expect(answer).ToNot(Equal("A-BOGUS-REQUEST-ID")) Expect(answer).To(MatchRegexp(uuid_regex)) Expect(logger).To(gbytes.Say("vcap-request-id-header-set")) resp, _ := httpConn.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) })