Skip to content

Commit

Permalink
Merge pull request pingcap#618 from pingcap/siddontang/multi-key-test
Browse files Browse the repository at this point in the history
localstore: add multi key inc test.
  • Loading branch information
ngaut committed Nov 23, 2015
2 parents d53371a + f786698 commit e482896
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions store/localstore/isolation_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package localstore_test

import (
"fmt"
"sync"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/kv"
)

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

var _ = Suite(&testIsolationSuite{})

type testIsolationSuite struct {
Expand Down Expand Up @@ -48,3 +54,55 @@ func (t *testIsolationSuite) TestInc(c *C) {

wg.Wait()
}

func (t *testIsolationSuite) TestMultiInc(c *C) {
store, err := tidb.NewStore("memory://test/test_isolation")
defer store.Close()

threadCnt := 4
incCnt := 2000
keyCnt := 4

keys := make([][]byte, 0, keyCnt)
for i := 0; i < keyCnt; i++ {
keys = append(keys, []byte(fmt.Sprintf("test_key_%d", i)))
}

var wg sync.WaitGroup

wg.Add(threadCnt)
for i := 0; i < threadCnt; i++ {
go func() {
defer wg.Done()
for j := 0; j < incCnt; j++ {
err = kv.RunInNewTxn(store, true, func(txn kv.Transaction) error {
for _, key := range keys {
_, err := txn.Inc(key, 1)
if err != nil {
return err
}
}

return nil
})
c.Assert(err, IsNil)
}
}()
}

wg.Wait()

for i := 0; i < keyCnt; i++ {
err = kv.RunInNewTxn(store, false, func(txn kv.Transaction) error {
for _, key := range keys {
id, err := txn.GetInt64(key)
if err != nil {
return err
}
c.Assert(id, Equals, int64(threadCnt*incCnt))
}
return nil
})
c.Assert(err, IsNil)
}
}

0 comments on commit e482896

Please sign in to comment.