Skip to content

Commit

Permalink
test(util): fix routinepool test
Browse files Browse the repository at this point in the history
close: cubefs#3211

Signed-off-by: NaturalSelect <[email protected]>
  • Loading branch information
NaturalSelect authored and NaturalSelect committed Mar 8, 2024
1 parent 714536e commit 9203f55
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions util/routinepool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package routinepool
package routinepool_test

import (
"runtime"
"sync"
"testing"

"github.com/cubefs/cubefs/util/routinepool"
"github.com/stretchr/testify/require"
)

func TestExample(t *testing.T) {
func TestRoutinePool(t *testing.T) {
maxRoutineNum := 10
pool := NewRoutinePool(maxRoutineNum)
pool := routinepool.NewRoutinePool(maxRoutineNum)

var max int32 = 0
totalRoutineNum := 100
Expand All @@ -31,23 +35,22 @@ func TestExample(t *testing.T) {

for i := 0; i < totalRoutineNum; i++ {
pool.Submit(func() {
currentNum := pool.RunningNum()
lock.Lock()
value++
if currentNum > max {
max = currentNum
for i := 0; i < 200; i++ {
currentNum := pool.RunningNum()
if currentNum > max {
max = currentNum
}
runtime.Gosched()
}
lock.Unlock()
})
}

pool.WaitAndClose()

if max != int32(maxRoutineNum) {
t.Fatalf("expected max running num(%v), got(%v)", maxRoutineNum, max)
}
require.EqualValues(t, max, maxRoutineNum)

if value != totalRoutineNum {
t.Fatalf("expected value(%v), got(%v)", totalRoutineNum, value)
}
require.EqualValues(t, totalRoutineNum, value)
}

0 comments on commit 9203f55

Please sign in to comment.