From c5ab3f2c939b2eae6b497f6efa9c380c059fd684 Mon Sep 17 00:00:00 2001 From: wph95 Date: Sat, 26 Oct 2019 23:24:48 +0800 Subject: [PATCH 1/3] added test --- executor/ddl_test.go | 10 ++-- executor/executor_test.go | 8 +-- executor/plugin_executor.go | 3 -- plugin/csv/csv.go | 1 - plugin/plugin.go | 23 +++++++-- plugin/plugin_engine_test.go | 96 ++++++++++++++++++++++++++++++++++++ plugin/test_engine.go | 65 ++++++++++++++++++++++++ 7 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 plugin/plugin_engine_test.go create mode 100644 plugin/test_engine.go diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 3e009fed443aa..ae2790338aad0 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -16,11 +16,6 @@ package executor_test import ( "context" "fmt" - "math" - "strconv" - "strings" - "time" - . "github.com/pingcap/check" "github.com/pingcap/failpoint" "github.com/pingcap/parser/model" @@ -41,6 +36,10 @@ import ( "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/testkit" "github.com/pingcap/tidb/util/testutil" + "math" + "strconv" + "strings" + "time" ) func (s *testSuite6) TestTruncateTable(c *C) { @@ -1005,3 +1004,4 @@ func (s *testSuite6) TestTimestampMinDefaultValue(c *C) { tk.MustExec("create table tdv(a int);") tk.MustExec("ALTER TABLE tdv ADD COLUMN ts timestamp DEFAULT '1970-01-01 08:00:01';") } + diff --git a/executor/executor_test.go b/executor/executor_test.go index 0bdca4d6acd4d..8c7392ea6ddc0 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -3948,9 +3948,7 @@ func (s *testSuite6) TearDownTest(c *C) { } } -type testSuite7 struct { - *baseTestSuite -} + func (s *testSuite7) TearDownTest(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -5053,3 +5051,7 @@ func (s *testSuiteP2) TestPointUpdatePreparedPlanWithCommitMode(c *C) { tk2.MustQuery("select * from t where a = 3").Check(testkit.Rows("3 3 11")) } + +type testSuite7 struct { + *baseTestSuite +} diff --git a/executor/plugin_executor.go b/executor/plugin_executor.go index a2f4c8de5ad46..d551d459db236 100644 --- a/executor/plugin_executor.go +++ b/executor/plugin_executor.go @@ -1,8 +1,6 @@ package executor import ( - "fmt" - "github.com/davecgh/go-spew/spew" "github.com/pingcap/parser/model" "github.com/pingcap/tidb/plugin" "github.com/pingcap/tidb/util/chunk" @@ -30,7 +28,6 @@ func (e *PluginScanExecutor) Open(ctx context.Context) error { func (e *PluginScanExecutor) Next(ctx context.Context, chk *chunk.Chunk) error { chk.Reset() err := e.pm.OnReaderNext(ctx, chk, e.meta) - fmt.Println("pe next finished", spew.Sdump(err)) return err } diff --git a/plugin/csv/csv.go b/plugin/csv/csv.go index 45062362f85bf..4ce992da1f23f 100644 --- a/plugin/csv/csv.go +++ b/plugin/csv/csv.go @@ -52,7 +52,6 @@ func OnReaderOpen(ctx context.Context, meta *plugin.ExecutorMeta) { func OnReaderNext(ctx context.Context, chk *chunk.Chunk, meta *plugin.ExecutorMeta) error { if _, ok := Files[meta.Table.Name.L]; !ok { - fmt.Println("have some problem") return nil } e := Files[meta.Table.Name.L] diff --git a/plugin/plugin.go b/plugin/plugin.go index 8a63d59af8cb0..056e25019c9cb 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -384,20 +384,37 @@ func Shutdown(ctx context.Context) { } } +// Get finds and returns plugin by kind and name parameters. +var TestP map[Kind][]Plugin + +func Set(kind Kind, p Plugin) { + if TestP == nil { + TestP = make(map[Kind][]Plugin) + } + TestP[kind] = append(TestP[kind], p) +} + // Get finds and returns plugin by kind and name parameters. func Get(kind Kind, name string) *Plugin { plugins := pluginGlobal.plugins() - if plugins == nil { - return nil + if plugins != nil { + for _, p := range plugins.plugins[kind] { + if p.Name == name { + return &p + } + } } - for _, p := range plugins.plugins[kind] { + + for _, p := range TestP[kind] { if p.Name == name { return &p } } + return nil } + func List(kind Kind) []Plugin { plugins := pluginGlobal.plugins() if plugins == nil { diff --git a/plugin/plugin_engine_test.go b/plugin/plugin_engine_test.go new file mode 100644 index 0000000000000..4b8ef631a548b --- /dev/null +++ b/plugin/plugin_engine_test.go @@ -0,0 +1,96 @@ +package plugin_test + +import ( + "flag" + . "github.com/pingcap/check" + "github.com/pingcap/parser" + "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/plugin" + "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/store/mockstore" + "github.com/pingcap/tidb/store/mockstore/mocktikv" + "github.com/pingcap/tidb/util/mock" + "github.com/pingcap/tidb/util/testkit" + "testing" + "unsafe" +) + +type baseTestSuite struct { + cluster *mocktikv.Cluster + mvccStore mocktikv.MVCCStore + store kv.Storage + domain *domain.Domain + *parser.Parser + ctx *mock.Context +} + +var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test") + +func (s *baseTestSuite) SetUpSuite(c *C) { + s.Parser = parser.New() + flag.Lookup("mockTikv") + useMockTikv := *mockTikv + if useMockTikv { + s.cluster = mocktikv.NewCluster() + mocktikv.BootstrapWithSingleStore(s.cluster) + s.mvccStore = mocktikv.MustNewMVCCStore() + store, err := mockstore.NewMockTikvStore( + mockstore.WithCluster(s.cluster), + mockstore.WithMVCCStore(s.mvccStore), + ) + c.Assert(err, IsNil) + s.store = store + session.SetSchemaLease(0) + session.DisableStats4Test() + } + d, err := session.BootstrapSession(s.store) + c.Assert(err, IsNil) + d.SetStatsUpdating(true) + s.domain = d +} + +func (s *baseTestSuite) TearDownSuite(c *C) { + s.domain.Close() + s.store.Close() +} + +var _ = Suite(&testPlugin{&baseTestSuite{}}) + +type testPlugin struct{ *baseTestSuite } + +func TestPlugin(t *testing.T) { + //rescueStdout := os.Stdout + //_, w, _ := os.Pipe() + //os.Stdout = w + // + //os.Stdout = rescueStdout + // + //runConf := RunConf{Output: rescueStdout, Verbose: true, KeepWorkDir: true} + TestingT(t) +} + +func (s *testPlugin) TestPlugin(c *C) { + tk := testkit.NewTestKit(c, s.store) + manifest := plugin.EngineManifest{ + Manifest: plugin.Manifest{ + Name: "csv", + }, + OnReaderOpen: plugin.OnReaderOpen, + OnReaderNext: plugin.OnReaderNext, + //OnReaderClose: plugin.OnReaderClose, + } + plugin.Set(plugin.Engine, plugin.Plugin{ + Manifest: (*plugin.Manifest)(unsafe.Pointer(&manifest)), + Path: "", + Disabled: 0, + State: 0, + }) + + tk.MustExec("use test") + tk.MustExec("create table t1(a int, b char(255)) ENGINE = csv") + result := tk.MustQuery("select * from t1") + result.Check(testkit.Rows("0 233333", "1 233333", "2 233333", "3 233333", "4 233333", "5 233333")) + result = tk.MustQuery("select * from t1 where a = 2") + result.Check(testkit.Rows("2 233333", )) +} diff --git a/plugin/test_engine.go b/plugin/test_engine.go new file mode 100644 index 0000000000000..ad122eb09400f --- /dev/null +++ b/plugin/test_engine.go @@ -0,0 +1,65 @@ +// Copyright 2019 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 plugin + +import ( + "context" + "fmt" + "github.com/pingcap/tidb/util/chunk" +) + +type ReadExecutor struct { + pos int +} + +var Files = make(map[string]*ReadExecutor) + +// Validate implements TiDB plugin's Validate SPI. +func Validate(ctx context.Context, m *Manifest) error { + fmt.Println("csv plugin validate") + return nil +} + +// OnInit implements TiDB plugin's OnInit SPI. +func OnInit(ctx context.Context, manifest *Manifest) error { + fmt.Println("csv init called") + return nil +} + +// OnShutdown implements TiDB plugin's OnShutdown SPI. +func OnShutdown(ctx context.Context, manifest *Manifest) error { + fmt.Println("csv shutdown called") + return nil +} + +func OnReaderOpen(ctx context.Context, meta *ExecutorMeta) { + Files[meta.Table.Name.L] = &ReadExecutor{ + pos: 0, + } +} + +func OnReaderNext(ctx context.Context, chk *chunk.Chunk, meta *ExecutorMeta) error { + if _, ok := Files[meta.Table.Name.L]; !ok { + fmt.Println("have some problem") + return nil + } + e := Files[meta.Table.Name.L] + if e.pos > 5 { + return nil + } + chk.AppendInt64(0, int64(e.pos)) + chk.AppendString(1, "233333") + e.pos += 1 + return nil +} From e2dbdc00a8ff4d8b8a96f173c13bcb695ab5f3a3 Mon Sep 17 00:00:00 2001 From: liufengkai Date: Sun, 27 Oct 2019 01:08:56 +0800 Subject: [PATCH 2/3] feature: add udf test. --- plugin/plugin.go | 27 ++++-- plugin/plugin_engine_test.go | 9 +- plugin/udftrim/udftrim_test.go | 155 +++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 11 deletions(-) create mode 100644 plugin/udftrim/udftrim_test.go diff --git a/plugin/plugin.go b/plugin/plugin.go index 056e25019c9cb..3c0e5a6fc41b2 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -387,11 +387,11 @@ func Shutdown(ctx context.Context) { // Get finds and returns plugin by kind and name parameters. var TestP map[Kind][]Plugin -func Set(kind Kind, p Plugin) { +func Set(kind Kind, p *Plugin) { if TestP == nil { TestP = make(map[Kind][]Plugin) } - TestP[kind] = append(TestP[kind], p) + TestP[kind] = append(TestP[kind], *p) } // Get finds and returns plugin by kind and name parameters. @@ -414,7 +414,6 @@ func Get(kind Kind, name string) *Plugin { return nil } - func List(kind Kind) []Plugin { plugins := pluginGlobal.plugins() if plugins == nil { @@ -426,11 +425,27 @@ func List(kind Kind) []Plugin { // ForeachPlugin loops all ready plugins. func ForeachPlugin(kind Kind, fn func(plugin *Plugin) error) error { plugins := pluginGlobal.plugins() + var pluginsMap map[Kind][]Plugin if plugins == nil { - return nil + pluginsMap = TestP + + if TestP == nil { + return nil + } + } else { + pluginsMap = plugins.plugins + for k, v := range TestP { + pluginsMap[k] = v + } } - for i := range plugins.plugins[kind] { - p := &plugins.plugins[kind][i] + + var strs []string + for _, v := range pluginsMap[UDF] { + strs = append(strs, v.Name) + } + logutil.BgLogger().Info("Fuck PluginsMap", zap.Any("type", kind), zap.Strings("map", strs)) + for i := range pluginsMap[kind] { + p := &pluginsMap[kind][i] if p.State != Ready { continue } diff --git a/plugin/plugin_engine_test.go b/plugin/plugin_engine_test.go index 4b8ef631a548b..0726f1243a41a 100644 --- a/plugin/plugin_engine_test.go +++ b/plugin/plugin_engine_test.go @@ -13,7 +13,6 @@ import ( "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/testkit" "testing" - "unsafe" ) type baseTestSuite struct { @@ -72,7 +71,7 @@ func TestPlugin(t *testing.T) { func (s *testPlugin) TestPlugin(c *C) { tk := testkit.NewTestKit(c, s.store) - manifest := plugin.EngineManifest{ + manifest := &plugin.EngineManifest{ Manifest: plugin.Manifest{ Name: "csv", }, @@ -80,11 +79,11 @@ func (s *testPlugin) TestPlugin(c *C) { OnReaderNext: plugin.OnReaderNext, //OnReaderClose: plugin.OnReaderClose, } - plugin.Set(plugin.Engine, plugin.Plugin{ - Manifest: (*plugin.Manifest)(unsafe.Pointer(&manifest)), + plugin.Set(plugin.Engine, &plugin.Plugin{ + Manifest: plugin.ExportManifest(manifest), Path: "", Disabled: 0, - State: 0, + State: plugin.Ready, }) tk.MustExec("use test") diff --git a/plugin/udftrim/udftrim_test.go b/plugin/udftrim/udftrim_test.go new file mode 100644 index 0000000000000..5b66c39df6a47 --- /dev/null +++ b/plugin/udftrim/udftrim_test.go @@ -0,0 +1,155 @@ +package main + +import ( + "database/sql" + "fmt" + "github.com/go-sql-driver/mysql" + . "github.com/pingcap/check" + "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/metrics" + "github.com/pingcap/tidb/plugin" + . "github.com/pingcap/tidb/server" + "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/store/mockstore" + "github.com/pingcap/tidb/util/testkit" + "go.uber.org/zap" + "io/ioutil" + "log" + "net/http" + "testing" + "time" +) + +var defaultDSNConfig = mysql.Config{ + User: "root", + Net: "tcp", + Addr: "127.0.0.1:4001", + DBName: "test", + Strict: true, +} + +type configOverrider func(*mysql.Config) +type TidbTestSuite struct { + tidbdrv *TiDBDriver + server *Server + domain *domain.Domain + store kv.Storage +} + +var suite = new(TidbTestSuite) +var _ = Suite(suite) + +func (ts *TidbTestSuite) SetUpSuite(c *C) { + manifest := &plugin.UDFManifest{ + Manifest: plugin.Manifest{ + Kind: plugin.UDF, + Name: "udftrim", + Description: "UDF Expression Release", + Version: 1, + RequireVersion: map[string]uint16{}, + License: "", + BuildTime: "2019-10-26 23:41:34.229509 +0800 CST m=+0.001093220", + Validate: Validate, + OnInit: OnInit, + OnShutdown: OnShutdown, + }, + GetUserDefinedFuncClass: GetUserDefinedFuncClass, + } + + plugin.Set(plugin.UDF, &plugin.Plugin{ + Manifest: plugin.ExportManifest(manifest), + Path: "", + Disabled: 0, + State: plugin.Ready, + }) + + metrics.RegisterMetrics() + var err error + ts.store, err = mockstore.NewMockTikvStore() + session.DisableStats4Test() + c.Assert(err, IsNil) + ts.domain, err = session.BootstrapSession(ts.store) + c.Assert(err, IsNil) + ts.tidbdrv = NewTiDBDriver(ts.store) + cfg := config.NewConfig() + cfg.Port = 4001 + cfg.Status.ReportStatus = true + cfg.Status.StatusPort = 10090 + cfg.Performance.TCPKeepAlive = true + + server, err := NewServer(cfg, ts.tidbdrv) + c.Assert(err, IsNil) + ts.server = server + go ts.server.Run() + waitUntilServerOnline(cfg.Status.StatusPort) +} + +// getDSN generates a DSN string for MySQL connection. +func getDSN(overriders ...configOverrider) string { + var config = defaultDSNConfig + for _, overrider := range overriders { + if overrider != nil { + overrider(&config) + } + } + return config.FormatDSN() +} + +const retryTime = 100 + +func waitUntilServerOnline(statusPort uint) { + // connect server + retry := 0 + for ; retry < retryTime; retry++ { + time.Sleep(time.Millisecond * 10) + db, err := sql.Open("mysql", getDSN()) + if err == nil { + db.Close() + break + } + } + if retry == retryTime { + log.Fatal("failed to connect DB in every 10 ms", zap.Int("retryTime", retryTime)) + } + // connect http status + statusURL := fmt.Sprintf("http://127.0.0.1:%d/status", statusPort) + for retry = 0; retry < retryTime; retry++ { + resp, err := http.Get(statusURL) + if err == nil { + ioutil.ReadAll(resp.Body) + resp.Body.Close() + break + } + time.Sleep(time.Millisecond * 10) + } + if retry == retryTime { + log.Fatal("failed to connect HTTP status in every 10 ms", zap.Int("retryTime", retryTime)) + } +} + +func (s *TidbTestSuite) TearDownSuite(c *C) { + s.domain.Close() + s.store.Close() +} + +var _ = Suite(&testPlugin{&TidbTestSuite{}}) + +type testPlugin struct{ *TidbTestSuite } + +func TestPlugin(t *testing.T) { + TestingT(t) +} + +func (s *testPlugin) TestPlugin(c *C) { + tk := testkit.NewTestKit(c, s.store) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b char(255))") + tk.MustExec(`insert into t values(1, " lfkdsk "),(2, " lfk dsk ")`) + tk.MustExec("analyze table t") + result := tk.MustQuery("select UDF_TRIM(b) from t where a = 1") + result.Check(testkit.Rows("lfkdsk", )) +} From e56bf962ae6f3d351cf64b6c8c2f6e56b375c6f5 Mon Sep 17 00:00:00 2001 From: liufengkai Date: Sun, 27 Oct 2019 01:11:14 +0800 Subject: [PATCH 3/3] refact csv test. --- plugin/csv/csv_test.go | 123 +++++++++++++++++++++++------------ plugin/plugin_engine_test.go | 95 --------------------------- plugin/test_engine.go | 65 ------------------ 3 files changed, 82 insertions(+), 201 deletions(-) delete mode 100644 plugin/plugin_engine_test.go delete mode 100644 plugin/test_engine.go diff --git a/plugin/csv/csv_test.go b/plugin/csv/csv_test.go index 21c3428e56cde..f03db342872cb 100644 --- a/plugin/csv/csv_test.go +++ b/plugin/csv/csv_test.go @@ -1,54 +1,95 @@ -// Copyright 2019 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 main_test +package main import ( - "context" - + "flag" + . "github.com/pingcap/check" + "github.com/pingcap/parser" + "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/plugin" - "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/store/mockstore" + "github.com/pingcap/tidb/store/mockstore/mocktikv" + "github.com/pingcap/tidb/util/mock" + "github.com/pingcap/tidb/util/testkit" + "testing" ) -func LoadRunShutdownPluginExample() { - ctx := context.Background() - var pluginVarNames []string - cfg := plugin.Config{ - Plugins: []string{"csv-1"}, - PluginDir: "/home/go/src/github.com/pingcap/tidb/plugin/csv", - GlobalSysVar: &variable.SysVars, - PluginVarNames: &pluginVarNames, - } +type baseTestSuite struct { + cluster *mocktikv.Cluster + mvccStore mocktikv.MVCCStore + store kv.Storage + domain *domain.Domain + *parser.Parser + ctx *mock.Context +} - err := plugin.Load(ctx, cfg) - if err != nil { - panic(err) +var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test") + +func (s *baseTestSuite) SetUpSuite(c *C) { + s.Parser = parser.New() + flag.Lookup("mockTikv") + useMockTikv := *mockTikv + if useMockTikv { + s.cluster = mocktikv.NewCluster() + mocktikv.BootstrapWithSingleStore(s.cluster) + s.mvccStore = mocktikv.MustNewMVCCStore() + store, err := mockstore.NewMockTikvStore( + mockstore.WithCluster(s.cluster), + mockstore.WithMVCCStore(s.mvccStore), + ) + c.Assert(err, IsNil) + s.store = store + session.SetSchemaLease(0) + session.DisableStats4Test() } + d, err := session.BootstrapSession(s.store) + c.Assert(err, IsNil) + d.SetStatsUpdating(true) + s.domain = d +} + +func (s *baseTestSuite) TearDownSuite(c *C) { + s.domain.Close() + s.store.Close() +} - // load and start TiDB domain. +var _ = Suite(&testPlugin{&baseTestSuite{}}) - err = plugin.Init(ctx, cfg) - if err != nil { - panic(err) - } +type testPlugin struct{ *baseTestSuite } - err = plugin.ForeachPlugin(plugin.Audit, func(auditPlugin *plugin.Plugin) error { - plugin.DeclareAuditManifest(auditPlugin.Manifest).OnGeneralEvent(context.Background(), nil, plugin.Log, "QUERY") - return nil - }) - if err != nil { - panic(err) +func TestPlugin(t *testing.T) { + //rescueStdout := os.Stdout + //_, w, _ := os.Pipe() + //os.Stdout = w + // + //os.Stdout = rescueStdout + // + //runConf := RunConf{Output: rescueStdout, Verbose: true, KeepWorkDir: true} + TestingT(t) +} + +func (s *testPlugin) TestPlugin(c *C) { + tk := testkit.NewTestKit(c, s.store) + manifest := &plugin.EngineManifest{ + Manifest: plugin.Manifest{ + Name: "csv", + }, + OnReaderOpen: OnReaderOpen, + OnReaderNext: OnReaderNext, + //OnReaderClose: plugin.OnReaderClose, } + plugin.Set(plugin.Engine, &plugin.Plugin{ + Manifest: plugin.ExportManifest(manifest), + Path: "", + Disabled: 0, + State: plugin.Ready, + }) - plugin.Shutdown(context.Background()) + tk.MustExec("use test") + tk.MustExec("create table t1(a int, b char(255)) ENGINE = csv") + result := tk.MustQuery("select * from t1") + result.Check(testkit.Rows("0 233333", "1 233333", "2 233333", "3 233333", "4 233333", "5 233333")) + result = tk.MustQuery("select * from t1 where a = 2") + result.Check(testkit.Rows("2 233333", )) } diff --git a/plugin/plugin_engine_test.go b/plugin/plugin_engine_test.go deleted file mode 100644 index 0726f1243a41a..0000000000000 --- a/plugin/plugin_engine_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package plugin_test - -import ( - "flag" - . "github.com/pingcap/check" - "github.com/pingcap/parser" - "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/plugin" - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/store/mockstore" - "github.com/pingcap/tidb/store/mockstore/mocktikv" - "github.com/pingcap/tidb/util/mock" - "github.com/pingcap/tidb/util/testkit" - "testing" -) - -type baseTestSuite struct { - cluster *mocktikv.Cluster - mvccStore mocktikv.MVCCStore - store kv.Storage - domain *domain.Domain - *parser.Parser - ctx *mock.Context -} - -var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test") - -func (s *baseTestSuite) SetUpSuite(c *C) { - s.Parser = parser.New() - flag.Lookup("mockTikv") - useMockTikv := *mockTikv - if useMockTikv { - s.cluster = mocktikv.NewCluster() - mocktikv.BootstrapWithSingleStore(s.cluster) - s.mvccStore = mocktikv.MustNewMVCCStore() - store, err := mockstore.NewMockTikvStore( - mockstore.WithCluster(s.cluster), - mockstore.WithMVCCStore(s.mvccStore), - ) - c.Assert(err, IsNil) - s.store = store - session.SetSchemaLease(0) - session.DisableStats4Test() - } - d, err := session.BootstrapSession(s.store) - c.Assert(err, IsNil) - d.SetStatsUpdating(true) - s.domain = d -} - -func (s *baseTestSuite) TearDownSuite(c *C) { - s.domain.Close() - s.store.Close() -} - -var _ = Suite(&testPlugin{&baseTestSuite{}}) - -type testPlugin struct{ *baseTestSuite } - -func TestPlugin(t *testing.T) { - //rescueStdout := os.Stdout - //_, w, _ := os.Pipe() - //os.Stdout = w - // - //os.Stdout = rescueStdout - // - //runConf := RunConf{Output: rescueStdout, Verbose: true, KeepWorkDir: true} - TestingT(t) -} - -func (s *testPlugin) TestPlugin(c *C) { - tk := testkit.NewTestKit(c, s.store) - manifest := &plugin.EngineManifest{ - Manifest: plugin.Manifest{ - Name: "csv", - }, - OnReaderOpen: plugin.OnReaderOpen, - OnReaderNext: plugin.OnReaderNext, - //OnReaderClose: plugin.OnReaderClose, - } - plugin.Set(plugin.Engine, &plugin.Plugin{ - Manifest: plugin.ExportManifest(manifest), - Path: "", - Disabled: 0, - State: plugin.Ready, - }) - - tk.MustExec("use test") - tk.MustExec("create table t1(a int, b char(255)) ENGINE = csv") - result := tk.MustQuery("select * from t1") - result.Check(testkit.Rows("0 233333", "1 233333", "2 233333", "3 233333", "4 233333", "5 233333")) - result = tk.MustQuery("select * from t1 where a = 2") - result.Check(testkit.Rows("2 233333", )) -} diff --git a/plugin/test_engine.go b/plugin/test_engine.go deleted file mode 100644 index ad122eb09400f..0000000000000 --- a/plugin/test_engine.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2019 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 plugin - -import ( - "context" - "fmt" - "github.com/pingcap/tidb/util/chunk" -) - -type ReadExecutor struct { - pos int -} - -var Files = make(map[string]*ReadExecutor) - -// Validate implements TiDB plugin's Validate SPI. -func Validate(ctx context.Context, m *Manifest) error { - fmt.Println("csv plugin validate") - return nil -} - -// OnInit implements TiDB plugin's OnInit SPI. -func OnInit(ctx context.Context, manifest *Manifest) error { - fmt.Println("csv init called") - return nil -} - -// OnShutdown implements TiDB plugin's OnShutdown SPI. -func OnShutdown(ctx context.Context, manifest *Manifest) error { - fmt.Println("csv shutdown called") - return nil -} - -func OnReaderOpen(ctx context.Context, meta *ExecutorMeta) { - Files[meta.Table.Name.L] = &ReadExecutor{ - pos: 0, - } -} - -func OnReaderNext(ctx context.Context, chk *chunk.Chunk, meta *ExecutorMeta) error { - if _, ok := Files[meta.Table.Name.L]; !ok { - fmt.Println("have some problem") - return nil - } - e := Files[meta.Table.Name.L] - if e.pos > 5 { - return nil - } - chk.AppendInt64(0, int64(e.pos)) - chk.AppendString(1, "233333") - e.pos += 1 - return nil -}