From 9ef796891f8f5aaea56abbdf41ee41d6e4e726cc Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 12 Dec 2023 19:51:53 +0800 Subject: [PATCH 01/17] add compression part --- pkg/webservice/handlers.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index c2ecfc863..0a8e90ddf 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -19,6 +19,8 @@ package webservice import ( + "bytes" + "compress/gzip" "encoding/json" "fmt" "io" @@ -614,6 +616,33 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { appsDao = append(appsDao, getApplicationDAO(app)) } + if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + w.Header().Set("Content-Encoding", "gzip") + + response, err := json.Marshal(appsDao) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusBadRequest) + return + } + + var comp bytes.Buffer + writer := gzip.NewWriter(&comp) + _, err = writer.Write(response) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + err = writer.Close() + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + if _, err = w.Write(comp.Bytes()); err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + } + return + } + if err := json.NewEncoder(w).Encode(appsDao); err != nil { buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) } From 85dd3de9daa3b741616146814da3469a6a7d1925 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 12 Dec 2023 19:51:53 +0800 Subject: [PATCH 02/17] add compression part --- pkg/webservice/handlers.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index c2ecfc863..0a8e90ddf 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -19,6 +19,8 @@ package webservice import ( + "bytes" + "compress/gzip" "encoding/json" "fmt" "io" @@ -614,6 +616,33 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { appsDao = append(appsDao, getApplicationDAO(app)) } + if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + w.Header().Set("Content-Encoding", "gzip") + + response, err := json.Marshal(appsDao) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusBadRequest) + return + } + + var comp bytes.Buffer + writer := gzip.NewWriter(&comp) + _, err = writer.Write(response) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + err = writer.Close() + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + if _, err = w.Write(comp.Bytes()); err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + } + return + } + if err := json.NewEncoder(w).Encode(appsDao); err != nil { buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) } From b5e2651a40f18ee174316b42ab951c07f1f946c3 Mon Sep 17 00:00:00 2001 From: targetoee Date: Fri, 8 Mar 2024 21:41:38 +0800 Subject: [PATCH 03/17] modify compress function and add test --- pkg/webservice/handlers.go | 68 +++++++++++++++++++++------------ pkg/webservice/handlers_test.go | 35 +++++++++++++++++ 2 files changed, 79 insertions(+), 24 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 0a8e90ddf..c89cd84a7 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -130,6 +130,10 @@ func writeHeaders(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Accept,Origin") } +func writeHeader(w http.ResponseWriter, key, val string) { + w.Header().Set(key, val) +} + func buildJSONErrorResponse(w http.ResponseWriter, detail string, code int) { w.WriteHeader(code) errorInfo := dao.NewYAPIError(nil, code, detail) @@ -616,30 +620,8 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { appsDao = append(appsDao, getApplicationDAO(app)) } - if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { - w.Header().Set("Content-Encoding", "gzip") - - response, err := json.Marshal(appsDao) - if err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusBadRequest) - return - } - - var comp bytes.Buffer - writer := gzip.NewWriter(&comp) - _, err = writer.Write(response) - if err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - return - } - err = writer.Close() - if err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - return - } - if _, err = w.Write(comp.Bytes()); err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - } + if checkHeader(r.Header, "Content-Encoding", "gzip") { + compress(w, appsDao) return } @@ -964,3 +946,41 @@ func getEvents(w http.ResponseWriter, r *http.Request) { buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) } } + + +func checkHeader(h http.Header, key string, value string) bool { + values := h.Values(key) + for _, v := range values { + if v == value { + return true + } + } + return false +} + +func compress(w http.ResponseWriter, data any) { + response, err := json.Marshal(data) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + + var compressedData bytes.Buffer + writer := gzip.NewWriter(&compressedData) + _, err = writer.Write(response) + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + + err = writer.Close() + if err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + return + } + + writeHeader(w, "Content-Encoding", "gzip") + if _, err = w.Write(compressedData.Bytes()); err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + } +} \ No newline at end of file diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index bd2405688..08ea6bab2 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,9 +19,12 @@ package webservice import ( + "bytes" + "compress/gzip" "context" "encoding/json" "fmt" + "io" "net/http" "net/http/httptest" "reflect" @@ -1785,3 +1788,35 @@ func runHealthCheckTest(t *testing.T, expected *dao.SchedulerHealthDAOInfo) { assert.Equal(t, expectedHealthCheck.DiagnosisMessage, actualHealthCheck.DiagnosisMessage) } } + +func TestCompressQueueApplications(t *testing.T) { + var appsDao []*dao.ApplicationDAOInfo + app := newApplication("app-01", "part-01", "queue-1", rmID, security.UserGroup{}) + app2 := newApplication("app-02", "part-01", "queue-1", rmID, security.UserGroup{}) + appsDao = append(appsDao, getApplicationDAO(app)) + appsDao = append(appsDao, getApplicationDAO(app2)) + + resp := &MockResponseWriter{} + compress(resp, appsDao) + + buf := bytes.NewBuffer(resp.outputBytes) + gzipReader, err := gzip.NewReader(buf) + assert.NilError(t, err, "Error while decompressing data.") + err = gzipReader.Close() + assert.NilError(t, err, "Error when close gzip reader.") + + uncompressedData, err := io.ReadAll(gzipReader) + assert.NilError(t, err, "Error when read decoded data.") + + var decodedData []*dao.ApplicationDAOInfo + err = json.Unmarshal(uncompressedData, &decodedData) + + for i := range decodedData { + assert.Equal(t, appsDao[i].ApplicationID, decodedData[i].ApplicationID) + assert.Equal(t, appsDao[i].Partition, decodedData[i].Partition) + assert.Equal(t, appsDao[i].QueueName, decodedData[i].QueueName) + assert.Equal(t, appsDao[i].SubmissionTime, decodedData[i].SubmissionTime) + assert.Equal(t, appsDao[i].User, decodedData[i].User) + assert.Equal(t, appsDao[i].Groups[0], decodedData[i].Groups[0]) + } +} From 7238626da637b85dca4e8e898f5fbaf6d1971178 Mon Sep 17 00:00:00 2001 From: targetoee Date: Sun, 10 Mar 2024 09:33:49 +0800 Subject: [PATCH 04/17] fix lint --- pkg/webservice/handlers_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 21624b221..69e025033 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -2625,6 +2625,7 @@ func TestCompressQueueApplications(t *testing.T) { var decodedData []*dao.ApplicationDAOInfo err = json.Unmarshal(uncompressedData, &decodedData) + assert.NilError(t, err, "Error when unmarshal decoded data.") for i := range decodedData { assert.Equal(t, appsDao[i].ApplicationID, decodedData[i].ApplicationID) @@ -2634,4 +2635,4 @@ func TestCompressQueueApplications(t *testing.T) { assert.Equal(t, appsDao[i].User, decodedData[i].User) assert.Equal(t, appsDao[i].Groups[0], decodedData[i].Groups[0]) } -} \ No newline at end of file +} From 995cb9776fa41dffda7819dc18467e8beca94959 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 12 Mar 2024 11:37:01 +0800 Subject: [PATCH 05/17] close writer and check supported comp type --- pkg/webservice/handlers.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 56e81b6a2..e23bf35af 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -61,6 +61,7 @@ const ( GroupNameMissing = "Group name is missing" ApplicationDoesNotExists = "Application not found" NodeDoesNotExists = "Node not found" + UnsupportedCompType = "Compression type not support" ) var allowedActiveStatusMsg string @@ -752,8 +753,12 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { appsDao = append(appsDao, getApplicationDAO(app)) } - if checkHeader(r.Header, "Content-Encoding", "gzip") { - compress(w, appsDao) + if len(r.Header.Get("Content-Encoding")) > 0 { + if checkHeader(r.Header, "Content-Encoding", "gzip") { + compress(w, appsDao) + } else { + buildJSONErrorResponse(w, UnsupportedCompType, http.StatusInternalServerError) + } return } @@ -1249,6 +1254,7 @@ func compress(w http.ResponseWriter, data any) { writer := gzip.NewWriter(&compressedData) _, err = writer.Write(response) if err != nil { + _ = writer.Close() buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) return } From 4cb1d9c3419a42b82974f8e2b39c283999d54911 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 12 Mar 2024 12:01:26 +0800 Subject: [PATCH 06/17] skip compress step if the data size is small --- pkg/webservice/handlers.go | 8 ++++++++ pkg/webservice/handlers_test.go | 34 ++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index e23bf35af..aaf17d3e2 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -1250,6 +1250,14 @@ func compress(w http.ResponseWriter, data any) { return } + // don't compress the data if it is smaller than MTU size + if len(response) < 1500 { + if err = json.NewEncoder(w).Encode(data); err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + } + return + } + var compressedData bytes.Buffer writer := gzip.NewWriter(&compressedData) _, err = writer.Write(response) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 69e025033..7b8fdd585 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -29,6 +29,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "strconv" "strings" "testing" "time" @@ -2606,14 +2607,38 @@ func NewResponseRecorderWithDeadline() *ResponseRecorderWithDeadline { func TestCompressQueueApplications(t *testing.T) { var appsDao []*dao.ApplicationDAOInfo - app := newApplication("app-01", "part-01", "queue-1", rmID, security.UserGroup{}) - app2 := newApplication("app-02", "part-01", "queue-1", rmID, security.UserGroup{}) - appsDao = append(appsDao, getApplicationDAO(app)) - appsDao = append(appsDao, getApplicationDAO(app2)) + + // case 1: data size is smaller than MTU size, so compression step is skipped + for i := 0; i < 2; i++ { + appName := "app-" + strconv.Itoa(i) + app := newApplication(appName, "part-01", "queue-1", rmID, security.UserGroup{}) + appsDao = append(appsDao, getApplicationDAO(app)) + } resp := &MockResponseWriter{} compress(resp, appsDao) + var decodedData []*dao.ApplicationDAOInfo + err := json.Unmarshal(resp.outputBytes, &decodedData) + assert.NilError(t, err, "Error when unmarshal data.") + + for i := range decodedData { + assert.Equal(t, appsDao[i].ApplicationID, decodedData[i].ApplicationID) + assert.Equal(t, appsDao[i].Partition, decodedData[i].Partition) + assert.Equal(t, appsDao[i].QueueName, decodedData[i].QueueName) + assert.Equal(t, appsDao[i].SubmissionTime, decodedData[i].SubmissionTime) + assert.Equal(t, appsDao[i].User, decodedData[i].User) + assert.Equal(t, appsDao[i].Groups[0], decodedData[i].Groups[0]) + } + + // case 2: data size is greater than MTU size, so do the compression + for i := 2; i < 10; i++ { + appName := "app-" + strconv.Itoa(i) + app := newApplication(appName, "part-01", "queue-1", rmID, security.UserGroup{}) + appsDao = append(appsDao, getApplicationDAO(app)) + } + compress(resp, appsDao) + buf := bytes.NewBuffer(resp.outputBytes) gzipReader, err := gzip.NewReader(buf) assert.NilError(t, err, "Error while decompressing data.") @@ -2623,7 +2648,6 @@ func TestCompressQueueApplications(t *testing.T) { uncompressedData, err := io.ReadAll(gzipReader) assert.NilError(t, err, "Error when read decoded data.") - var decodedData []*dao.ApplicationDAOInfo err = json.Unmarshal(uncompressedData, &decodedData) assert.NilError(t, err, "Error when unmarshal decoded data.") From 40b96491f6de432c0be0fa07e37cf027f5770f2a Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 12 Mar 2024 19:46:17 +0800 Subject: [PATCH 07/17] correct header setting --- pkg/webservice/handlers.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index aaf17d3e2..0106389d6 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -752,13 +752,8 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { for _, app := range queue.GetCopyOfApps() { appsDao = append(appsDao, getApplicationDAO(app)) } - - if len(r.Header.Get("Content-Encoding")) > 0 { - if checkHeader(r.Header, "Content-Encoding", "gzip") { - compress(w, appsDao) - } else { - buildJSONErrorResponse(w, UnsupportedCompType, http.StatusInternalServerError) - } + if checkHeader(r.Header, "Accept-Encoding", "gzip") { + compress(w, appsDao) return } @@ -1236,8 +1231,12 @@ func getStream(w http.ResponseWriter, r *http.Request) { func checkHeader(h http.Header, key string, value string) bool { values := h.Values(key) for _, v := range values { - if v == value { - return true + v2 := strings.Split(v, ",") + for _, item := range v2 { + item = strings.TrimSpace(item) + if item == value { + return true + } } } return false From 4f5756873aef5ca34fd566c0f7e958a64ed27f70 Mon Sep 17 00:00:00 2001 From: targetoee Date: Sun, 17 Mar 2024 09:03:07 +0800 Subject: [PATCH 08/17] modify compress & checkheader function --- pkg/webservice/handlers.go | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 0106389d6..6f448e3cd 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -19,7 +19,6 @@ package webservice import ( - "bytes" "compress/gzip" "encoding/json" "fmt" @@ -1231,12 +1230,8 @@ func getStream(w http.ResponseWriter, r *http.Request) { func checkHeader(h http.Header, key string, value string) bool { values := h.Values(key) for _, v := range values { - v2 := strings.Split(v, ",") - for _, item := range v2 { - item = strings.TrimSpace(item) - if item == value { - return true - } + if strings.Contains(v, value) { + return true } } return false @@ -1249,31 +1244,20 @@ func compress(w http.ResponseWriter, data any) { return } - // don't compress the data if it is smaller than MTU size - if len(response) < 1500 { - if err = json.NewEncoder(w).Encode(data); err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - } - return - } - - var compressedData bytes.Buffer - writer := gzip.NewWriter(&compressedData) + writer := gzip.NewWriter(w) + w.Header().Set("Content-Encoding", "gzip") _, err = writer.Write(response) if err != nil { _ = writer.Close() buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) return } - - err = writer.Close() + err = writer.Flush() if err != nil { + _ = writer.Close() buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) return } - writeHeader(w, "Content-Encoding", "gzip") - if _, err = w.Write(compressedData.Bytes()); err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - } + _ = writer.Close() } From 4e7228a6c7fce2f52e69908fb40e45ba4d43d138 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 19 Mar 2024 14:49:04 +0800 Subject: [PATCH 09/17] remove unused testcase --- pkg/webservice/handlers_test.go | 56 --------------------------------- 1 file changed, 56 deletions(-) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 7b8fdd585..5367f4592 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -2604,59 +2604,3 @@ func NewResponseRecorderWithDeadline() *ResponseRecorderWithDeadline { ResponseRecorder: httptest.NewRecorder(), } } - -func TestCompressQueueApplications(t *testing.T) { - var appsDao []*dao.ApplicationDAOInfo - - // case 1: data size is smaller than MTU size, so compression step is skipped - for i := 0; i < 2; i++ { - appName := "app-" + strconv.Itoa(i) - app := newApplication(appName, "part-01", "queue-1", rmID, security.UserGroup{}) - appsDao = append(appsDao, getApplicationDAO(app)) - } - - resp := &MockResponseWriter{} - compress(resp, appsDao) - - var decodedData []*dao.ApplicationDAOInfo - err := json.Unmarshal(resp.outputBytes, &decodedData) - assert.NilError(t, err, "Error when unmarshal data.") - - for i := range decodedData { - assert.Equal(t, appsDao[i].ApplicationID, decodedData[i].ApplicationID) - assert.Equal(t, appsDao[i].Partition, decodedData[i].Partition) - assert.Equal(t, appsDao[i].QueueName, decodedData[i].QueueName) - assert.Equal(t, appsDao[i].SubmissionTime, decodedData[i].SubmissionTime) - assert.Equal(t, appsDao[i].User, decodedData[i].User) - assert.Equal(t, appsDao[i].Groups[0], decodedData[i].Groups[0]) - } - - // case 2: data size is greater than MTU size, so do the compression - for i := 2; i < 10; i++ { - appName := "app-" + strconv.Itoa(i) - app := newApplication(appName, "part-01", "queue-1", rmID, security.UserGroup{}) - appsDao = append(appsDao, getApplicationDAO(app)) - } - compress(resp, appsDao) - - buf := bytes.NewBuffer(resp.outputBytes) - gzipReader, err := gzip.NewReader(buf) - assert.NilError(t, err, "Error while decompressing data.") - err = gzipReader.Close() - assert.NilError(t, err, "Error when close gzip reader.") - - uncompressedData, err := io.ReadAll(gzipReader) - assert.NilError(t, err, "Error when read decoded data.") - - err = json.Unmarshal(uncompressedData, &decodedData) - assert.NilError(t, err, "Error when unmarshal decoded data.") - - for i := range decodedData { - assert.Equal(t, appsDao[i].ApplicationID, decodedData[i].ApplicationID) - assert.Equal(t, appsDao[i].Partition, decodedData[i].Partition) - assert.Equal(t, appsDao[i].QueueName, decodedData[i].QueueName) - assert.Equal(t, appsDao[i].SubmissionTime, decodedData[i].SubmissionTime) - assert.Equal(t, appsDao[i].User, decodedData[i].User) - assert.Equal(t, appsDao[i].Groups[0], decodedData[i].Groups[0]) - } -} From 22059023351802318f90522b8ee8d4ef519c7316 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 19 Mar 2024 14:51:35 +0800 Subject: [PATCH 10/17] unused import --- pkg/webservice/handlers_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 5367f4592..6745ce16f 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,17 +19,13 @@ package webservice import ( - "bytes" - "compress/gzip" "context" "encoding/json" "errors" "fmt" - "io" "net/http" "net/http/httptest" "reflect" - "strconv" "strings" "testing" "time" From b811993b34ce15309a2a0762962c6ec53fe0a4a7 Mon Sep 17 00:00:00 2001 From: targetoee Date: Tue, 19 Mar 2024 15:47:51 +0800 Subject: [PATCH 11/17] draft testcase --- pkg/webservice/handlers_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 6745ce16f..546993ad1 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,10 +19,13 @@ package webservice import ( + "bytes" + "compress/gzip" "context" "encoding/json" "errors" "fmt" + "io" "net/http" "net/http/httptest" "reflect" @@ -1469,6 +1472,27 @@ func TestGetQueueApplicationsHandler(t *testing.T) { resp = &MockResponseWriter{} getQueueApplications(resp, req) assertParamsMissing(t, resp) + + // test compression + var req4 *http.Request + req4, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/applications", strings.NewReader("")) + req4 = req4.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{ + httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID}, + httprouter.Param{Key: "queue", Value: "root.default"}, + })) + req4.Header.Set("Accept-Encoding", "gzip") + assert.NilError(t, err, "Get Queue Applications Handler request failed") + resp4 := &MockResponseWriter{} + getQueueApplications(resp4, req4) + + var appsDao4 []*dao.ApplicationDAOInfo + buf := bytes.NewBuffer(resp4.outputBytes) + gzipReader, err := gzip.NewReader(buf) + assert.NilError(t, err, "Create gzip reader failed.") + uncompress, err := io.ReadAll(gzipReader) + assert.NilError(t, err, "Decode gzip data failed.") + err = json.Unmarshal(uncompress, &appsDao4) + assert.NilError(t, err, unmarshalError) } func checkLegalGetAppsRequest(t *testing.T, url string, params httprouter.Params, expected []*dao.ApplicationDAOInfo) { From 8a6f9cfeebaae19f5cfcbedcdd7834f52acf0abc Mon Sep 17 00:00:00 2001 From: targetoee Date: Fri, 5 Apr 2024 13:29:41 +0800 Subject: [PATCH 12/17] handle compression in unified manner --- pkg/webservice/handlers.go | 45 --------------------------------- pkg/webservice/handlers_test.go | 23 ----------------- pkg/webservice/webservice.go | 29 ++++++++++++++++++++- 3 files changed, 28 insertions(+), 69 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 6f448e3cd..ffea78441 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -19,7 +19,6 @@ package webservice import ( - "compress/gzip" "encoding/json" "fmt" "io" @@ -60,7 +59,6 @@ const ( GroupNameMissing = "Group name is missing" ApplicationDoesNotExists = "Application not found" NodeDoesNotExists = "Node not found" - UnsupportedCompType = "Compression type not support" ) var allowedActiveStatusMsg string @@ -155,10 +153,6 @@ func writeHeaders(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Accept,Origin") } -func writeHeader(w http.ResponseWriter, key, val string) { - w.Header().Set(key, val) -} - func buildJSONErrorResponse(w http.ResponseWriter, detail string, code int) { w.WriteHeader(code) errorInfo := dao.NewYAPIError(nil, code, detail) @@ -751,10 +745,6 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { for _, app := range queue.GetCopyOfApps() { appsDao = append(appsDao, getApplicationDAO(app)) } - if checkHeader(r.Header, "Accept-Encoding", "gzip") { - compress(w, appsDao) - return - } if err := json.NewEncoder(w).Encode(appsDao); err != nil { buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) @@ -1226,38 +1216,3 @@ func getStream(w http.ResponseWriter, r *http.Request) { } } } - -func checkHeader(h http.Header, key string, value string) bool { - values := h.Values(key) - for _, v := range values { - if strings.Contains(v, value) { - return true - } - } - return false -} - -func compress(w http.ResponseWriter, data any) { - response, err := json.Marshal(data) - if err != nil { - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - return - } - - writer := gzip.NewWriter(w) - w.Header().Set("Content-Encoding", "gzip") - _, err = writer.Write(response) - if err != nil { - _ = writer.Close() - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - return - } - err = writer.Flush() - if err != nil { - _ = writer.Close() - buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) - return - } - - _ = writer.Close() -} diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 546993ad1..04b6a67b3 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,13 +19,11 @@ package webservice import ( - "bytes" "compress/gzip" "context" "encoding/json" "errors" "fmt" - "io" "net/http" "net/http/httptest" "reflect" @@ -1472,27 +1470,6 @@ func TestGetQueueApplicationsHandler(t *testing.T) { resp = &MockResponseWriter{} getQueueApplications(resp, req) assertParamsMissing(t, resp) - - // test compression - var req4 *http.Request - req4, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/applications", strings.NewReader("")) - req4 = req4.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{ - httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID}, - httprouter.Param{Key: "queue", Value: "root.default"}, - })) - req4.Header.Set("Accept-Encoding", "gzip") - assert.NilError(t, err, "Get Queue Applications Handler request failed") - resp4 := &MockResponseWriter{} - getQueueApplications(resp4, req4) - - var appsDao4 []*dao.ApplicationDAOInfo - buf := bytes.NewBuffer(resp4.outputBytes) - gzipReader, err := gzip.NewReader(buf) - assert.NilError(t, err, "Create gzip reader failed.") - uncompress, err := io.ReadAll(gzipReader) - assert.NilError(t, err, "Decode gzip data failed.") - err = json.Unmarshal(uncompress, &appsDao4) - assert.NilError(t, err, unmarshalError) } func checkLegalGetAppsRequest(t *testing.T, url string, params httprouter.Params, expected []*dao.ApplicationDAOInfo) { diff --git a/pkg/webservice/webservice.go b/pkg/webservice/webservice.go index bf064dfff..01f09f4b4 100644 --- a/pkg/webservice/webservice.go +++ b/pkg/webservice/webservice.go @@ -19,9 +19,12 @@ package webservice import ( + "compress/gzip" "context" "errors" + "io" "net/http" + "strings" "time" "github.com/julienschmidt/httprouter" @@ -43,7 +46,7 @@ type WebService struct { func newRouter() *httprouter.Router { router := httprouter.New() for _, webRoute := range webRoutes { - handler := loggingHandler(webRoute.HandlerFunc, webRoute.Name) + handler := gzipHandler(loggingHandler(webRoute.HandlerFunc, webRoute.Name)) router.Handler(webRoute.Method, webRoute.Pattern, handler) } return router @@ -94,3 +97,27 @@ func (m *WebService) StopWebApp() error { return nil } + +type gzipResponseWriter struct { + io.Writer + http.ResponseWriter +} + +func (w gzipResponseWriter) Write(b []byte) (int, error) { + return w.Writer.Write(b) +} + +func gzipHandler(fn http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + fn(w, r) + return + } + w.Header().Set("Content-Encoding", "gzip") + w.Header().Del("Content-Length") + gz := gzip.NewWriter(w) + defer gz.Close() + gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w} + fn(gzr, r) + } +} From 1c4a0e1d5cf7c1f87f0f4f8955965c8ee3958e66 Mon Sep 17 00:00:00 2001 From: targetoee Date: Fri, 5 Apr 2024 16:58:16 +0800 Subject: [PATCH 13/17] fix test --- pkg/scheduler/tests/restclient_test.go | 8 +++++++- pkg/webservice/handlers_test.go | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/scheduler/tests/restclient_test.go b/pkg/scheduler/tests/restclient_test.go index 362407976..81cbb0b2a 100644 --- a/pkg/scheduler/tests/restclient_test.go +++ b/pkg/scheduler/tests/restclient_test.go @@ -50,7 +50,13 @@ func (c *RClient) GetEventsStream(count uint64) (io.ReadCloser, error) { return nil, err } req.URL.RawQuery = "count=" + strconv.FormatUint(count, 10) - resp, err := http.DefaultClient.Do(req) + tr := &http.Transport{ + DisableCompression: true, + } + client := &http.Client{ + Transport: tr, + } + resp, err := client.Do(req) if err != nil { return nil, err } diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 0d4685062..dd264d285 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,7 +19,6 @@ package webservice import ( - "compress/gzip" "context" "encoding/json" "errors" From 41c0570fc59b8f45c19b826c9588afa41a59dcd7 Mon Sep 17 00:00:00 2001 From: targetoee Date: Mon, 8 Apr 2024 14:48:12 +0800 Subject: [PATCH 14/17] add compression test case --- pkg/webservice/handlers_test.go | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index dd264d285..3ef867fe1 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,13 +19,18 @@ package webservice import ( + "compress/gzip" "context" "encoding/json" "errors" "fmt" + "io" + "net" "net/http" "net/http/httptest" + "net/url" "reflect" + "sort" "strings" "testing" "time" @@ -2602,3 +2607,82 @@ func NewResponseRecorderWithDeadline() *ResponseRecorderWithDeadline { ResponseRecorder: httptest.NewRecorder(), } } + +func TestCompressGetQueueApplicationAPI(t *testing.T) { + schedulerContext, err := scheduler.NewClusterContext(rmID, policyGroup, []byte(configDefault)) + assert.NilError(t, err, "Error when creating cluster context.") + partitionName := common.GetNormalizedPartitionName("default", rmID) + part := schedulerContext.GetPartition(partitionName) + _ = addApp(t, "app-1", part, "root.default", false) + _ = addApp(t, "app-2", part, "root.default", false) + _ = addApp(t, "app-3", part, "root.default", false) + + m := NewWebApp(schedulerContext, nil) + m.StartWebApp() + defer func() { + err = m.StopWebApp() + assert.NilError(t, err, "Error when closing webapp service.") + }() + + err = common.WaitFor(500*time.Millisecond, 2*time.Second, func() bool { + conn, connErr := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "9080"), time.Second) + if connErr == nil { + defer conn.Close() + return true + } + return false + }) + assert.NilError(t, err, "Webapp failed to start in 2 seconds.") + + u := &url.URL{ + Host: "localhost:9080", + Scheme: "http", + Path: "/ws/v1/partition/default/queue/root.default/applications", + } + + // 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 + req.Header.Set("Accept-Encoding", "deflate") + resp, err := http.DefaultClient.Do(req) + assert.NilError(t, err, "Request failed to send.") + data, err := io.ReadAll(resp.Body) + assert.NilError(t, err, "Failed when reading data.") + defer resp.Body.Close() + var originalResp []*dao.ApplicationDAOInfo + err = json.Unmarshal(data, &originalResp) + assert.NilError(t, err, unmarshalError) + + // request with gzip compression enabled + req.Header.Set("Accept-Encoding", "gzip") + resp2, err := http.DefaultClient.Do(req) + assert.NilError(t, err, "Request failed to send.") + gzipReader, err := gzip.NewReader(resp2.Body) + assert.NilError(t, err, "Failed to create gzip reader.") + data2, err := io.ReadAll(gzipReader) + assert.NilError(t, err, "Failed when reading data.") + defer resp2.Body.Close() + defer gzipReader.Close() + var compressedResp []*dao.ApplicationDAOInfo + err = json.Unmarshal(data2, &compressedResp) + assert.NilError(t, err, unmarshalError) + + sort.Slice(originalResp, func(i, j int) bool { + return (originalResp[i].SubmissionTime < originalResp[j].SubmissionTime) + }) + sort.Slice(compressedResp, func(i, j int) bool { + return (compressedResp[i].SubmissionTime < compressedResp[j].SubmissionTime) + }) + + for i := 0; i < 3; i++ { + assert.Equal(t, originalResp[i].ApplicationID, compressedResp[i].ApplicationID) + assert.Equal(t, originalResp[i].Partition, compressedResp[i].Partition) + assert.Equal(t, originalResp[i].QueueName, compressedResp[i].QueueName) + assert.Equal(t, originalResp[i].SubmissionTime, compressedResp[i].SubmissionTime) + assert.Equal(t, originalResp[i].User, compressedResp[i].User) + assert.DeepEqual(t, originalResp[i].Groups, compressedResp[i].Groups) + } +} From ca18e799aa61017019fe8992c427c781730fc2f9 Mon Sep 17 00:00:00 2001 From: targetoee Date: Sat, 27 Apr 2024 10:51:13 +0800 Subject: [PATCH 15/17] add webservice test --- pkg/webservice/handlers_test.go | 84 -------------------- pkg/webservice/webservice_test.go | 126 ++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 84 deletions(-) create mode 100644 pkg/webservice/webservice_test.go diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 3ef867fe1..dd264d285 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -19,18 +19,13 @@ package webservice import ( - "compress/gzip" "context" "encoding/json" "errors" "fmt" - "io" - "net" "net/http" "net/http/httptest" - "net/url" "reflect" - "sort" "strings" "testing" "time" @@ -2607,82 +2602,3 @@ func NewResponseRecorderWithDeadline() *ResponseRecorderWithDeadline { ResponseRecorder: httptest.NewRecorder(), } } - -func TestCompressGetQueueApplicationAPI(t *testing.T) { - schedulerContext, err := scheduler.NewClusterContext(rmID, policyGroup, []byte(configDefault)) - assert.NilError(t, err, "Error when creating cluster context.") - partitionName := common.GetNormalizedPartitionName("default", rmID) - part := schedulerContext.GetPartition(partitionName) - _ = addApp(t, "app-1", part, "root.default", false) - _ = addApp(t, "app-2", part, "root.default", false) - _ = addApp(t, "app-3", part, "root.default", false) - - m := NewWebApp(schedulerContext, nil) - m.StartWebApp() - defer func() { - err = m.StopWebApp() - assert.NilError(t, err, "Error when closing webapp service.") - }() - - err = common.WaitFor(500*time.Millisecond, 2*time.Second, func() bool { - conn, connErr := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", "9080"), time.Second) - if connErr == nil { - defer conn.Close() - return true - } - return false - }) - assert.NilError(t, err, "Webapp failed to start in 2 seconds.") - - u := &url.URL{ - Host: "localhost:9080", - Scheme: "http", - Path: "/ws/v1/partition/default/queue/root.default/applications", - } - - // 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 - req.Header.Set("Accept-Encoding", "deflate") - resp, err := http.DefaultClient.Do(req) - assert.NilError(t, err, "Request failed to send.") - data, err := io.ReadAll(resp.Body) - assert.NilError(t, err, "Failed when reading data.") - defer resp.Body.Close() - var originalResp []*dao.ApplicationDAOInfo - err = json.Unmarshal(data, &originalResp) - assert.NilError(t, err, unmarshalError) - - // request with gzip compression enabled - req.Header.Set("Accept-Encoding", "gzip") - resp2, err := http.DefaultClient.Do(req) - assert.NilError(t, err, "Request failed to send.") - gzipReader, err := gzip.NewReader(resp2.Body) - assert.NilError(t, err, "Failed to create gzip reader.") - data2, err := io.ReadAll(gzipReader) - assert.NilError(t, err, "Failed when reading data.") - defer resp2.Body.Close() - defer gzipReader.Close() - var compressedResp []*dao.ApplicationDAOInfo - err = json.Unmarshal(data2, &compressedResp) - assert.NilError(t, err, unmarshalError) - - sort.Slice(originalResp, func(i, j int) bool { - return (originalResp[i].SubmissionTime < originalResp[j].SubmissionTime) - }) - sort.Slice(compressedResp, func(i, j int) bool { - return (compressedResp[i].SubmissionTime < compressedResp[j].SubmissionTime) - }) - - for i := 0; i < 3; i++ { - assert.Equal(t, originalResp[i].ApplicationID, compressedResp[i].ApplicationID) - assert.Equal(t, originalResp[i].Partition, compressedResp[i].Partition) - assert.Equal(t, originalResp[i].QueueName, compressedResp[i].QueueName) - assert.Equal(t, originalResp[i].SubmissionTime, compressedResp[i].SubmissionTime) - assert.Equal(t, originalResp[i].User, compressedResp[i].User) - assert.DeepEqual(t, originalResp[i].Groups, compressedResp[i].Groups) - } -} diff --git a/pkg/webservice/webservice_test.go b/pkg/webservice/webservice_test.go new file mode 100644 index 000000000..0df75139f --- /dev/null +++ b/pkg/webservice/webservice_test.go @@ -0,0 +1,126 @@ +/* + 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 TestCompressionWithDummyRoute(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 { + defer conn.Close() + return true + } + return false + }) + assert.NilError(t, err, "Web app failed to start in 2 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 + req.Header.Set("Accept-Encoding", "deflate") + resp, err := http.DefaultClient.Do(req) + assert.NilError(t, err, "Request failed to send.") + defer resp.Body.Close() + byteArr, err := io.ReadAll(resp.Body) + assert.NilError(t, err, "Failed when reading data.") + var respMsg map[string]string + err = json.Unmarshal(byteArr, &respMsg) + assert.NilError(t, err, unmarshalError) + assert.Equal(t, respMsg["data"], "hello world") + + // request with gzip compression enabled + req.Header.Set("Accept-Encoding", "gzip") + resp2, err := http.DefaultClient.Do(req) + assert.NilError(t, err, "Request failed to send.") + defer resp2.Body.Close() + gzipReader, err := gzip.NewReader(resp2.Body) + assert.NilError(t, err, "Failed to create gzip reader.") + byteArr2, err := io.ReadAll(gzipReader) + assert.NilError(t, err, "Failed when reading data.") + var respMsg2 map[string]string + err = json.Unmarshal(byteArr2, &respMsg2) + assert.NilError(t, err, unmarshalError) + assert.Equal(t, respMsg2["data"], "hello world") + defer gzipReader.Close() +} + +func getHelloWorld(w http.ResponseWriter, r *http.Request) { + writeHeaders(w) + result := map[string]string{ + "data": "hello world", + } + + if err := json.NewEncoder(w).Encode(result); err != nil { + buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) + } +} From 0a6c931766657ca27e41766b792fa44cc19876bb Mon Sep 17 00:00:00 2001 From: targetoee Date: Sat, 27 Apr 2024 11:01:22 +0800 Subject: [PATCH 16/17] adjust error message and close connection immediately --- pkg/webservice/webservice_test.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/webservice/webservice_test.go b/pkg/webservice/webservice_test.go index 0df75139f..01319c988 100644 --- a/pkg/webservice/webservice_test.go +++ b/pkg/webservice/webservice_test.go @@ -67,10 +67,9 @@ func TestCompressionWithDummyRoute(t *testing.T) { 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 { - defer conn.Close() - return true + conn.Close() } - return false + return connErr == nil }) assert.NilError(t, err, "Web app failed to start in 2 seconds.") @@ -89,10 +88,10 @@ func TestCompressionWithDummyRoute(t *testing.T) { // prevent http.DefaultClient from automatically adding gzip header req.Header.Set("Accept-Encoding", "deflate") resp, err := http.DefaultClient.Do(req) - assert.NilError(t, err, "Request failed to send.") + assert.NilError(t, err, "Http request failed.") defer resp.Body.Close() byteArr, err := io.ReadAll(resp.Body) - assert.NilError(t, err, "Failed when reading data.") + assert.NilError(t, err, "Failed to read body.") var respMsg map[string]string err = json.Unmarshal(byteArr, &respMsg) assert.NilError(t, err, unmarshalError) @@ -101,14 +100,14 @@ func TestCompressionWithDummyRoute(t *testing.T) { // request with gzip compression enabled req.Header.Set("Accept-Encoding", "gzip") resp2, err := http.DefaultClient.Do(req) - assert.NilError(t, err, "Request failed to send.") + assert.NilError(t, err, "Http request failed.") defer resp2.Body.Close() gzipReader, err := gzip.NewReader(resp2.Body) assert.NilError(t, err, "Failed to create gzip reader.") - byteArr2, err := io.ReadAll(gzipReader) - assert.NilError(t, err, "Failed when reading data.") + compressedData, err := io.ReadAll(gzipReader) + assert.NilError(t, err, "Failed to read body") var respMsg2 map[string]string - err = json.Unmarshal(byteArr2, &respMsg2) + err = json.Unmarshal(compressedData, &respMsg2) assert.NilError(t, err, unmarshalError) assert.Equal(t, respMsg2["data"], "hello world") defer gzipReader.Close() From 3f806baf9f8fa47ce55f00fc571f790a9f3f564f Mon Sep 17 00:00:00 2001 From: targetoee Date: Sun, 28 Apr 2024 23:39:54 +0800 Subject: [PATCH 17/17] adjust function name and message --- pkg/webservice/webservice_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/webservice/webservice_test.go b/pkg/webservice/webservice_test.go index 01319c988..33622f010 100644 --- a/pkg/webservice/webservice_test.go +++ b/pkg/webservice/webservice_test.go @@ -37,7 +37,7 @@ import ( "github.com/apache/yunikorn-core/pkg/scheduler" ) -func TestCompressionWithDummyRoute(t *testing.T) { +func TestCompression(t *testing.T) { m := NewWebApp(scheduler.NewScheduler().GetClusterContext(), nil) // dummy route and corresponding handler testRoute := route{ @@ -71,7 +71,7 @@ func TestCompressionWithDummyRoute(t *testing.T) { } return connErr == nil }) - assert.NilError(t, err, "Web app failed to start in 2 seconds.") + assert.NilError(t, err, "Web app failed to start in 5 seconds.") u := &url.URL{ Host: "localhost:9080",