fakesqldb

package
v2.1.0-rc.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2017 License: BSD-3-Clause Imports: 12 Imported by: 0

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

func New(t *testing.T) *DB

New creates a server, and starts listening.

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

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

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

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

ComQuery is part of the mysqlconn.Handler interface.

func (*DB) ConnParams

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

ConnParams returns the ConnParams to connect to the DB.

func (*DB) ConnectionClosed

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

ConnectionClosed is part of the mysqlconn.Handler interface.

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

func (db *DB) EnableShouldClose()

EnableShouldClose closes the connection when processing the next query.

func (*DB) GetQueryCalledNum

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

GetQueryCalledNum returns how many times db executes a certain query.

func (*DB) NewConnection

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

NewConnection is part of the mysqlconn.Handler interface.

func (*DB) SetBeforeFunc

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

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

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

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.

Jump to

Keyboard shortcuts

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