From 57e0616b3d35021bdb13044ad0fdf3d54bd2722d Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Thu, 3 Aug 2017 20:45:02 +0200 Subject: [PATCH] Simplify integration suite setup --- integrationtests/client_test.go | 9 ++-- integrationtests/drop_test.go | 3 +- integrationtests/integration_test.go | 9 ++-- .../integrationtests_suite_test.go | 49 +++---------------- integrationtests/random_rtt_test.go | 3 +- integrationtests/rtt_test.go | 3 +- 6 files changed, 22 insertions(+), 54 deletions(-) diff --git a/integrationtests/client_test.go b/integrationtests/client_test.go index 9f2eda25..a7ae278d 100644 --- a/integrationtests/client_test.go +++ b/integrationtests/client_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/lucas-clemente/quic-go/h2quic" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/protocol" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -46,7 +47,7 @@ var _ = Describe("Client tests", func() { }) It("downloads a hello", func() { - resp, err := client.Get("https://quic.clemente.io:" + port + "/hello") + resp, err := client.Get("https://quic.clemente.io:" + testserver.Port() + "/hello") Expect(err).ToNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(200)) body, err := ioutil.ReadAll(gbytes.TimeoutReader(resp.Body, 3*time.Second)) @@ -56,7 +57,7 @@ var _ = Describe("Client tests", func() { It("downloads a small file", func() { dataMan.GenerateData(dataLen) - resp, err := client.Get("https://quic.clemente.io:" + port + "/data") + resp, err := client.Get("https://quic.clemente.io:" + testserver.Port() + "/data") Expect(err).ToNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(200)) body, err := ioutil.ReadAll(gbytes.TimeoutReader(resp.Body, 5*time.Second)) @@ -66,7 +67,7 @@ var _ = Describe("Client tests", func() { It("downloads a large file", func() { dataMan.GenerateData(dataLongLen) - resp, err := client.Get("https://quic.clemente.io:" + port + "/data") + resp, err := client.Get("https://quic.clemente.io:" + testserver.Port() + "/data") Expect(err).ToNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(200)) body, err := ioutil.ReadAll(gbytes.TimeoutReader(resp.Body, 20*time.Second)) @@ -77,7 +78,7 @@ var _ = Describe("Client tests", func() { It("uploads a file", func() { dataMan.GenerateData(dataLen) data := bytes.NewReader(dataMan.GetData()) - resp, err := client.Post("https://quic.clemente.io:"+port+"/echo", "text/plain", data) + resp, err := client.Post("https://quic.clemente.io:"+testserver.Port()+"/echo", "text/plain", data) Expect(err).ToNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(200)) body, err := ioutil.ReadAll(gbytes.TimeoutReader(resp.Body, 5*time.Second)) diff --git a/integrationtests/drop_test.go b/integrationtests/drop_test.go index 832d8d8a..f6dbf2b2 100644 --- a/integrationtests/drop_test.go +++ b/integrationtests/drop_test.go @@ -8,6 +8,7 @@ import ( _ "github.com/lucas-clemente/quic-clients" // download clients "github.com/lucas-clemente/quic-go/integrationtests/tools/proxy" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/protocol" . "github.com/onsi/ginkgo" @@ -25,7 +26,7 @@ var _ = Describe("Drop Proxy", func() { runDropTest := func(dropCallback quicproxy.DropCallback, version protocol.VersionNumber) { var err error proxy, err = quicproxy.NewQuicProxy("localhost:0", quicproxy.Opts{ - RemoteAddr: "localhost:" + port, + RemoteAddr: "localhost:" + testserver.Port(), DropPacket: dropCallback, }) Expect(err).ToNot(HaveOccurred()) diff --git a/integrationtests/integration_test.go b/integrationtests/integration_test.go index 68d77791..fc92e308 100644 --- a/integrationtests/integration_test.go +++ b/integrationtests/integration_test.go @@ -7,6 +7,7 @@ import ( "strconv" "sync" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/protocol" _ "github.com/lucas-clemente/quic-clients" // download clients @@ -31,7 +32,7 @@ var _ = Describe("Integration tests", func() { clientPath, "--quic-version="+strconv.Itoa(int(version)), "--host=127.0.0.1", - "--port="+port, + "--port="+testserver.Port(), "https://quic.clemente.io/hello", ) session, err := Start(command, nil, GinkgoWriter) @@ -47,7 +48,7 @@ var _ = Describe("Integration tests", func() { clientPath, "--quic-version="+strconv.Itoa(int(version)), "--host=127.0.0.1", - "--port="+port, + "--port="+testserver.Port(), "--body=foo", "https://quic.clemente.io/echo", ) @@ -64,7 +65,7 @@ var _ = Describe("Integration tests", func() { clientPath, "--quic-version="+strconv.Itoa(int(version)), "--host=127.0.0.1", - "--port="+port, + "--port="+testserver.Port(), "https://quic.clemente.io/data", ) session, err := Start(command, nil, GinkgoWriter) @@ -85,7 +86,7 @@ var _ = Describe("Integration tests", func() { clientPath, "--quic-version="+strconv.Itoa(int(version)), "--host=127.0.0.1", - "--port="+port, + "--port="+testserver.Port(), "https://quic.clemente.io/data", ) session, err := Start(command, nil, GinkgoWriter) diff --git a/integrationtests/integrationtests_suite_test.go b/integrationtests/integrationtests_suite_test.go index 3b5a3da1..1cccab2d 100644 --- a/integrationtests/integrationtests_suite_test.go +++ b/integrationtests/integrationtests_suite_test.go @@ -4,17 +4,12 @@ import ( "fmt" "io" "io/ioutil" - "net" "net/http" "path/filepath" "runtime" - "strconv" - - "github.com/lucas-clemente/quic-go/h2quic" - "github.com/lucas-clemente/quic-go/internal/testdata" - _ "github.com/lucas-clemente/quic-go/integrationtests/tools/testlog" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -28,9 +23,7 @@ const ( ) var ( - server *h2quic.Server dataMan dataManager - port string clientPath string serverPath string ) @@ -40,23 +33,17 @@ func TestIntegration(t *testing.T) { RunSpecs(t, "Integration Tests Suite") } -var _ = BeforeSuite(setupHTTPHandlers) +var _ = JustBeforeEach(testserver.StartQuicServer) -var _ = BeforeEach(func() { +var _ = AfterEach(testserver.StopQuicServer) + +func init() { _, thisfile, _, ok := runtime.Caller(0) if !ok { - Fail("Failed to get current path") + panic("Failed to get current path") } clientPath = filepath.Join(thisfile, fmt.Sprintf("../../../quic-clients/client-%s-debug", runtime.GOOS)) serverPath = filepath.Join(thisfile, fmt.Sprintf("../../../quic-clients/server-%s-debug", runtime.GOOS)) -}) - -var _ = JustBeforeEach(startQuicServer) - -var _ = AfterEach(stopQuicServer) - -func setupHTTPHandlers() { - defer GinkgoRecover() http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { defer GinkgoRecover() @@ -79,28 +66,4 @@ func setupHTTPHandlers() { _, err = w.Write(body) Expect(err).NotTo(HaveOccurred()) }) - -} - -func startQuicServer() { - server = &h2quic.Server{ - Server: &http.Server{ - TLSConfig: testdata.GetTLSConfig(), - }, - } - - addr, err := net.ResolveUDPAddr("udp", "0.0.0.0:0") - Expect(err).NotTo(HaveOccurred()) - conn, err := net.ListenUDP("udp", addr) - Expect(err).NotTo(HaveOccurred()) - port = strconv.Itoa(conn.LocalAddr().(*net.UDPAddr).Port) - - go func() { - defer GinkgoRecover() - server.Serve(conn) - }() -} - -func stopQuicServer() { - Expect(server.Close()).NotTo(HaveOccurred()) } diff --git a/integrationtests/random_rtt_test.go b/integrationtests/random_rtt_test.go index f7f03d18..e8cfd7e3 100644 --- a/integrationtests/random_rtt_test.go +++ b/integrationtests/random_rtt_test.go @@ -10,6 +10,7 @@ import ( _ "github.com/lucas-clemente/quic-clients" // download clients "github.com/lucas-clemente/quic-go/integrationtests/tools/proxy" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/protocol" . "github.com/onsi/ginkgo" @@ -60,7 +61,7 @@ var _ = Describe("Random RTT", func() { rand.Seed(time.Now().UnixNano()) var err error proxy, err = quicproxy.NewQuicProxy("localhost:", quicproxy.Opts{ - RemoteAddr: "localhost:" + port, + RemoteAddr: "localhost:" + testserver.Port(), DelayPacket: func(_ quicproxy.Direction, _ protocol.PacketNumber) time.Duration { return getRandomDuration(minRtt, maxRtt) }, diff --git a/integrationtests/rtt_test.go b/integrationtests/rtt_test.go index 6d3f8c1e..a0a570fc 100644 --- a/integrationtests/rtt_test.go +++ b/integrationtests/rtt_test.go @@ -9,6 +9,7 @@ import ( _ "github.com/lucas-clemente/quic-clients" // download clients "github.com/lucas-clemente/quic-go/integrationtests/tools/proxy" + "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/protocol" . "github.com/onsi/ginkgo" @@ -26,7 +27,7 @@ var _ = Describe("non-zero RTT", func() { runRTTTest := func(rtt time.Duration, version protocol.VersionNumber) { var err error proxy, err = quicproxy.NewQuicProxy("localhost:", quicproxy.Opts{ - RemoteAddr: "localhost:" + port, + RemoteAddr: "localhost:" + testserver.Port(), DelayPacket: func(_ quicproxy.Direction, _ protocol.PacketNumber) time.Duration { return rtt / 2 },