testutil

package
v0.44.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodBeginTransaction    string = "BEGIN_TRANSACTION"
	MethodCommitTransaction   string = "COMMIT_TRANSACTION"
	MethodCreateSession       string = "CREATE_SESSION"
	MethodDeleteSession       string = "DELETE_SESSION"
	MethodGetSession          string = "GET_SESSION"
	MethodExecuteStreamingSql string = "EXECUTE_STREAMING_SQL"
)

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

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 InMemSpannerServer added in v0.42.0

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

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

	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 added in v0.42.0

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 added in v0.37.0

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 added in v0.11.0

func (m *MockCloudSpannerClient) Freeze()

Freeze stalls all requests.

func (*MockCloudSpannerClient) GetSession

GetSession is a placeholder for SpannerClient.GetSession.

func (*MockCloudSpannerClient) PartitionQuery added in v0.20.0

PartitionQuery is a placeholder for SpannerServer.PartitionQuery.

func (*MockCloudSpannerClient) PartitionRead added in v0.20.0

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 added in v0.11.0

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 SimulatedExecutionTime added in v0.42.0

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 added in v0.42.0

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

StatementResult represents a mocked result on the test server. Th result can be either a ResultSet, an update count or an error.

type StatementResultType added in v0.42.0

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
)

Jump to

Keyboard shortcuts

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