From eae189708f2caa2b5c6c280930d8769c24eb75c5 Mon Sep 17 00:00:00 2001 From: caixw Date: Mon, 27 May 2024 14:33:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugins/health):=20=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store.Get 返回值永远不会为空。其包含的内容不会被执行。 --- plugins/health/health.go | 6 ++--- plugins/health/health_test.go | 51 ++++++++++++++++++++++------------- plugins/health/store.go | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/plugins/health/health.go b/plugins/health/health.go index 3dd3c8d..64c8eb8 100644 --- a/plugins/health/health.go +++ b/plugins/health/health.go @@ -42,10 +42,8 @@ func New(store Store) *Health { return &Health{Enabled: true, store: store} } func (h *Health) Plugin(s web.Server) { s.Routers().Use(web.MiddlewareFunc(func(next web.HandlerFunc, method, pattern, router string) web.HandlerFunc { - if method != http.MethodOptions && method != "" && method != http.MethodHead && - pattern != "" && - h.store.Get(router, method, pattern) == nil { - h.store.Save(newState(router, method, pattern)) + if method != http.MethodOptions && method != "" && method != http.MethodHead && pattern != "" { + h.store.Get(router, method, pattern) // Get 带初始化功能 } return next })) diff --git a/plugins/health/health_test.go b/plugins/health/health_test.go index c5dbf19..d4bc48c 100644 --- a/plugins/health/health_test.go +++ b/plugins/health/health_test.go @@ -23,10 +23,17 @@ var _ web.Plugin = &Health{} func TestHealth(t *testing.T) { a := assert.New(t, false) s := testserver.New(a) - - h := New(NewCacheStore(s, "health_")) - s.Use(h) r := s.Routers().New("def", nil) + + // 在应用插件之前添加的路由 + r.Put("/", func(*web.Context) web.Responser { + time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。 + return nil + }) + + plugin := New(NewCacheStore(s, "health_")) + s.Use(plugin) + r.Get("/", func(ctx *web.Context) web.Responser { status, err := strconv.Atoi(ctx.Request().FormValue("status")) if err != nil { @@ -39,31 +46,39 @@ func TestHealth(t *testing.T) { time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。 return nil }) - r.Delete("/users", func(ctx *web.Context) web.Responser { - status, err := strconv.Atoi(ctx.Request().FormValue("status")) - if err != nil { - panic(err) - } - time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。 - return web.Status(status) - }) + + a.Length(plugin.store.All(), 3). + Length(plugin.States(), 3) defer servertest.Run(a, s)() defer s.Close(0) - state := h.store.Get(r.Name(), http.MethodGet, "/") + state := plugin.store.Get(r.Name(), http.MethodGet, "/") a.Equal(0, state.Count) // 第一次访问 GET / servertest.Get(a, "http://localhost:8080/").Query("status", "200").Do(nil).Status(200) time.Sleep(time.Microsecond * 500) - state = h.store.Get(r.Name(), http.MethodGet, "/") + state = plugin.store.Get(r.Name(), http.MethodGet, "/") a.Equal(1, state.Count).True(state.Min > 0) + // 中途添加的 API + r.Delete("/users", func(ctx *web.Context) web.Responser { + status, err := strconv.Atoi(ctx.Request().FormValue("status")) + if err != nil { + panic(err) + } + time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。 + return web.Status(status) + }) + + a.Length(plugin.store.All(), 4). + Length(plugin.States(), 4) + // 第二次访问 GET / servertest.Get(a, "http://localhost:8080/").Query("status", "500").Do(nil) time.Sleep(time.Microsecond * 500) - state = h.store.Get(r.Name(), http.MethodGet, "/") + state = plugin.store.Get(r.Name(), http.MethodGet, "/") a.Equal(2, state.Count). Equal(1, state.ServerErrors). Equal(0, state.UserErrors) @@ -71,19 +86,19 @@ func TestHealth(t *testing.T) { // 第一次访问 POST / servertest.NewRequest(a, http.MethodPost, "http://localhost:8080/").Query("status", "201").Do(nil) time.Sleep(time.Microsecond * 500) - state = h.store.Get(r.Name(), http.MethodPost, "/") + state = plugin.store.Get(r.Name(), http.MethodPost, "/") a.Equal(1, state.Count) // 第一次访问 DELETE /users servertest.Delete(a, "http://localhost:8080/users").Query("status", "401").Do(nil) time.Sleep(time.Microsecond * 500) - state = h.store.Get(r.Name(), http.MethodDelete, "/users") + state = plugin.store.Get(r.Name(), http.MethodDelete, "/users") a.Equal(1, state.Count). Equal(0, state.ServerErrors). Equal(1, state.UserErrors). - Length(h.States(), 3) + Length(plugin.States(), 4) - for _, s := range h.States() { + for _, s := range plugin.States() { println(s.Router, s.Method, s.Pattern) } } diff --git a/plugins/health/store.go b/plugins/health/store.go index f4886cb..d9c8205 100644 --- a/plugins/health/store.go +++ b/plugins/health/store.go @@ -16,7 +16,7 @@ import ( type Store interface { // Get 获取指定 API 的数据 // - // 如果还不存在,则应该将只有 route、method 和 pattern 不为空的 [State] 对象写入当前接口并返回。 + // 如果还不存在,则应该将只有 route、method 和 pattern 不为空的 [State] 对象写入 [Store] 并返回。 Get(route, method, pattern string) *State // Save 保存数据内容