forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", )) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |