chia7712 commented on code in PR #757: URL: https://github.com/apache/yunikorn-core/pull/757#discussion_r1596861596
########## pkg/webservice/webservice_test.go: ########## @@ -0,0 +1,125 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package webservice + +import ( + "compress/gzip" + "encoding/json" + "io" + "net" + "net/http" + "net/url" + "testing" + "time" + + "github.com/julienschmidt/httprouter" + "go.uber.org/zap" + "gotest.tools/v3/assert" + + "github.com/apache/yunikorn-core/pkg/common" + "github.com/apache/yunikorn-core/pkg/log" + "github.com/apache/yunikorn-core/pkg/scheduler" +) + +func TestCompression(t *testing.T) { + m := NewWebApp(scheduler.NewScheduler().GetClusterContext(), nil) + // dummy route and corresponding handler + testRoute := route{ + "testHelloWord", + "GET", + "/ws/v1/helloWorld", + getHelloWorld, + } + router := httprouter.New() + testHandler := gzipHandler(loggingHandler(testRoute.HandlerFunc, testRoute.Name)) + router.Handler(testRoute.Method, testRoute.Pattern, testHandler) + + // start simulation server + m.httpServer = &http.Server{Addr: ":9080", Handler: router, ReadHeaderTimeout: 5 * time.Second} + go func() { Review Comment: Could we reuse the code of `WebService`? For example, we can add variety `startWebApp` to accept a custom route. ```go func newRouter(routes []route) *httprouter.Router { router := httprouter.New() for _, webRoute := range routes { handler := gzipHandler(loggingHandler(webRoute.HandlerFunc, webRoute.Name)) router.Handler(webRoute.Method, webRoute.Pattern, handler) } return router } func (m *WebService) StartWebApp() { m.startWebApp(webRoutes) } func (m *WebService) startWebApp(routes []route) { m.httpServer = &http.Server{Addr: ":9080", Handler: newRouter(routes)} log.Log(log.REST).Info("web-app started", zap.Int("port", 9080)) go func() { httpError := m.httpServer.ListenAndServe() if httpError != nil && !errors.Is(httpError, http.ErrServerClosed) { log.Log(log.REST).Error("HTTP serving error", zap.Error(httpError)) } }() } ``` and then we call `startWebApp` instead of invoking thread. ```go m := NewWebApp(scheduler.NewScheduler().GetClusterContext(), nil) // start simulation server m.startWebApp([]route{route{ "testHelloWord", "GET", "/ws/v1/helloWorld", getHelloWorld, }}) ``` ########## pkg/webservice/webservice_test.go: ########## @@ -0,0 +1,125 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package webservice + +import ( + "compress/gzip" + "encoding/json" + "io" + "net" + "net/http" + "net/url" + "testing" + "time" + + "github.com/julienschmidt/httprouter" + "go.uber.org/zap" + "gotest.tools/v3/assert" + + "github.com/apache/yunikorn-core/pkg/common" + "github.com/apache/yunikorn-core/pkg/log" + "github.com/apache/yunikorn-core/pkg/scheduler" +) + +func TestCompression(t *testing.T) { + m := NewWebApp(scheduler.NewScheduler().GetClusterContext(), nil) + // dummy route and corresponding handler + testRoute := route{ + "testHelloWord", + "GET", + "/ws/v1/helloWorld", + getHelloWorld, + } + router := httprouter.New() + testHandler := gzipHandler(loggingHandler(testRoute.HandlerFunc, testRoute.Name)) + router.Handler(testRoute.Method, testRoute.Pattern, testHandler) + + // start simulation server + m.httpServer = &http.Server{Addr: ":9080", Handler: router, ReadHeaderTimeout: 5 * time.Second} + go func() { + httpError := m.httpServer.ListenAndServe() + if httpError != nil { + log.Log(log.REST).Error("HTTP serving error", + zap.Error(httpError)) + } + }() + defer func() { + err := m.StopWebApp() + assert.NilError(t, err, "Error when closing webapp service.") + }() + + err := common.WaitFor(500*time.Millisecond, 5*time.Second, func() bool { + conn, connErr := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "9080"), time.Second) + if connErr == nil { + conn.Close() + } + return connErr == nil + }) + assert.NilError(t, err, "Web app failed to start in 5 seconds.") + + u := &url.URL{ + Host: "localhost:9080", + Scheme: "http", + Path: "/ws/v1/helloWorld", + } + + // request without gzip compression + var buf io.ReadWriter + req, err := http.NewRequest("GET", u.String(), buf) + assert.NilError(t, err, "Create new http request failed.") + req.Header.Set("Accept", "application/json") + + // prevent http.DefaultClient from automatically adding gzip header Review Comment: We can merge those code into a helper function. For example: ```go check := func(compression bool) { req, err := http.NewRequest("GET", u.String(), nil) assert.NilError(t, err, "Create new http request failed.") req.Header.Set("Accept", "application/json") if compression { req.Header.Set("Accept-Encoding", "gzip") } else { req.Header.Set("Accept-Encoding", "deflate") } resp, err := http.DefaultClient.Do(req) assert.NilError(t, err, "Http request failed.") defer resp.Body.Close() var reader io.Reader if compression { gzipReader, err := gzip.NewReader(resp.Body) assert.NilError(t, err, "Failed to create gzip reader.") defer gzipReader.Close() reader = gzipReader } else { reader = resp.Body } byteArr, err := io.ReadAll(reader) assert.NilError(t, err, "Failed to read body.") var respMsg map[string]string err = json.Unmarshal(byteArr, &respMsg) assert.NilError(t, err, unmarshalError) assert.Equal(t, len(respMsg), 1) assert.Equal(t, respMsg["data"], "hello world") } check(false) check(true) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
