Skip to content

Commit b4fab68

Browse files
committed
*: simplify WaitUntil usage (tikv#4613)
* *: simplify WaitUntil usage ref tikv#4399 Signed-off-by: disksing <[email protected]> * minor fix Signed-off-by: disksing <[email protected]>
1 parent ca5e3e1 commit b4fab68

18 files changed

+48
-48
lines changed

client/option_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *testClientSuite) TestDynamicOptionChange(c *C) {
4747
expectBool := true
4848
o.setEnableTSOFollowerProxy(expectBool)
4949
// Check the value changing notification.
50-
testutil.WaitUntil(c, func(c *C) bool {
50+
testutil.WaitUntil(c, func() bool {
5151
<-o.enableTSOFollowerProxyCh
5252
return true
5353
})

pkg/mock/mockhbstream/mockhbstream_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func (s *testHeartbeatStreamSuite) TestActivity(c *C) {
6666

6767
// Active stream is stream1.
6868
hbs.BindStream(1, stream1)
69-
testutil.WaitUntil(c, func(c *C) bool {
69+
testutil.WaitUntil(c, func() bool {
7070
hbs.SendMsg(region, proto.Clone(msg).(*pdpb.RegionHeartbeatResponse))
7171
return stream1.Recv() != nil && stream2.Recv() == nil
7272
})
7373
// Rebind to stream2.
7474
hbs.BindStream(1, stream2)
75-
testutil.WaitUntil(c, func(c *C) bool {
75+
testutil.WaitUntil(c, func() bool {
7676
hbs.SendMsg(region, proto.Clone(msg).(*pdpb.RegionHeartbeatResponse))
7777
return stream1.Recv() == nil && stream2.Recv() != nil
7878
})
@@ -83,7 +83,7 @@ func (s *testHeartbeatStreamSuite) TestActivity(c *C) {
8383
c.Assert(res.GetHeader().GetError(), NotNil)
8484
// Switch back to 1 again.
8585
hbs.BindStream(1, stream1)
86-
testutil.WaitUntil(c, func(c *C) bool {
86+
testutil.WaitUntil(c, func() bool {
8787
hbs.SendMsg(region, proto.Clone(msg).(*pdpb.RegionHeartbeatResponse))
8888
return stream1.Recv() != nil && stream2.Recv() == nil
8989
})

pkg/testutil/testutil.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131

3232
// CheckFunc is a condition checker that passed to WaitUntil. Its implementation
3333
// may call c.Fatal() to abort the test, or c.Log() to add more information.
34-
type CheckFunc func(c *check.C) bool
34+
type CheckFunc func() bool
3535

3636
// WaitOp represents available options when execute WaitUntil
3737
type WaitOp struct {
@@ -63,7 +63,7 @@ func WaitUntil(c *check.C, f CheckFunc, opts ...WaitOption) {
6363
opt(option)
6464
}
6565
for i := 0; i < option.retryTimes; i++ {
66-
if f(c) {
66+
if f() {
6767
return
6868
}
6969
time.Sleep(option.sleepInterval)

server/api/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func mustNewCluster(c *C, num int, opts ...func(cfg *config.Config)) ([]*config.
123123
}
124124

125125
func mustWaitLeader(c *C, svrs []*server.Server) {
126-
testutil.WaitUntil(c, func(c *C) bool {
126+
testutil.WaitUntil(c, func() bool {
127127
var leader *pdpb.Member
128128
for _, svr := range svrs {
129129
l := svr.GetLeader()

server/api/tso_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *testTsoSuite) TearDownSuite(c *C) {
4848
}
4949

5050
func (s *testTsoSuite) TestTransferAllocator(c *C) {
51-
testutil.WaitUntil(c, func(c *C) bool {
51+
testutil.WaitUntil(c, func() bool {
5252
s.svr.GetTSOAllocatorManager().ClusterDCLocationChecker()
5353
_, err := s.svr.GetTSOAllocatorManager().GetAllocator("dc-1")
5454
return err == nil

server/cluster/coordinator_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ func BenchmarkPatrolRegion(b *testing.B) {
894894
}
895895

896896
func waitOperator(c *C, co *coordinator, regionID uint64) {
897-
testutil.WaitUntil(c, func(c *C) bool {
897+
testutil.WaitUntil(c, func() bool {
898898
return co.opController.GetOperator(regionID) != nil
899899
})
900900
}
@@ -1206,7 +1206,7 @@ func (s *testScheduleControllerSuite) TestInterval(c *C) {
12061206

12071207
func waitAddLearner(c *C, stream mockhbstream.HeartbeatStream, region *core.RegionInfo, storeID uint64) *core.RegionInfo {
12081208
var res *pdpb.RegionHeartbeatResponse
1209-
testutil.WaitUntil(c, func(c *C) bool {
1209+
testutil.WaitUntil(c, func() bool {
12101210
if res = stream.Recv(); res != nil {
12111211
return res.GetRegionId() == region.GetID() &&
12121212
res.GetChangePeer().GetChangeType() == eraftpb.ConfChangeType_AddLearnerNode &&
@@ -1222,7 +1222,7 @@ func waitAddLearner(c *C, stream mockhbstream.HeartbeatStream, region *core.Regi
12221222

12231223
func waitPromoteLearner(c *C, stream mockhbstream.HeartbeatStream, region *core.RegionInfo, storeID uint64) *core.RegionInfo {
12241224
var res *pdpb.RegionHeartbeatResponse
1225-
testutil.WaitUntil(c, func(c *C) bool {
1225+
testutil.WaitUntil(c, func() bool {
12261226
if res = stream.Recv(); res != nil {
12271227
return res.GetRegionId() == region.GetID() &&
12281228
res.GetChangePeer().GetChangeType() == eraftpb.ConfChangeType_AddNode &&
@@ -1239,7 +1239,7 @@ func waitPromoteLearner(c *C, stream mockhbstream.HeartbeatStream, region *core.
12391239

12401240
func waitRemovePeer(c *C, stream mockhbstream.HeartbeatStream, region *core.RegionInfo, storeID uint64) *core.RegionInfo {
12411241
var res *pdpb.RegionHeartbeatResponse
1242-
testutil.WaitUntil(c, func(c *C) bool {
1242+
testutil.WaitUntil(c, func() bool {
12431243
if res = stream.Recv(); res != nil {
12441244
return res.GetRegionId() == region.GetID() &&
12451245
res.GetChangePeer().GetChangeType() == eraftpb.ConfChangeType_RemoveNode &&
@@ -1255,7 +1255,7 @@ func waitRemovePeer(c *C, stream mockhbstream.HeartbeatStream, region *core.Regi
12551255

12561256
func waitTransferLeader(c *C, stream mockhbstream.HeartbeatStream, region *core.RegionInfo, storeID uint64) *core.RegionInfo {
12571257
var res *pdpb.RegionHeartbeatResponse
1258-
testutil.WaitUntil(c, func(c *C) bool {
1258+
testutil.WaitUntil(c, func() bool {
12591259
if res = stream.Recv(); res != nil {
12601260
if res.GetRegionId() == region.GetID() {
12611261
for _, peer := range append(res.GetTransferLeader().GetPeers(), res.GetTransferLeader().GetPeer()) {
@@ -1273,7 +1273,7 @@ func waitTransferLeader(c *C, stream mockhbstream.HeartbeatStream, region *core.
12731273
}
12741274

12751275
func waitNoResponse(c *C, stream mockhbstream.HeartbeatStream) {
1276-
testutil.WaitUntil(c, func(c *C) bool {
1276+
testutil.WaitUntil(c, func() bool {
12771277
res := stream.Recv()
12781278
return res == nil
12791279
})

server/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestMain(m *testing.M) {
4343

4444
func mustWaitLeader(c *C, svrs []*Server) *Server {
4545
var leader *Server
46-
testutil.WaitUntil(c, func(c *C) bool {
46+
testutil.WaitUntil(c, func() bool {
4747
for _, s := range svrs {
4848
if !s.IsClosed() && s.member.IsLeader() {
4949
leader = s

tests/client/client_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *clientTestSuite) TestClientLeaderChange(c *C) {
9090
cli := setupCli(c, s.ctx, endpoints)
9191

9292
var ts1, ts2 uint64
93-
testutil.WaitUntil(c, func(c *C) bool {
93+
testutil.WaitUntil(c, func() bool {
9494
p1, l1, err := cli.GetTS(context.TODO())
9595
if err == nil {
9696
ts1 = tsoutil.ComposeTS(p1, l1)
@@ -111,7 +111,7 @@ func (s *clientTestSuite) TestClientLeaderChange(c *C) {
111111
waitLeader(c, cli.(client), cluster.GetServer(leader).GetConfig().ClientUrls)
112112

113113
// Check TS won't fall back after leader changed.
114-
testutil.WaitUntil(c, func(c *C) bool {
114+
testutil.WaitUntil(c, func() bool {
115115
p2, l2, err := cli.GetTS(context.TODO())
116116
if err == nil {
117117
ts2 = tsoutil.ComposeTS(p2, l2)
@@ -140,7 +140,7 @@ func (s *clientTestSuite) TestLeaderTransfer(c *C) {
140140
cli := setupCli(c, s.ctx, endpoints)
141141

142142
var lastTS uint64
143-
testutil.WaitUntil(c, func(c *C) bool {
143+
testutil.WaitUntil(c, func() bool {
144144
physical, logical, err := cli.GetTS(context.TODO())
145145
if err == nil {
146146
lastTS = tsoutil.ComposeTS(physical, logical)
@@ -217,7 +217,7 @@ func (s *clientTestSuite) TestTSOAllocatorLeader(c *C) {
217217
var allocatorLeaderMap = make(map[string]string)
218218
for _, dcLocation := range dcLocationConfig {
219219
var pdName string
220-
testutil.WaitUntil(c, func(c *C) bool {
220+
testutil.WaitUntil(c, func() bool {
221221
pdName = cluster.WaitAllocatorLeader(dcLocation)
222222
return len(pdName) > 0
223223
})
@@ -423,7 +423,7 @@ func (s *clientTestSuite) TestGetTsoFromFollowerClient1(c *C) {
423423

424424
c.Assert(failpoint.Enable("github.com/tikv/pd/client/unreachableNetwork", "return(true)"), IsNil)
425425
var lastTS uint64
426-
testutil.WaitUntil(c, func(c *C) bool {
426+
testutil.WaitUntil(c, func() bool {
427427
physical, logical, err := cli.GetTS(context.TODO())
428428
if err == nil {
429429
lastTS = tsoutil.ComposeTS(physical, logical)
@@ -451,7 +451,7 @@ func (s *clientTestSuite) TestGetTsoFromFollowerClient2(c *C) {
451451

452452
c.Assert(failpoint.Enable("github.com/tikv/pd/client/unreachableNetwork", "return(true)"), IsNil)
453453
var lastTS uint64
454-
testutil.WaitUntil(c, func(c *C) bool {
454+
testutil.WaitUntil(c, func() bool {
455455
physical, logical, err := cli.GetTS(context.TODO())
456456
if err == nil {
457457
lastTS = tsoutil.ComposeTS(physical, logical)
@@ -506,7 +506,7 @@ func setupCli(c *C, ctx context.Context, endpoints []string, opts ...pd.ClientOp
506506
}
507507

508508
func waitLeader(c *C, cli client, leader string) {
509-
testutil.WaitUntil(c, func(c *C) bool {
509+
testutil.WaitUntil(c, func() bool {
510510
cli.ScheduleCheckLeader()
511511
return cli.GetLeaderAddr() == leader
512512
})
@@ -721,7 +721,7 @@ func (s *testClientSuite) TestGetRegion(c *C) {
721721
err := s.regionHeartbeat.Send(req)
722722
c.Assert(err, IsNil)
723723

724-
testutil.WaitUntil(c, func(c *C) bool {
724+
testutil.WaitUntil(c, func() bool {
725725
r, err := s.client.GetRegion(context.Background(), []byte("a"))
726726
c.Assert(err, IsNil)
727727
if r == nil {
@@ -759,7 +759,7 @@ func (s *testClientSuite) TestGetPrevRegion(c *C) {
759759
}
760760
time.Sleep(500 * time.Millisecond)
761761
for i := 0; i < 20; i++ {
762-
testutil.WaitUntil(c, func(c *C) bool {
762+
testutil.WaitUntil(c, func() bool {
763763
r, err := s.client.GetPrevRegion(context.Background(), []byte{byte(i)})
764764
c.Assert(err, IsNil)
765765
if i > 0 && i < regionLen {
@@ -798,7 +798,7 @@ func (s *testClientSuite) TestScanRegions(c *C) {
798798
}
799799

800800
// Wait for region heartbeats.
801-
testutil.WaitUntil(c, func(c *C) bool {
801+
testutil.WaitUntil(c, func() bool {
802802
scanRegions, err := s.client.ScanRegions(context.Background(), []byte{0}, nil, 10)
803803
return err == nil && len(scanRegions) == 10
804804
})
@@ -865,7 +865,7 @@ func (s *testClientSuite) TestGetRegionByID(c *C) {
865865
err := s.regionHeartbeat.Send(req)
866866
c.Assert(err, IsNil)
867867

868-
testutil.WaitUntil(c, func(c *C) bool {
868+
testutil.WaitUntil(c, func() bool {
869869
r, err := s.client.GetRegionByID(context.Background(), regionID)
870870
c.Assert(err, IsNil)
871871
if r == nil {
@@ -1146,7 +1146,7 @@ func (s *testClientSuite) TestScatterRegion(c *C) {
11461146
err := s.regionHeartbeat.Send(req)
11471147
regionsID := []uint64{regionID}
11481148
c.Assert(err, IsNil)
1149-
testutil.WaitUntil(c, func(c *C) bool {
1149+
testutil.WaitUntil(c, func() bool {
11501150
scatterResp, err := s.client.ScatterRegions(context.Background(), regionsID, pd.WithGroup("test"), pd.WithRetry(1))
11511151
if c.Check(err, NotNil) {
11521152
return false

tests/cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (c *TestCluster) WaitAllLeaders(testC *check.C, dcLocations map[string]stri
611611
for _, dcLocation := range dcLocations {
612612
wg.Add(1)
613613
go func(dc string) {
614-
testutil.WaitUntil(testC, func(testC *check.C) bool {
614+
testutil.WaitUntil(testC, func() bool {
615615
leaderName := c.WaitAllocatorLeader(dc)
616616
return leaderName != ""
617617
})

tests/pdctl/member/member_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (s *memberTestSuite) TestMember(c *C) {
7474
args = []string{"-u", pdAddr, "member", "leader", "transfer", "pd2"}
7575
_, err = pdctl.ExecuteCommand(cmd, args...)
7676
c.Assert(err, IsNil)
77-
testutil.WaitUntil(c, func(c *C) bool {
77+
testutil.WaitUntil(c, func() bool {
7878
return c.Check("pd2", Equals, svr.GetLeader().GetName())
7979
})
8080

@@ -84,7 +84,7 @@ func (s *memberTestSuite) TestMember(c *C) {
8484
output, err = pdctl.ExecuteCommand(cmd, args...)
8585
c.Assert(strings.Contains(string(output), "Success"), IsTrue)
8686
c.Assert(err, IsNil)
87-
testutil.WaitUntil(c, func(c *C) bool {
87+
testutil.WaitUntil(c, func() bool {
8888
return c.Check("pd2", Not(Equals), svr.GetLeader().GetName())
8989
})
9090

tests/server/api/api_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (s *serverTestSuite) TestReconnect(c *C) {
8787
// Make sure they proxy requests to the new leader.
8888
for name, s := range cluster.GetServers() {
8989
if name != leader {
90-
testutil.WaitUntil(c, func(c *C) bool {
90+
testutil.WaitUntil(c, func() bool {
9191
res, e := http.Get(s.GetConfig().AdvertiseClientUrls + "/pd/api/v1/version")
9292
c.Assert(e, IsNil)
9393
defer res.Body.Close()
@@ -103,7 +103,7 @@ func (s *serverTestSuite) TestReconnect(c *C) {
103103
// Request will fail with no leader.
104104
for name, s := range cluster.GetServers() {
105105
if name != leader && name != newLeader {
106-
testutil.WaitUntil(c, func(c *C) bool {
106+
testutil.WaitUntil(c, func() bool {
107107
res, err := http.Get(s.GetConfig().AdvertiseClientUrls + "/pd/api/v1/version")
108108
c.Assert(err, IsNil)
109109
defer res.Body.Close()

tests/server/member/member_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (s *memberTestSuite) TestMemberDelete(c *C) {
111111
httpClient := &http.Client{Timeout: 15 * time.Second}
112112
for _, t := range table {
113113
c.Log(time.Now(), "try to delete:", t.path)
114-
testutil.WaitUntil(c, func(c *C) bool {
114+
testutil.WaitUntil(c, func() bool {
115115
addr := leader.GetConfig().ClientUrls + "/pd/api/v1/members/" + t.path
116116
req, err := http.NewRequest(http.MethodDelete, addr, nil)
117117
c.Assert(err, IsNil)
@@ -187,21 +187,21 @@ func (s *memberTestSuite) TestLeaderPriority(c *C) {
187187
server1 := cluster.GetServer(leader1)
188188
addr := server1.GetConfig().ClientUrls
189189
// PD leader should sync with etcd leader.
190-
testutil.WaitUntil(c, func(c *C) bool {
190+
testutil.WaitUntil(c, func() bool {
191191
return cluster.GetLeader() == leader1
192192
})
193193
// Bind a lower priority to current leader.
194194
s.post(c, addr+"/pd/api/v1/members/name/"+leader1, `{"leader-priority": -1}`)
195195
// Wait etcd leader change.
196196
leader2 := s.waitEtcdLeaderChange(c, server1, leader1)
197197
// PD leader should sync with etcd leader again.
198-
testutil.WaitUntil(c, func(c *C) bool {
198+
testutil.WaitUntil(c, func() bool {
199199
return cluster.GetLeader() == leader2
200200
})
201201
}
202202

203203
func (s *memberTestSuite) post(c *C, url string, body string) {
204-
testutil.WaitUntil(c, func(c *C) bool {
204+
testutil.WaitUntil(c, func() bool {
205205
res, err := http.Post(url, "", bytes.NewBufferString(body)) // #nosec
206206
c.Assert(err, IsNil)
207207
b, err := io.ReadAll(res.Body)
@@ -214,7 +214,7 @@ func (s *memberTestSuite) post(c *C, url string, body string) {
214214

215215
func (s *memberTestSuite) waitEtcdLeaderChange(c *C, server *tests.TestServer, old string) string {
216216
var leader string
217-
testutil.WaitUntil(c, func(c *C) bool {
217+
testutil.WaitUntil(c, func() bool {
218218
var err error
219219
leader, err = server.GetEtcdLeader()
220220
if err != nil {
@@ -271,7 +271,7 @@ func (s *memberTestSuite) TestLeaderResignWithBlock(c *C) {
271271

272272
func (s *memberTestSuite) waitLeaderChange(c *C, cluster *tests.TestCluster, old string) string {
273273
var leader string
274-
testutil.WaitUntil(c, func(c *C) bool {
274+
testutil.WaitUntil(c, func() bool {
275275
leader = cluster.GetLeader()
276276
if leader == old || leader == "" {
277277
return false
@@ -383,7 +383,7 @@ func (s *leaderTestSuite) sendRequest(c *C, addr string) {
383383

384384
func mustWaitLeader(c *C, svrs []*server.Server) *server.Server {
385385
var leader *server.Server
386-
testutil.WaitUntil(c, func(c *C) bool {
386+
testutil.WaitUntil(c, func() bool {
387387
for _, s := range svrs {
388388
if !s.IsClosed() && s.GetMember().IsLeader() {
389389
leader = s

tests/server/region_syncer/region_syncer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (s *regionSyncerTestSuite) TestRegionSyncer(c *C) {
140140
c.Assert(followerServer, NotNil)
141141
cacheRegions := leaderServer.GetServer().GetBasicCluster().GetRegions()
142142
c.Assert(cacheRegions, HasLen, regionLen)
143-
testutil.WaitUntil(c, func(c *C) bool {
143+
testutil.WaitUntil(c, func() bool {
144144
for _, region := range cacheRegions {
145145
r := followerServer.GetServer().GetBasicCluster().GetRegion(region.GetID())
146146
if !(c.Check(r.GetMeta(), DeepEquals, region.GetMeta()) &&

tests/server/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (s *serverTestSuite) TestLeader(c *C) {
138138

139139
err = cluster.GetServer(leader1).Stop()
140140
c.Assert(err, IsNil)
141-
testutil.WaitUntil(c, func(c *C) bool {
141+
testutil.WaitUntil(c, func() bool {
142142
leader := cluster.GetLeader()
143143
return leader != leader1
144144
})

tests/server/tso/allocator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (s *testAllocatorSuite) TestPriorityAndDifferentLocalTSO(c *C) {
156156
c.Assert(err, IsNil)
157157
dcLocationConfig["pd4"] = "dc-4"
158158
cluster.CheckClusterDCLocation()
159-
testutil.WaitUntil(c, func(c *C) bool {
159+
testutil.WaitUntil(c, func() bool {
160160
leaderName := cluster.WaitAllocatorLeader("dc-4")
161161
return leaderName != ""
162162
})
@@ -180,7 +180,7 @@ func (s *testAllocatorSuite) TestPriorityAndDifferentLocalTSO(c *C) {
180180
for serverName, dcLocation := range dcLocationConfig {
181181
go func(serName, dc string) {
182182
defer wg.Done()
183-
testutil.WaitUntil(c, func(c *C) bool {
183+
testutil.WaitUntil(c, func() bool {
184184
leaderName := cluster.WaitAllocatorLeader(dc)
185185
return leaderName == serName
186186
}, testutil.WithRetryTimes(12), testutil.WithSleepInterval(5*time.Second))
@@ -231,7 +231,7 @@ func (s *testAllocatorSuite) testTSOSuffix(c *C, cluster *tests.TestCluster, am
231231
allocator, err := am.GetAllocator(dcLocation)
232232
c.Assert(err, IsNil)
233233
var tso pdpb.Timestamp
234-
testutil.WaitUntil(c, func(c *C) bool {
234+
testutil.WaitUntil(c, func() bool {
235235
tso, err = allocator.GenerateTSO(1)
236236
c.Assert(err, IsNil)
237237
return tso.GetPhysical() != 0

0 commit comments

Comments
 (0)