Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: migrate test-infra to testify for isolation_test.go #29018

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 116 additions & 39 deletions session/isolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ package session_test

import (
"sync"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)

type testIsolationSuite struct {
testSessionSuiteBase
}

/*
These test cases come from the paper <A Critique of ANSI SQL Isolation Levels>.
The sign 'P0', 'P1'.... can be found in the paper. These cases will run under snapshot isolation.
*/
func (s *testIsolationSuite) TestP0DirtyWrite(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestP0DirtyWrite(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand All @@ -43,7 +48,7 @@ func (s *testIsolationSuite) TestP0DirtyWrite(c *C) {
session2.MustExec("update x set c = c+1 where id = 1;")
session1.MustExec("commit;")
_, err := session2.Exec("commit;")
c.Assert(err, NotNil)
require.Error(t, err)

session1.MustExec("set tidb_txn_mode = 'pessimistic'")
session2.MustExec("set tidb_txn_mode = 'pessimistic'")
Expand Down Expand Up @@ -86,9 +91,17 @@ func (s *testIsolationSuite) TestP0DirtyWrite(c *C) {
session2.MustQuery("select * from x").Check(testkit.Rows("1 3"))
}

func (s *testIsolationSuite) TestP1DirtyRead(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestP1DirtyRead(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand Down Expand Up @@ -130,9 +143,17 @@ func (s *testIsolationSuite) TestP1DirtyRead(c *C) {
session2.MustExec("commit;")
}

func (s *testIsolationSuite) TestP2NonRepeatableRead(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestP2NonRepeatableRead(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
Expand Down Expand Up @@ -195,9 +216,17 @@ func (s *testIsolationSuite) TestP2NonRepeatableRead(c *C) {
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestP3Phantom(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestP3Phantom(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists z;")
Expand Down Expand Up @@ -257,9 +286,17 @@ func (s *testIsolationSuite) TestP3Phantom(c *C) {
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestP4LostUpdate(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestP4LostUpdate(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand All @@ -273,7 +310,7 @@ func (s *testIsolationSuite) TestP4LostUpdate(c *C) {
session2.MustExec("commit;")
session1.MustExec("update x set c = c+1 where id = 1;")
_, err := session1.Exec("commit;")
c.Assert(err, NotNil)
require.Error(t, err)

session1.MustExec("set tidb_txn_mode = 'pessimistic'")
session2.MustExec("set tidb_txn_mode = 'pessimistic'")
Expand Down Expand Up @@ -310,11 +347,19 @@ func (s *testIsolationSuite) TestP4LostUpdate(c *C) {
}

// cursor is not supported
func (s *testIsolationSuite) TestP4CLostUpdate(c *C) {}
func TestP4CLostUpdate(t *testing.T) {}

func TestA3Phantom(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

func (s *testIsolationSuite) TestA3Phantom(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand Down Expand Up @@ -359,9 +404,17 @@ func (s *testIsolationSuite) TestA3Phantom(c *C) {
session2.MustExec("commit;")
}

func (s *testIsolationSuite) TestA5AReadSkew(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestA5AReadSkew(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
Expand Down Expand Up @@ -418,9 +471,17 @@ func (s *testIsolationSuite) TestA5AReadSkew(c *C) {
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestA5BWriteSkew(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestA5BWriteSkew(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
Expand Down Expand Up @@ -509,9 +570,17 @@ func (s *testIsolationSuite) TestA5BWriteSkew(c *C) {
These test cases come from the paper <Highly Available Transactions: Virtues and Limitations>
for tidb, we support read-after-write on cluster level.
*/
func (s *testIsolationSuite) TestReadAfterWrite(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestReadAfterWrite(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand Down Expand Up @@ -556,9 +625,17 @@ func (s *testIsolationSuite) TestReadAfterWrite(c *C) {
/*
This case will do harm in Innodb, even if in snapshot isolation, but harmless in tidb.
*/
func (s *testIsolationSuite) TestPhantomReadInInnodb(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)
func TestPhantomReadInInnodb(t *testing.T) {
t.Parallel()

s := createTestSuite(func(err error) { require.NoError(t, err) }, nil)
defer s.cleanSuite(func(err error) { require.NoError(t, err) })

session1 := testkit.NewTestKit(t, s.store)
session2 := testkit.NewTestKit(t, s.store)

session1.MustExec("use test")
session2.MustExec("use test")

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
Expand Down
60 changes: 36 additions & 24 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ var _ = Suite(&testSessionSuite{})
var _ = Suite(&testSessionSuite2{})
var _ = Suite(&testSessionSuite3{})
var _ = Suite(&testSchemaSuite{})
var _ = Suite(&testIsolationSuite{})
var _ = SerialSuites(&testSchemaSerialSuite{})
var _ = SerialSuites(&testSessionSerialSuite{})
var _ = SerialSuites(&testBackupRestoreSuite{})
Expand Down Expand Up @@ -183,21 +182,10 @@ func clearETCD(ebd kv.EtcdBackend) error {
return nil
}

func initPdAddrs() {
initPdAddrsOnce.Do(func() {
addrs := strings.Split(*pdAddrs, ",")
pdAddrChan = make(chan string, len(addrs))
for _, addr := range addrs {
addr = strings.TrimSpace(addr)
if addr != "" {
pdAddrChan <- addr
}
}
})
}

func (s *testSessionSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()
func createTestSuite(assert func(error), s *testSessionSuiteBase) *testSessionSuiteBase {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary work around, will clean up after session tests migrated.

if s == nil {
s = new(testSessionSuiteBase)
}

if *withTiKV {
initPdAddrs()
Expand All @@ -207,11 +195,11 @@ func (s *testSessionSuiteBase) SetUpSuite(c *C) {
conf.TxnLocalLatches.Enabled = false
})
store, err := d.Open(fmt.Sprintf("tikv://%s?disableGC=true", s.pdAddr))
c.Assert(err, IsNil)
assert(err)
err = clearStorage(store)
c.Assert(err, IsNil)
assert(err)
err = clearETCD(store.(kv.EtcdBackend))
c.Assert(err, IsNil)
assert(err)
session.ResetStoreForWithTiKVTest(store)
s.store = store
} else {
Expand All @@ -221,25 +209,49 @@ func (s *testSessionSuiteBase) SetUpSuite(c *C) {
s.cluster = c
}),
)
c.Assert(err, IsNil)
assert(err)
s.store = store
session.DisableStats4Test()
}

var err error
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
assert(err)

return s
}

func (s *testSessionSuiteBase) TearDownSuite(c *C) {
func initPdAddrs() {
initPdAddrsOnce.Do(func() {
addrs := strings.Split(*pdAddrs, ",")
pdAddrChan = make(chan string, len(addrs))
for _, addr := range addrs {
addr = strings.TrimSpace(addr)
if addr != "" {
pdAddrChan <- addr
}
}
})
}

func (s *testSessionSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()
createTestSuite(func(err error) { c.Assert(err, IsNil) }, s)
}

func (s *testSessionSuiteBase) cleanSuite(assert func(error)) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
assert(s.store.Close())
if *withTiKV {
pdAddrChan <- s.pdAddr
}
}

func (s *testSessionSuiteBase) TearDownSuite(c *C) {
defer testleak.AfterTest(c)()
s.cleanSuite(func(err error) { c.Assert(err, IsNil) })
}

func (s *testSessionSuiteBase) TearDownTest(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
r := tk.MustQuery("show full tables")
Expand Down