Documentation ¶
Overview ¶
Package fakesqldb provides a MySQL server for tests.
Index ¶
- type DB
- func (db *DB) AddExpectedExecuteFetch(entry ExpectedExecuteFetch)
- func (db *DB) AddExpectedExecuteFetchAtIndex(index int, entry ExpectedExecuteFetch)
- func (db *DB) AddExpectedQuery(query string, err error)
- func (db *DB) AddExpectedQueryAtIndex(index int, query string, err error)
- func (db *DB) AddQuery(query string, expectedResult *sqltypes.Result) *ExpectedResult
- func (db *DB) AddQueryPattern(queryPattern string, expectedResult *sqltypes.Result)
- func (db *DB) AddQueryPatternWithCallback(queryPattern string, expectedResult *sqltypes.Result, callback func(string))
- func (db *DB) AddRejectedQuery(query string, err error)
- func (db *DB) ClearQueryPattern()
- func (db *DB) Close()
- func (db *DB) CloseAllConnections()
- func (db *DB) ComBinlogDumpGTID(c *mysql.Conn, gtidSet mysql.GTIDSet) error
- func (db *DB) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*querypb.BindVariable) ([]*querypb.Field, error)
- func (db *DB) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error
- func (db *DB) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, ...) error
- func (db *DB) ConnParams() dbconfigs.Connector
- func (db *DB) ConnParamsWithUname(uname string) dbconfigs.Connector
- func (db *DB) ConnectionClosed(c *mysql.Conn)
- func (db *DB) DeleteAllEntries()
- func (db *DB) DeleteAllEntriesAfterIndex(index int)
- func (db *DB) DeleteQuery(query string)
- func (db *DB) DeleteRejectedQuery(query string)
- func (db *DB) DisableConnFail()
- func (db *DB) EnableConnFail()
- func (db *DB) EnableInfinite()
- func (db *DB) EnableShouldClose()
- func (db *DB) GetEntry(index int) *ExpectedExecuteFetch
- func (db *DB) GetQueryCalledNum(query string) int
- func (db *DB) HandleQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error
- func (db *DB) MockQueriesForTable(table string, result *sqltypes.Result)
- func (db *DB) Name() string
- func (db *DB) NewConnection(c *mysql.Conn)
- func (db *DB) OrderMatters() *DB
- func (db *DB) QueryLog() string
- func (db *DB) RejectQueryPattern(queryPattern, error string)
- func (db *DB) ResetQueryLog()
- func (db *DB) SetBeforeFunc(query string, f func())
- func (db *DB) SetConnDelay(d time.Duration)
- func (db *DB) SetName(name string) *DB
- func (db *DB) SetNeverFail(neverFail bool)
- func (db *DB) VerifyAllExecutedOrFail()
- func (db *DB) WaitForClose(timeout time.Duration) error
- func (db *DB) WarningCount(c *mysql.Conn) uint16
- type ExpectedExecuteFetch
- type ExpectedResult
- type QueryHandler
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 (*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 ¶
AddExpectedQuery adds a single query with no result.
func (*DB) AddExpectedQueryAtIndex ¶
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 ¶
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 ¶
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
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) 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 ¶
ConnParams returns the ConnParams to connect to the DB.
func (*DB) ConnParamsWithUname ¶
ConnParamsWithUname returns ConnParams to connect to the DB with the Uname set to the provided value.
func (*DB) ConnectionClosed ¶
ConnectionClosed is part of the mysql.Handler interface.
func (*DB) DeleteAllEntries ¶
func (db *DB) DeleteAllEntries()
DeleteAllEntries removes all ordered entries.
func (*DB) DeleteAllEntriesAfterIndex ¶
DeleteAllEntriesAfterIndex removes all queries after the index.
func (*DB) DeleteQuery ¶
DeleteQuery deletes query from the fake DB.
func (*DB) DeleteRejectedQuery ¶
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 ¶
GetQueryCalledNum returns how many times db executes a certain query.
func (*DB) HandleQuery ¶
HandleQuery is the default implementation of the QueryHandler interface
func (*DB) MockQueriesForTable ¶ added in v0.14.0
func (*DB) NewConnection ¶
NewConnection is part of the mysql.Handler interface.
func (*DB) RejectQueryPattern ¶ added in v0.10.0
RejectQueryPattern allows a query pattern to be rejected with an error
func (*DB) SetBeforeFunc ¶
SetBeforeFunc sets the BeforeFunc field for the previously registered "query".
func (*DB) SetConnDelay ¶
SetConnDelay delays connections to this fake DB for the given duration
func (*DB) SetNeverFail ¶ added in v0.14.0
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 ¶
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.
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.