Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Sep 22, 2023
1 parent e3e346a commit 8ba64b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pkg/utils/syncutil/flexible_wait_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ func (fwg *FlexibleWaitGroup) Wait() {
}
fwg.Unlock()
}

// getCount returns the current count of the FlexibleWaitGroup.
// It is only used for testing.
func (fwg *FlexibleWaitGroup) getCount() int {
fwg.Lock()
defer fwg.Unlock()
return fwg.count
}
12 changes: 6 additions & 6 deletions pkg/utils/syncutil/flexible_wait_group_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 TiKV Project Authors.
// Copyright 2023 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,14 +24,14 @@ import (
func TestFlexibleWaitGroup(t *testing.T) {
re := require.New(t)
fwg := NewFlexibleWaitGroup()
now := time.Now()
for i := 20; i >= 0; i-- {
fwg.Add(1)
go func(i int) {
defer fwg.Done()
time.Sleep(time.Millisecond * time.Duration(i*50))
}(i)
}
now := time.Now()
fwg.Wait()
re.GreaterOrEqual(time.Since(now).Milliseconds(), int64(1000))
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestNegativeDelta(t *testing.T) {
fwg.Done()
}()
fwg.Wait()
require.Equal(0, fwg.count)
require.Equal(0, fwg.getCount())
}

// TestMultipleWait tests the case where Wait is called multiple times concurrently.
Expand All @@ -108,7 +108,7 @@ func TestMultipleWait(t *testing.T) {
}()
<-done
<-done
require.Equal(0, fwg.count)
require.Equal(0, fwg.getCount())
}

// TestAddAfterWaitFinished tests the case where Add is called after Wait has finished.
Expand All @@ -126,7 +126,7 @@ func TestAddAfterWaitFinished(t *testing.T) {
}()
<-done
fwg.Add(1)
require.Equal(1, fwg.count)
require.Equal(1, fwg.getCount())
fwg.Done()
require.Equal(0, fwg.count)
require.Equal(0, fwg.getCount())
}

0 comments on commit 8ba64b8

Please sign in to comment.