Skip to content

Commit

Permalink
ddl: fix some unit test funcion that can't run independently (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Dec 29, 2021
1 parent 7e2d12c commit c205b01
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
29 changes: 29 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/rand"
"strings"
"sync/atomic"
"testing"
"time"

. "github.com/pingcap/check"
Expand All @@ -44,6 +45,7 @@ import (
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
ntestkit "github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/admin"
"github.com/pingcap/tidb/util/collate"
Expand Down Expand Up @@ -3523,3 +3525,30 @@ func (s *testSerialDBSuite1) TestAddPartitionReplicaBiggerThanTiFlashStores(c *C
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:-1]DDL job rollback, error msg: [ddl] add partition wait for tiflash replica to complete")
}

func TestDropAndTruncatePartition(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := ntestkit.CreateMockStore(t)
defer clean()

ddl.ExportTestDropAndTruncatePartition(t)
}

func TestTable(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := ntestkit.CreateMockStore(t)
defer clean()

ddl.ExportTestTable(t)
}

func TestRenameTables(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := ntestkit.CreateMockStore(t)
defer clean()

ddl.ExportTestRenameTables(t)
}
2 changes: 1 addition & 1 deletion ddl/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestDropAndTruncatePartition(t *testing.T) {
func ExportTestDropAndTruncatePartition(t *testing.T) {
store := testCreateStoreT(t, "test_store")
d, err := testNewDDLAndStart(
context.Background(),
Expand Down
4 changes: 2 additions & 2 deletions ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func testGetTableWithError(d *ddl, schemaID, tableID int64) (table.Table, error)
return tbl, nil
}

func TestTable(t *testing.T) {
func ExportTestTable(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
ddl, err := testNewDDLAndStart(
Expand Down Expand Up @@ -346,7 +346,7 @@ func testAlterNoCacheTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSche
return job
}

func TestRenameTables(t *testing.T) {
func ExportTestRenameTables(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
ddl, err := testNewDDLAndStart(
Expand Down
27 changes: 19 additions & 8 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"io"
"bytes"
"fmt"
"math/rand"
Expand Down Expand Up @@ -344,13 +345,20 @@ func (n *numa) worker(wg *sync.WaitGroup, ch chan task) {
defer wg.Done()
for t := range ch {
start := time.Now()
if err := n.runTestCase(t.pkg, t.test, t.old); err != nil {
fmt.Println("run test case error", t.pkg, t.test, t.old, time.Since(start), err)
res := n.runTestCase(t.pkg, t.test, t.old)
if res.err != nil {
fmt.Println("[FAIL] ", t.pkg, t.test, t.old, time.Since(start), res.err)
io.Copy(os.Stderr, &res.output)
}
}
}

func (n *numa) runTestCase(pkg string, fn string, old bool) error {
type testResult struct {
err error
output bytes.Buffer
}

func (n *numa) runTestCase(pkg string, fn string, old bool) (res testResult) {
exe := "./" + testFileName(pkg)
var cmd *exec.Cmd
if n.numactl {
Expand All @@ -359,12 +367,13 @@ func (n *numa) runTestCase(pkg string, fn string, old bool) error {
cmd = n.testCommand(exe, fn, old)
}
cmd.Dir = path.Join(workDir, pkg)
_, err := cmd.CombinedOutput()
if err != nil {
// fmt.Println("run test case error", pkg, fn, string(output))
return err
// Combine the test case output, so the run result for failed cases can be displayed.
cmd.Stdout = &res.output
cmd.Stderr = &res.output
if err := cmd.Run(); err != nil {
res.err = withTrace(err)
}
return nil
return res
}

func (n *numa) testCommandWithNumaCtl(exe string, fn string, old bool) *exec.Cmd {
Expand Down Expand Up @@ -415,6 +424,8 @@ func buildTestBinary(pkg string) error {
// go test -c
cmd := exec.Command("go", "test", "-c", "-vet", "off", "-o", testFileName(pkg))
cmd.Dir = path.Join(workDir, pkg)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return withTrace(err)
}
Expand Down

0 comments on commit c205b01

Please sign in to comment.