Skip to content

Commit

Permalink
test: fix TestRedirectWithMetrics data race (#7640)
Browse files Browse the repository at this point in the history
close #7639

Signed-off-by: husharp <jinhao.hu@pingcap.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
HuSharp and ti-chi-bot[bot] authored Dec 29, 2023
1 parent b4a73b3 commit cf22c04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 8 additions & 0 deletions client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,11 @@ func (c *client) request(ctx context.Context, reqInfo *requestInfo, headerOpts .
func (c *client) UpdateMembersInfo() {
c.inner.updateMembersInfo(c.inner.ctx)
}

// setLeaderAddrIdx sets the index of the leader address in the inner client.
// only used for testing.
func (c *client) setLeaderAddrIdx(idx int) {
c.inner.Lock()
defer c.inner.Unlock()
c.inner.leaderAddrIdx = idx
}
17 changes: 8 additions & 9 deletions client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestRedirectWithMetrics(t *testing.T) {
re.NoError(err)
failureCnt.Write(&out)
re.Equal(float64(3), out.Counter.GetValue())
c.Close()

// 2. Test the Leader success, just need to send to leader.
httpClient = newHTTPClientWithRequestChecker(func(req *http.Request) error {
Expand All @@ -133,15 +134,15 @@ func TestRedirectWithMetrics(t *testing.T) {
}
return nil
})
c.(*client).inner.cli = httpClient
// Force to update members info.
c.(*client).inner.leaderAddrIdx = 0
c.(*client).inner.pdAddrs = pdAddrs
c = NewClient("test-http-pd-redirect", pdAddrs, WithHTTPClient(httpClient), WithMetrics(metricCnt, nil))
// force to update members info.
c.(*client).setLeaderAddrIdx(0)
c.CreateScheduler(context.Background(), "test", 0)
successCnt, err := c.(*client).inner.requestCounter.GetMetricWithLabelValues([]string{createSchedulerName, ""}...)
re.NoError(err)
successCnt.Write(&out)
re.Equal(float64(1), out.Counter.GetValue())
c.Close()

// 3. Test when the leader fails, needs to be sent to the follower in order,
// and returns directly if one follower succeeds
Expand All @@ -152,10 +153,9 @@ func TestRedirectWithMetrics(t *testing.T) {
}
return nil
})
c.(*client).inner.cli = httpClient
// Force to update members info.
c.(*client).inner.leaderAddrIdx = 0
c.(*client).inner.pdAddrs = pdAddrs
c = NewClient("test-http-pd-redirect", pdAddrs, WithHTTPClient(httpClient), WithMetrics(metricCnt, nil))
// force to update members info.
c.(*client).setLeaderAddrIdx(0)
c.CreateScheduler(context.Background(), "test", 0)
successCnt, err = c.(*client).inner.requestCounter.GetMetricWithLabelValues([]string{createSchedulerName, ""}...)
re.NoError(err)
Expand All @@ -167,6 +167,5 @@ func TestRedirectWithMetrics(t *testing.T) {
failureCnt.Write(&out)
// leader failure
re.Equal(float64(4), out.Counter.GetValue())

c.Close()
}

0 comments on commit cf22c04

Please sign in to comment.