Documentation ¶
Overview ¶
Package fakesqldb provides a MySQL server for tests.
Index ¶
- type DB
- func (db *DB) AddQuery(query string, expectedResult *sqltypes.Result) *ExpectedResult
- func (db *DB) AddQueryPattern(queryPattern string, expectedResult *sqltypes.Result)
- func (db *DB) AddRejectedQuery(query string, err error)
- func (db *DB) Close()
- func (db *DB) CloseAllConnections()
- func (db *DB) ComQuery(c *mysqlconn.Conn, query string) (*sqltypes.Result, error)
- func (db *DB) ConnParams() *sqldb.ConnParams
- func (db *DB) ConnectionClosed(c *mysqlconn.Conn)
- func (db *DB) DeleteQuery(query string)
- func (db *DB) DeleteRejectedQuery(query string)
- func (db *DB) DisableConnFail()
- func (db *DB) EnableConnFail()
- func (db *DB) EnableShouldClose()
- func (db *DB) GetQueryCalledNum(query string) int
- func (db *DB) NewConnection(c *mysqlconn.Conn)
- func (db *DB) SetBeforeFunc(query string, f func())
- func (db *DB) SetName(name string) *DB
- func (db *DB) WaitForClose(timeout time.Duration) error
- type ExpectedResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a fake database and all its methods are thread safe. It creates a mysqlconn.Listener and implements the mysqlconn.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) 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) AddRejectedQuery ¶
AddRejectedQuery adds a query which will be rejected at execution time.
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 WaitForShutdown() as well.
func (*DB) ConnParams ¶
func (db *DB) ConnParams() *sqldb.ConnParams
ConnParams returns the ConnParams to connect to the DB.
func (*DB) ConnectionClosed ¶
ConnectionClosed is part of the mysqlconn.Handler interface.
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) EnableShouldClose ¶
func (db *DB) EnableShouldClose()
EnableShouldClose closes the connection when processing the next query.
func (*DB) GetQueryCalledNum ¶
GetQueryCalledNum returns how many times db executes a certain query.
func (*DB) NewConnection ¶
NewConnection is part of the mysqlconn.Handler interface.
func (*DB) SetBeforeFunc ¶
SetBeforeFunc sets the BeforeFunc field for the previously registered "query".
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 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.