Skip to content

Commit

Permalink
close #4483: fix failpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB committed Dec 22, 2021
1 parent dd14b0b commit 9fef69a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions pkg/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ func NewSelfProtector(s *server.Server) negroni.Handler {

func (protector *selfProtector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
handler := protector.s.GetSelfProtectionHandler()
if handler == nil || handler.HandleHTTPSelfProtection(r) {
failpoint.Inject("addSelfProtectionHTTPHeader", func() {
w.Header().Add("self-protection", "ok")
})

failpoint.Inject("addSelfProtectionHTTPHeader", func() {
w.Header().Add("self-protection", "ok")
})

if handler == nil || handler.HandleHTTPSelfProtection(r) {
next(w, r)
} else {
// current plan will only deny request when over the speed limit
Expand Down
2 changes: 1 addition & 1 deletion tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func (s *testSelfProtectorSuite) TearDownSuite(c *C) {
}

func (s *testSelfProtectorSuite) TestSelfProtect(c *C) {
c.Assert(failpoint.Enable("github.com/tikv/pd/pkg/apiutil/serverapi/addSelfProtectionHTTPHeader", "return(true)"), IsNil)
leader := s.cluster.GetServer(s.cluster.GetLeader())
header := mustRequestSuccess(c, leader.GetServer())
c.Assert(failpoint.Enable("github.com/tikv/pd/pkg/apiutil/serverapi/addSelfProtectionHTTPHeader", `return(true)`), IsNil)
c.Assert(header.Get("self-protection"), Equals, "ok")
c.Assert(failpoint.Disable("github.com/tikv/pd/pkg/apiutil/serverapi/addSelfProtectionHTTPHeader"), IsNil)
}
Expand Down

0 comments on commit 9fef69a

Please sign in to comment.