Skip to content

Commit

Permalink
fix(plugins/health): 修正初始化数据时的错误判断
Browse files Browse the repository at this point in the history
Store.Get 返回值永远不会为空。其包含的内容不会被执行。
  • Loading branch information
caixw committed May 27, 2024
1 parent 84ee537 commit eae1897
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
6 changes: 2 additions & 4 deletions plugins/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
Expand Down
51 changes: 33 additions & 18 deletions plugins/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,51 +46,59 @@ 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)

// 第一次访问 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)
}
}
2 changes: 1 addition & 1 deletion plugins/health/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 保存数据内容
Expand Down

0 comments on commit eae1897

Please sign in to comment.