Skip to content

Commit

Permalink
Merge pull request #9 from WPH95/add-udf-test
Browse files Browse the repository at this point in the history
feature: add udf test and refact csv test
  • Loading branch information
wph95 authored Oct 26, 2019
2 parents 4f8140f + e56bf96 commit 7b02f9e
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 59 deletions.
10 changes: 5 additions & 5 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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';")
}

8 changes: 5 additions & 3 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions executor/plugin_executor.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion plugin/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
123 changes: 82 additions & 41 deletions plugin/csv/csv_test.go
Original file line number Diff line number Diff line change
@@ -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", ))
}
44 changes: 38 additions & 6 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,17 +384,33 @@ 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
}

Expand All @@ -409,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
}
Expand Down
Loading

0 comments on commit 7b02f9e

Please sign in to comment.