Skip to content

Commit

Permalink
util/bitmap: migrate test-infra to testify (pingcap#26312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ateb14 authored Jul 19, 2021
1 parent 2f028b3 commit ac83504
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
27 changes: 11 additions & 16 deletions util/bitmap/concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ import (
"sync/atomic"
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/assert"
)

func TestT(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testBitmap{})
func TestConcurrentBitmapSet(t *testing.T) {
t.Parallel()

type testBitmap struct{}

func (s *testBitmap) TestConcurrentBitmapSet(c *C) {
const loopCount = 1000
const interval = 2

Expand All @@ -46,24 +40,26 @@ func (s *testBitmap) TestConcurrentBitmapSet(c *C) {

for i := 0; i < loopCount; i++ {
if i%interval == 0 {
c.Assert(bm.UnsafeIsSet(i), IsTrue)
assert.Equal(t, true, bm.UnsafeIsSet(i))
} else {
c.Assert(bm.UnsafeIsSet(i), IsFalse)
assert.Equal(t, false, bm.UnsafeIsSet(i))
}
}
}

// TestConcurrentBitmapUniqueSetter checks if isSetter is unique everytime
// when a bit is set.
func (s *testBitmap) TestConcurrentBitmapUniqueSetter(c *C) {
func TestConcurrentBitmapUniqueSetter(t *testing.T) {
t.Parallel()

const loopCount = 10000
const competitorsPerSet = 50

wg := &sync.WaitGroup{}
bm := NewConcurrentBitmap(32)
var setterCounter uint64
var clearCounter uint64
// Concurrently set bit, and check if isSetter count matchs zero clearing count.
// Concurrently set bit, and check if isSetter count matches zero clearing count.
for i := 0; i < loopCount; i++ {
// Clear bitmap to zero.
if atomic.CompareAndSwapUint32(&(bm.segments[0]), 0x00000001, 0x00000000) {
Expand All @@ -81,7 +77,6 @@ func (s *testBitmap) TestConcurrentBitmapUniqueSetter(c *C) {
}
}
wg.Wait()
// If clearCounter is too big, it means setter concurrency of this test is not enough.
c.Assert(clearCounter < loopCount, Equals, true)
c.Assert(setterCounter, Equals, clearCounter+1)
assert.Less(t, clearCounter, uint64(loopCount))
assert.Equal(t, setterCounter, clearCounter+1)
}
26 changes: 26 additions & 0 deletions util/bitmap/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package bitmap

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
goleak.VerifyTestMain(m)
}

0 comments on commit ac83504

Please sign in to comment.