Skip to content

Commit

Permalink
fix unstable test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyfhust committed Jan 6, 2024
1 parent 4c7102c commit 7681bfc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
24 changes: 9 additions & 15 deletions pkg/ddl/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,11 @@ func TestAlterAddConstraintStateChange1(t *testing.T) {
tk1.MustExec("use test")
tk1.MustExec("insert into t values(12)")

var checkErr error
d := dom.DDL()
originalCallback := d.GetHook()
callback := &callback.TestDDLCallback{}
// StatNone -> StateWriteOnly
onJobUpdatedExportedFunc1 := func(job *model.Job) {
if checkErr != nil {
return
}
originalCallback.OnChanged(nil)
if job.SchemaState == model.StateWriteOnly {
// set constraint state
Expand Down Expand Up @@ -174,15 +170,11 @@ func TestAlterAddConstraintStateChange2(t *testing.T) {
tk1.MustExec("use test")
tk1.MustExec("insert into t values(12)")

var checkErr error
d := dom.DDL()
originalCallback := d.GetHook()
callback := &callback.TestDDLCallback{}
// StateWriteOnly -> StateWriteReorganization
onJobUpdatedExportedFunc2 := func(job *model.Job) {
if checkErr != nil {
return
}
originalCallback.OnChanged(nil)
if job.SchemaState == model.StateWriteReorganization {
// set constraint state
Expand Down Expand Up @@ -217,13 +209,13 @@ func TestAlterAddConstraintStateChange3(t *testing.T) {
tk1.MustExec("use test")
tk1.MustExec("insert into t values(12)")

var checkErr error
addCheckDone := false
d := dom.DDL()
originalCallback := d.GetHook()
callback := &callback.TestDDLCallback{}
// StateWriteReorganization -> StatePublic
onJobUpdatedExportedFunc3 := func(job *model.Job) {
if checkErr != nil {
if job.Type != model.ActionAddCheckConstraint || job.TableName != "t" {
return
}
originalCallback.OnChanged(nil)
Expand All @@ -239,13 +231,19 @@ func TestAlterAddConstraintStateChange3(t *testing.T) {
// recover
tableCommon.Constraints[0].State = model.StatePublic
tableCommon.WritableConstraint()
addCheckDone = true
}
}
callback.OnJobUpdatedExported.Store(&onJobUpdatedExportedFunc3)
d.SetHook(callback)
tk.MustExec("alter table t add constraint c3 check ( a > 10)")
// Issue TiDB#48123.
time.Sleep(50 * time.Millisecond)
for i := 0; i <= 100; i++ {
if addCheckDone {
break
}
time.Sleep(10 * time.Millisecond)
}
tk.MustQuery("select * from t").Check(testkit.Rows("12"))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL,\nCONSTRAINT `c3` CHECK ((`a` > 10))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}
Expand All @@ -261,15 +259,11 @@ func TestAlterEnforcedConstraintStateChange(t *testing.T) {
tk1.MustExec("use test")
tk1.MustExec("insert into t values(12)")

var checkErr error
d := dom.DDL()
originalCallback := d.GetHook()
callback := &callback.TestDDLCallback{}
// StateWriteReorganization -> StatePublic
onJobUpdatedExportedFunc3 := func(job *model.Job) {
if checkErr != nil {
return
}
originalCallback.OnChanged(nil)
if job.SchemaState == model.StateWriteReorganization {
// set constraint state
Expand Down
7 changes: 5 additions & 2 deletions pkg/ddl/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func TestIndexChange(t *testing.T) {
publicTable table.Table
)
onJobUpdatedExportedFunc := func(job *model.Job) {
if job.Type != model.ActionAddIndex || job.TableName != "t" {
return
}
if job.SchemaState == prevState {
return
}
Expand Down Expand Up @@ -87,11 +90,11 @@ func TestIndexChange(t *testing.T) {
// We need to make sure onJobUpdated is called in the first hook.
// After testCreateIndex(), onJobUpdated() may not be called when job.state is Sync.
// If we skip this check, prevState may wrongly set to StatePublic.
for i := 0; i <= 10; i++ {
for i := 0; i <= 100; i++ {
if addIndexDone {
break
}
time.Sleep(50 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
}
v := getSchemaVer(t, tk.Session())
checkHistoryJobArgs(t, tk.Session(), jobID.Load(), &historyJobArgs{ver: v, tbl: publicTable.Meta()})
Expand Down

0 comments on commit 7681bfc

Please sign in to comment.