From 6be37ad533934bdb379123b8cc9cf2deae600708 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 17 Feb 2024 15:50:07 +0800 Subject: [PATCH] chore: optimize coding style and add unit tests (#3917) --- core/mathx/entropy_test.go | 14 ++++++++++++++ rest/handler/timeouthandler_test.go | 1 + 2 files changed, 15 insertions(+) diff --git a/core/mathx/entropy_test.go b/core/mathx/entropy_test.go index 3b6c2659..03db018d 100644 --- a/core/mathx/entropy_test.go +++ b/core/mathx/entropy_test.go @@ -15,3 +15,17 @@ func TestCalcEntropy(t *testing.T) { } assert.True(t, CalcEntropy(m) > .99) } + +func TestCalcEmptyEntropy(t *testing.T) { + m := make(map[any]int) + assert.Equal(t, float64(1), CalcEntropy(m)) +} + +func TestCalcDiffEntropy(t *testing.T) { + const total = 1000 + m := make(map[any]int, total) + for i := 0; i < total; i++ { + m[i] = i + } + assert.True(t, CalcEntropy(m) < .99) +} diff --git a/rest/handler/timeouthandler_test.go b/rest/handler/timeouthandler_test.go index ee6fdb91..f4f97d45 100644 --- a/rest/handler/timeouthandler_test.go +++ b/rest/handler/timeouthandler_test.go @@ -172,6 +172,7 @@ func TestTimeoutSSE(t *testing.T) { handler.ServeHTTP(resp, req) assert.Equal(t, http.StatusOK, resp.Code) } + func TestTimeoutWebsocket(t *testing.T) { timeoutHandler := TimeoutHandler(time.Millisecond) handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {