fakesqldb

package
v0.14.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 14 Imported by: 3

Documentation

Overview

Package fakesqldb provides a MySQL server for tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	mysql.UnimplementedHandler

	// 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

func New(t testing.TB) *DB

New creates a server, and starts listening.

func (*DB) AddExpectedExecuteFetch

func (db *DB) AddExpectedExecuteFetch(entry ExpectedExecuteFetch)

AddExpectedExecuteFetch adds an ExpectedExecuteFetch directly.

func (*DB) AddExpectedExecuteFetchAtIndex

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

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

func (*DB) AddExpectedQuery

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

AddExpectedQuery adds a single query with no result.

func (*DB) AddExpectedQueryAtIndex

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

AddExpectedQueryAtIndex adds an expected ordered query at an index.

func (*DB) AddQuery

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

AddQuery adds a query and its expected result.

func (*DB) AddQueryPattern

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) AddQueryPatternWithCallback

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

AddQueryPatternWithCallback is similar to AddQueryPattern: in addition it calls the provided callback function The callback can be used to set user counters/variables for testing specific usecases

func (*DB) AddRejectedQuery

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

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

func (*DB) ClearQueryPattern added in v0.10.0

func (db *DB) ClearQueryPattern()

ClearQueryPattern removes all query patterns set up

func (*DB) Close

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

func (db *DB) CloseAllConnections()

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

func (*DB) ComBinlogDumpGTID added in v0.14.0

func (db *DB) ComBinlogDumpGTID(c *mysql.Conn, gtidSet mysql.GTIDSet) error

ComBinlogDumpGTID is part of the mysql.Handler interface.

func (*DB) ComPrepare

func (db *DB) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*querypb.BindVariable) ([]*querypb.Field, error)

ComPrepare is part of the mysql.Handler interface.

func (*DB) ComQuery

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) ComStmtExecute

func (db *DB) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error

ComStmtExecute is part of the mysql.Handler interface.

func (*DB) ConnParams

func (db *DB) ConnParams() dbconfigs.Connector

ConnParams returns the ConnParams to connect to the DB.

func (*DB) ConnParamsWithUname

func (db *DB) ConnParamsWithUname(uname string) dbconfigs.Connector

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

func (*DB) ConnectionClosed

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

ConnectionClosed is part of the mysql.Handler interface.

func (*DB) DeleteAllEntries

func (db *DB) DeleteAllEntries()

DeleteAllEntries removes all ordered entries.

func (*DB) DeleteAllEntriesAfterIndex

func (db *DB) DeleteAllEntriesAfterIndex(index int)

DeleteAllEntriesAfterIndex removes all queries after the index.

func (*DB) DeleteQuery

func (db *DB) DeleteQuery(query string)

DeleteQuery deletes query from the fake DB.

func (*DB) DeleteRejectedQuery

func (db *DB) DeleteRejectedQuery(query string)

DeleteRejectedQuery deletes query from the fake DB.

func (*DB) DisableConnFail

func (db *DB) DisableConnFail()

DisableConnFail makes connection to this fake DB success.

func (*DB) EnableConnFail

func (db *DB) EnableConnFail()

EnableConnFail makes connection to this fake DB fail.

func (*DB) EnableInfinite

func (db *DB) EnableInfinite()

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

func (*DB) EnableShouldClose

func (db *DB) EnableShouldClose()

EnableShouldClose closes the connection when processing the next query.

func (*DB) GetEntry

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

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

GetQueryCalledNum returns how many times db executes a certain query.

func (*DB) HandleQuery

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) MockQueriesForTable added in v0.14.0

func (db *DB) MockQueriesForTable(table string, result *sqltypes.Result)

func (*DB) NewConnection

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

NewConnection is part of the mysql.Handler interface.

func (*DB) OrderMatters

func (db *DB) OrderMatters() *DB

OrderMatters sets the orderMatters flag.

func (*DB) QueryLog

func (db *DB) QueryLog() string

QueryLog returns the query log in a semicomma separated string

func (*DB) RejectQueryPattern added in v0.10.0

func (db *DB) RejectQueryPattern(queryPattern, error string)

RejectQueryPattern allows a query pattern to be rejected with an error

func (*DB) ResetQueryLog

func (db *DB) ResetQueryLog()

ResetQueryLog resets the query log

func (*DB) SetBeforeFunc

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

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

func (*DB) SetConnDelay

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

SetConnDelay delays connections to this fake DB for the given duration

func (*DB) SetName

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

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

func (*DB) SetNeverFail added in v0.14.0

func (db *DB) SetNeverFail(neverFail bool)

func (*DB) VerifyAllExecutedOrFail

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

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

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

WarningCount is part of the mysql.Handler interface.

type ExpectedExecuteFetch

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

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

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

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL