testutil

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2020 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodBeginTransaction    string = "BEGIN_TRANSACTION"
	MethodCommitTransaction   string = "COMMIT_TRANSACTION"
	MethodBatchCreateSession  string = "BATCH_CREATE_SESSION"
	MethodCreateSession       string = "CREATE_SESSION"
	MethodDeleteSession       string = "DELETE_SESSION"
	MethodGetSession          string = "GET_SESSION"
	MethodExecuteSql          string = "EXECUTE_SQL"
	MethodExecuteStreamingSql string = "EXECUTE_STREAMING_SQL"
	MethodExecuteBatchDml     string = "EXECUTE_BATCH_DML"
)

The method names that can be used to register execution times and errors.

View Source
const SelectFooFromBar = "SELECT FOO FROM BAR"

SelectFooFromBar is a SELECT statement that is added to the mocked test server and will return a one-col-two-rows result set containing the INT64 values 1 and 2.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbums = "SELECT SingerId, AlbumId, AlbumTitle FROM Albums"

SelectSingerIDAlbumIDAlbumTitleFromAlbums i a SELECT statement that is added to the mocked test server and will return a 3-cols-3-rows result set.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount int = 3

SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount is the number of cols returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount int64 = 3

SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount is the number of rows returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement.

View Source
const UpdateBarSetFoo = "UPDATE FOO SET BAR=1 WHERE BAZ=2"

UpdateBarSetFoo is an UPDATE statement that is added to the mocked test server that will return an update count of 5.

View Source
const UpdateBarSetFooRowCount = 5

UpdateBarSetFooRowCount is the constant update count value returned by the statement defined in UpdateBarSetFoo.

Variables

View Source
var (
	// KvMeta is the Metadata for mocked KV table.
	KvMeta = sppb.ResultSetMetadata{
		RowType: &sppb.StructType{
			Fields: []*sppb.StructType_Field{
				{
					Name: "Key",
					Type: &sppb.Type{Code: sppb.TypeCode_STRING},
				},
				{
					Name: "Value",
					Type: &sppb.Type{Code: sppb.TypeCode_STRING},
				},
			},
		},
	}
)

Functions

func DecodeResumeToken

func DecodeResumeToken(t []byte) (uint64, error)

DecodeResumeToken decodes a mock resume token into an uint64 integer.

func EncodeResumeToken

func EncodeResumeToken(t uint64) []byte

EncodeResumeToken return mock resume token encoding for an uint64 integer.

Types

type InMemInstanceAdminServer added in v1.2.0

type InMemInstanceAdminServer interface {
	instancepb.InstanceAdminServer
	Stop()
	Resps() []proto.Message
	SetResps([]proto.Message)
	Reqs() []proto.Message
	SetReqs([]proto.Message)
	SetErr(error)
}

InMemInstanceAdminServer contains the InstanceAdminServer interface plus a couple of specific methods for setting mocked results.

func NewInMemInstanceAdminServer added in v1.2.0

func NewInMemInstanceAdminServer() InMemInstanceAdminServer

NewInMemInstanceAdminServer creates a new in-mem test server.

type InMemSpannerServer

type InMemSpannerServer interface {
	spannerpb.SpannerServer

	// Stops this server.
	Stop()

	// Resets the in-mem server to its default state, deleting all sessions and
	// transactions that have been created on the server. Mocked results are
	// not deleted.
	Reset()

	// Sets an error that will be returned by the next server call. The server
	// call will also automatically clear the error.
	SetError(err error)

	// Puts a mocked result on the server for a specific sql statement. The
	// server does not parse the SQL string in any way, it is merely used as
	// a key to the mocked result. The result will be used for all methods that
	// expect a SQL statement, including (batch) DML methods.
	PutStatementResult(sql string, result *StatementResult) error

	// Puts a mocked result on the server for a specific partition token. The
	// result will only be used for query requests that specify a partition
	// token.
	PutPartitionResult(partitionToken []byte, result *StatementResult) error

	// Adds a PartialResultSetExecutionTime to the server that should be returned
	// for the specified SQL string.
	AddPartialResultSetError(sql string, err PartialResultSetExecutionTime)

	// Removes a mocked result on the server for a specific sql statement.
	RemoveStatementResult(sql string)

	// Aborts the specified transaction . This method can be used to test
	// transaction retry logic.
	AbortTransaction(id []byte)

	// Puts a simulated execution time for one of the Spanner methods.
	PutExecutionTime(method string, executionTime SimulatedExecutionTime)
	// Freeze stalls all requests.
	Freeze()
	// Unfreeze restores processing requests.
	Unfreeze()

	TotalSessionsCreated() uint
	TotalSessionsDeleted() uint
	SetMaxSessionsReturnedByServerPerBatchRequest(sessionCount int32)
	SetMaxSessionsReturnedByServerInTotal(sessionCount int32)

	ReceivedRequests() chan interface{}
	DumpSessions() map[string]bool
	ClearPings()
	DumpPings() []string
}

InMemSpannerServer contains the SpannerServer interface plus a couple of specific methods for adding mocked results and resetting the server.

func NewInMemSpannerServer

func NewInMemSpannerServer() InMemSpannerServer

NewInMemSpannerServer creates a new in-mem test server.

type MockCloudSpanner

type MockCloudSpanner struct {
	sppb.SpannerServer
	// contains filtered or unexported fields
}

MockCloudSpanner is a mock implementation of SpannerServer interface. TODO: make MockCloudSpanner a full-fleged Cloud Spanner implementation.

func NewMockCloudSpanner

func NewMockCloudSpanner(t *testing.T, ts time.Time) *MockCloudSpanner

NewMockCloudSpanner creates a new MockCloudSpanner instance.

func (*MockCloudSpanner) AddMsg

func (m *MockCloudSpanner) AddMsg(err error, resumeToken bool)

AddMsg generates a new mocked row which can be received by client.

func (*MockCloudSpanner) Addr

func (m *MockCloudSpanner) Addr() string

Addr returns the listening address of mock server.

func (*MockCloudSpanner) BeginTransaction

BeginTransaction is a placeholder for SpannerServer.BeginTransaction.

func (*MockCloudSpanner) CreateSession

CreateSession is a placeholder for SpannerServer.CreateSession.

func (*MockCloudSpanner) DeleteSession

DeleteSession is a placeholder for SpannerServer.DeleteSession.

func (*MockCloudSpanner) Done

func (m *MockCloudSpanner) Done()

Done signals an end to a mocked stream.

func (*MockCloudSpanner) ExecuteStreamingSql

ExecuteStreamingSql is a mock implementation of SpannerServer.ExecuteStreamingSql.

func (*MockCloudSpanner) GetSession

GetSession is a placeholder for SpannerServer.GetSession.

func (*MockCloudSpanner) Serve

func (m *MockCloudSpanner) Serve()

Serve runs a MockCloudSpanner listening on a random localhost address.

func (*MockCloudSpanner) Stop

func (m *MockCloudSpanner) Stop()

Stop terminates MockCloudSpanner and closes the serving port.

func (*MockCloudSpanner) StreamingRead

StreamingRead is a placeholder for SpannerServer.StreamingRead.

type MockCloudSpannerClient

type MockCloudSpannerClient struct {
	sppb.SpannerClient

	// Expected set of actions that have been executed by the client. These
	// interfaces should be type reflected against with *Request types in sppb,
	// such as sppb.GetSessionRequest. Buffered to a large degree.
	ReceivedRequests chan interface{}
	// contains filtered or unexported fields
}

MockCloudSpannerClient is a mock implementation of sppb.SpannerClient.

func NewMockCloudSpannerClient

func NewMockCloudSpannerClient(t *testing.T) *MockCloudSpannerClient

NewMockCloudSpannerClient creates new MockCloudSpannerClient instance.

func (*MockCloudSpannerClient) BeginTransaction

BeginTransaction is a placeholder for SpannerClient.BeginTransaction.

func (*MockCloudSpannerClient) Commit

Commit is a placeholder for SpannerClient.Commit.

func (*MockCloudSpannerClient) CreateSession

CreateSession is a placeholder for SpannerClient.CreateSession.

func (*MockCloudSpannerClient) DeleteSession

DeleteSession is a placeholder for SpannerClient.DeleteSession.

func (*MockCloudSpannerClient) DumpPings

func (m *MockCloudSpannerClient) DumpPings() []string

DumpPings dumps the ping history.

func (*MockCloudSpannerClient) DumpSessions

func (m *MockCloudSpannerClient) DumpSessions() map[string]bool

DumpSessions dumps the internal session table.

func (*MockCloudSpannerClient) ExecuteBatchDml

ExecuteBatchDml is a placeholder for SpannerClient.ExecuteBatchDml.

func (*MockCloudSpannerClient) ExecuteSql

ExecuteSql is a placeholder for SpannerClient.ExecuteSql.

func (*MockCloudSpannerClient) ExecuteStreamingSql

ExecuteStreamingSql is a mock implementation of SpannerClient.ExecuteStreamingSql.

func (*MockCloudSpannerClient) Freeze

func (m *MockCloudSpannerClient) Freeze()

Freeze stalls all requests.

func (*MockCloudSpannerClient) GetSession

GetSession is a placeholder for SpannerClient.GetSession.

func (*MockCloudSpannerClient) PartitionQuery

PartitionQuery is a placeholder for SpannerServer.PartitionQuery.

func (*MockCloudSpannerClient) PartitionRead

PartitionRead is a placeholder for SpannerServer.PartitionRead.

func (*MockCloudSpannerClient) Rollback

Rollback is a placeholder for SpannerClient.Rollback.

func (*MockCloudSpannerClient) StreamingRead

StreamingRead is a placeholder for SpannerClient.StreamingRead.

func (*MockCloudSpannerClient) Unfreeze

func (m *MockCloudSpannerClient) Unfreeze()

Unfreeze restores processing requests.

type MockCtlMsg

type MockCtlMsg struct {
	// If ResumeToken == true, mock server will generate a row with
	// resume token.
	ResumeToken bool
	// If Err != nil, mock server will return error in RPC response.
	Err error
}

MockCtlMsg encapsulates PartialResultSet/error that might be sent to client

type MockedSpannerInMemTestServer

type MockedSpannerInMemTestServer struct {
	TestSpanner       InMemSpannerServer
	TestInstanceAdmin InMemInstanceAdminServer
	// contains filtered or unexported fields
}

MockedSpannerInMemTestServer is an InMemSpannerServer with results for a number of SQL statements readily mocked.

func NewMockedSpannerInMemTestServer

func NewMockedSpannerInMemTestServer(t *testing.T) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func())

NewMockedSpannerInMemTestServer creates a MockedSpannerInMemTestServer at localhost with a random port and returns client options that can be used to connect to it.

func NewMockedSpannerInMemTestServerWithAddr added in v1.2.0

func NewMockedSpannerInMemTestServerWithAddr(t *testing.T, addr string) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func())

NewMockedSpannerInMemTestServerWithAddr creates a MockedSpannerInMemTestServer at a given listening address and returns client options that can be used to connect to it.

func (*MockedSpannerInMemTestServer) CreateSingleRowSingersResult added in v1.5.0

func (s *MockedSpannerInMemTestServer) CreateSingleRowSingersResult(rowNum int64) *StatementResult

CreateSingleRowSingersResult creates a result set containing a single row of the SelectSingerIDAlbumIDAlbumTitleFromAlbums result set, or zero rows if the given rowNum is greater than the number of rows in the result set. This method can be used to mock results for different partitions of a BatchReadOnlyTransaction.

type PartialResultSetExecutionTime added in v1.1.0

type PartialResultSetExecutionTime struct {
	ResumeToken   []byte
	ExecutionTime time.Duration
	Err           error
}

PartialResultSetExecutionTime represents execution times and errors that should be used when a PartialResult at the specified resume token is to be returned.

type SimulatedExecutionTime

type SimulatedExecutionTime struct {
	MinimumExecutionTime time.Duration
	RandomExecutionTime  time.Duration
	Errors               []error
	// Keep error after execution. The error will continue to be returned until
	// it is cleared.
	KeepError bool
}

SimulatedExecutionTime represents the time the execution of a method should take, and any errors that should be returned by the method.

type StatementResult

type StatementResult struct {
	Type        StatementResultType
	Err         error
	ResultSet   *spannerpb.ResultSet
	UpdateCount int64
}

StatementResult represents a mocked result on the test server. The result is either of: a ResultSet, an update count or an error.

type StatementResultType

type StatementResultType int

StatementResultType indicates the type of result returned by a SQL statement.

const (
	// StatementResultError indicates that the sql statement returns an error.
	StatementResultError StatementResultType = 0
	// StatementResultResultSet indicates that the sql statement returns a
	// result set.
	StatementResultResultSet StatementResultType = 1
	// StatementResultUpdateCount indicates that the sql statement returns an
	// update count.
	StatementResultUpdateCount StatementResultType = 2
	// MaxRowsPerPartialResultSet is the maximum number of rows returned in
	// each PartialResultSet. This number is deliberately set to a low value to
	// ensure that most queries return more than one PartialResultSet.
	MaxRowsPerPartialResultSet = 1
)

Jump to

Keyboard shortcuts

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