go-vitess.v1: gopkg.in/src-d/go-vitess.v1/mysql/fakesqldb Index | Files

package fakesqldb

import "gopkg.in/src-d/go-vitess.v1/mysql/fakesqldb"

Package fakesqldb provides a MySQL server for tests.

Index

Package Files

server.go

type DB Uses

type DB struct {

    // AllowAll: if set to true, ComQuery returns an empty result
    // for all queries. This flag is used for benchmarking.
    AllowAll bool

    // Handler: interface that allows a caller to override the query handling
    // implementation. By default it points to the DB itself
    Handler QueryHandler
    // contains filtered or unexported fields
}

DB is a fake database and all its methods are thread safe. It creates a mysql.Listener and implements the mysql.Handler interface. We use a Unix socket to connect to the database, as this is the most common way for clients to connect to MySQL. This impacts the error codes we're getting back: when the server side is closed, the client queries will return CRServerGone(2006) when sending the data, as opposed to CRServerLost(2013) when reading the response.

func New Uses

func New(t *testing.T) *DB

New creates a server, and starts listening.

func (*DB) AddExpectedExecuteFetch Uses

func (db *DB) AddExpectedExecuteFetch(entry ExpectedExecuteFetch)

AddExpectedExecuteFetch adds an ExpectedExecuteFetch directly.

func (*DB) AddExpectedExecuteFetchAtIndex Uses

func (db *DB) AddExpectedExecuteFetchAtIndex(index int, entry ExpectedExecuteFetch)

AddExpectedExecuteFetchAtIndex inserts a new entry at index. index values start at 0.

func (*DB) AddExpectedQuery Uses

func (db *DB) AddExpectedQuery(query string, err error)

AddExpectedQuery adds a single query with no result.

func (*DB) AddExpectedQueryAtIndex Uses

func (db *DB) AddExpectedQueryAtIndex(index int, query string, err error)

AddExpectedQueryAtIndex adds an expected ordered query at an index.

func (*DB) AddQuery Uses

func (db *DB) AddQuery(query string, expectedResult *sqltypes.Result) *ExpectedResult

AddQuery adds a query and its expected result.

func (*DB) AddQueryPattern Uses

func (db *DB) AddQueryPattern(queryPattern string, expectedResult *sqltypes.Result)

AddQueryPattern adds an expected result for a set of queries. These patterns are checked if no exact matches from AddQuery() are found. This function forces the addition of begin/end anchors (^$) and turns on case-insensitive matching mode.

func (*DB) AddRejectedQuery Uses

func (db *DB) AddRejectedQuery(query string, err error)

AddRejectedQuery adds a query which will be rejected at execution time.

func (*DB) Close Uses

func (db *DB) Close()

Close closes the Listener and waits for it to stop accepting. It then closes all connections, and cleans up the temporary directory.

func (*DB) CloseAllConnections Uses

func (db *DB) CloseAllConnections()

CloseAllConnections can be used to provoke MySQL client errors for open connections. Make sure to call WaitForShutdown() as well.

func (*DB) ComQuery Uses

func (db *DB) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error

ComQuery is part of the mysql.Handler interface.

func (*DB) ConnParams Uses

func (db *DB) ConnParams() *mysql.ConnParams

ConnParams returns the ConnParams to connect to the DB.

func (*DB) ConnParamsWithUname Uses

func (db *DB) ConnParamsWithUname(uname string) *mysql.ConnParams

ConnParamsWithUname returns ConnParams to connect to the DB with the Uname set to the provided value.

func (*DB) ConnectionClosed Uses

func (db *DB) ConnectionClosed(c *mysql.Conn)

ConnectionClosed is part of the mysql.Handler interface.

func (*DB) DeleteAllEntries Uses

func (db *DB) DeleteAllEntries()

DeleteAllEntries removes all ordered entries.

func (*DB) DeleteAllEntriesAfterIndex Uses

func (db *DB) DeleteAllEntriesAfterIndex(index int)

DeleteAllEntriesAfterIndex removes all queries after the index.

func (*DB) DeleteQuery Uses

func (db *DB) DeleteQuery(query string)

DeleteQuery deletes query from the fake DB.

func (*DB) DeleteRejectedQuery Uses

func (db *DB) DeleteRejectedQuery(query string)

DeleteRejectedQuery deletes query from the fake DB.

func (*DB) DisableConnFail Uses

func (db *DB) DisableConnFail()

DisableConnFail makes connection to this fake DB success.

func (*DB) EnableConnFail Uses

func (db *DB) EnableConnFail()

EnableConnFail makes connection to this fake DB fail.

func (*DB) EnableInfinite Uses

func (db *DB) EnableInfinite()

EnableInfinite turns on the infinite flag (the last ordered query is used).

func (*DB) EnableShouldClose Uses

func (db *DB) EnableShouldClose()

EnableShouldClose closes the connection when processing the next query.

func (*DB) GetEntry Uses

func (db *DB) GetEntry(index int) *ExpectedExecuteFetch

GetEntry returns the expected entry at "index". If index is out of bounds, the return value will be nil.

func (*DB) GetQueryCalledNum Uses

func (db *DB) GetQueryCalledNum(query string) int

GetQueryCalledNum returns how many times db executes a certain query.

func (*DB) HandleQuery Uses

func (db *DB) HandleQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error

HandleQuery is the default implementation of the QueryHandler interface

func (*DB) NewConnection Uses

func (db *DB) NewConnection(c *mysql.Conn)

NewConnection is part of the mysql.Handler interface.

func (*DB) OrderMatters Uses

func (db *DB) OrderMatters() *DB

OrderMatters sets the orderMatters flag.

func (*DB) SetBeforeFunc Uses

func (db *DB) SetBeforeFunc(query string, f func())

SetBeforeFunc sets the BeforeFunc field for the previously registered "query".

func (*DB) SetConnDelay Uses

func (db *DB) SetConnDelay(d time.Duration)

SetConnDelay delays connections to this fake DB for the given duration

func (*DB) SetName Uses

func (db *DB) SetName(name string) *DB

SetName sets the name of the DB. to differentiate them in tests if needed.

func (*DB) VerifyAllExecutedOrFail Uses

func (db *DB) VerifyAllExecutedOrFail()

VerifyAllExecutedOrFail checks that all expected queries where actually received and executed. If not, it will let the test fail.

func (*DB) WaitForClose Uses

func (db *DB) WaitForClose(timeout time.Duration) error

WaitForClose should be used after CloseAllConnections() is closed and you want to provoke a MySQL client error with errno 2006.

If you don't call this function and execute the next query right away, you will very likely see errno 2013 instead due to timing issues. That's because the following can happen:

1. vttablet MySQL client is able to send the query to this fake server. 2. The fake server sees the query and calls the ComQuery() callback. 3. The fake server tries to write the response back on the connection. => This will finally fail because the connection is already closed. In this example, the client would have been able to send off the query and therefore return errno 2013 ("server lost"). Instead, if step 1 already fails, the client returns errno 2006 ("gone away"). By waiting for the connections to close, you make sure of that.

func (*DB) WarningCount Uses

func (db *DB) WarningCount(c *mysql.Conn) uint16

WarningCount is part of the mysql.Handler interface.

type ExpectedExecuteFetch Uses

type ExpectedExecuteFetch struct {
    Query       string
    QueryResult *sqltypes.Result
    Error       error
    // AfterFunc is a callback which is executed while the query
    // is executed i.e., before the fake responds to the client.
    AfterFunc func()
}

ExpectedExecuteFetch defines for an expected query the to be faked output. It is used for ordered expected output.

type ExpectedResult Uses

type ExpectedResult struct {
    *sqltypes.Result
    // BeforeFunc() is synchronously called before the server returns the result.
    BeforeFunc func()
}

ExpectedResult holds the data for a matched query.

type QueryHandler Uses

type QueryHandler interface {
    HandleQuery(*mysql.Conn, string, func(*sqltypes.Result) error) error
}

QueryHandler is the interface used by the DB to simulate executed queries

Package fakesqldb imports 12 packages (graph) and is imported by 6 packages. Updated 2019-06-13. Refresh now. Tools for package owners.