diff --git a/dm/dm/master/openapi_test.go b/dm/dm/master/openapi_test.go index 9638136fa99..453a485bdce 100644 --- a/dm/dm/master/openapi_test.go +++ b/dm/dm/master/openapi_test.go @@ -93,10 +93,11 @@ func (t *openAPISuite) TestRedirectRequestToLeader(c *check.C) { s1 := NewServer(cfg1) c.Assert(s1.Start(ctx), check.IsNil) defer s1.Close() + defer cancel() // wait the first one become the leader c.Assert(utils.WaitSomething(30, 100*time.Millisecond, func() bool { - return s1.election.IsLeader() + return s1.election.IsLeader() && s1.scheduler.Started() }), check.IsTrue) // join to an existing cluster @@ -113,6 +114,12 @@ func (t *openAPISuite) TestRedirectRequestToLeader(c *check.C) { s2 := NewServer(cfg2) c.Assert(s2.Start(ctx), check.IsNil) defer s2.Close() + defer cancel() // this cancel must call before s.Close() to avoid deadlock + + // wait the second master ready + c.Assert(utils.WaitSomething(30, 100*time.Millisecond, func() bool { + return s2.election.IsLeader() + }), check.IsFalse) baseURL := "/api/v1/sources" // list source from leader @@ -128,7 +135,6 @@ func (t *openAPISuite) TestRedirectRequestToLeader(c *check.C) { // list source not from leader will get a redirect result2 := testutil.NewRequest().Get(baseURL).GoWithHTTPHandler(t.testT, s2.openapiHandles) c.Assert(result2.Code(), check.Equals, http.StatusTemporaryRedirect) - cancel() } func (t *openAPISuite) TestOpenAPIWillNotStartInDefaultConfig(c *check.C) { @@ -151,7 +157,7 @@ func (t *openAPISuite) TestOpenAPIWillNotStartInDefaultConfig(c *check.C) { }), check.IsTrue) c.Assert(s1.openapiHandles, check.IsNil) defer s1.Close() - cancel() + defer cancel() } func (t *openAPISuite) TestSourceAPI(c *check.C) {