package testutil

import "cloud.google.com/go/spanner/internal/testutil"

Index

Constants

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"
	MethodStreamingRead       string = "EXECUTE_STREAMING_READ"
	MethodBatchWrite          string = "BATCH_WRITE"
	MethodPartitionQuery      string = "PARTITION_QUERY"
)

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

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.

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.

const SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount int = 3

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

const SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount int64 = 3

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

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.

const UpdateBarSetFooRowCount = 5

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

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.

func KvMeta

func KvMeta() *spannerpb.ResultSetMetadata

KvMeta is the Metadata for mocked KV table.

Types

type InMemInstanceAdminServer

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

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 MockedSpannerInMemTestServer

type MockedSpannerInMemTestServer struct {
	TestSpanner       InMemSpannerServer
	TestInstanceAdmin InMemInstanceAdminServer

	ServerAddress string
	// 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, sopt ...grpc.ServerOption) (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

func NewMockedSpannerInMemTestServerWithAddr(t *testing.T, addr string, sopt ...grpc.ServerOption) (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

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

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
	Responses            []interface{}
	// 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
	ResumeTokens [][]byte
}

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

func (*StatementResult) ToPartialResultSets

func (s *StatementResult) ToPartialResultSets(resumeToken []byte) (result []*spannerpb.PartialResultSet, err error)

ToPartialResultSets converts a ResultSet to a PartialResultSet. This method is used to convert a mocked result to a PartialResultSet when one of the streaming methods are called.

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
)

type TestExporter

type TestExporter struct {
	Stats chan *view.Data
	Views []*view.View
	// contains filtered or unexported fields
}

TestExporter is a test utility exporter. It should be created with NewtestExporter.

func NewTestExporter

func NewTestExporter(views ...*view.View) *TestExporter

NewTestExporter creates a TestExporter and registers it with OpenCensus.

func (*TestExporter) ExportSpan

func (te *TestExporter) ExportSpan(s *trace.SpanData)

ExportSpan exports a span.

func (*TestExporter) ExportView

func (te *TestExporter) ExportView(vd *view.Data)

ExportView exports a view.

func (*TestExporter) Spans

func (te *TestExporter) Spans() []*trace.SpanData

Spans returns the exported spans.

func (*TestExporter) Unregister

func (te *TestExporter) Unregister()

Unregister unregisters the exporter from OpenCensus.

Source Files

inmem_instance_admin_server.go inmem_spanner_server.go mocked_inmem_server.go trace.go

Version
v1.76.1 (latest)
Published
Feb 21, 2025
Platform
linux/amd64
Imports
34 packages
Last checked
14 hours ago

Tools for package owners.