Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeResumeToken(t []byte) (uint64, error)
- func EncodeResumeToken(t uint64) []byte
- type InMemInstanceAdminServer
- type InMemSpannerServer
- type MockCloudSpanner
- func (m *MockCloudSpanner) AddMsg(err error, resumeToken bool)
- func (m *MockCloudSpanner) Addr() string
- func (m *MockCloudSpanner) BatchCreateSessions(c context.Context, r *sppb.BatchCreateSessionsRequest) (*sppb.BatchCreateSessionsResponse, error)
- func (m *MockCloudSpanner) BeginTransaction(_ context.Context, r *sppb.BeginTransactionRequest) (*sppb.Transaction, error)
- func (m *MockCloudSpanner) CreateSession(c context.Context, r *sppb.CreateSessionRequest) (*sppb.Session, error)
- func (m *MockCloudSpanner) DeleteSession(c context.Context, r *sppb.DeleteSessionRequest) (*empty.Empty, error)
- func (m *MockCloudSpanner) Done()
- func (m *MockCloudSpanner) ExecuteStreamingSql(r *sppb.ExecuteSqlRequest, s sppb.Spanner_ExecuteStreamingSqlServer) error
- func (m *MockCloudSpanner) GetSession(c context.Context, r *sppb.GetSessionRequest) (*sppb.Session, error)
- func (m *MockCloudSpanner) Serve()
- func (m *MockCloudSpanner) Stop()
- func (m *MockCloudSpanner) StreamingRead(r *sppb.ReadRequest, s sppb.Spanner_StreamingReadServer) error
- type MockCloudSpannerClient
- func (m *MockCloudSpannerClient) BeginTransaction(ctx context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error)
- func (m *MockCloudSpannerClient) Commit(ctx context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error)
- func (m *MockCloudSpannerClient) CreateSession(ctx context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
- func (m *MockCloudSpannerClient) DeleteSession(ctx context.Context, r *sppb.DeleteSessionRequest, opts ...grpc.CallOption) (*empty.Empty, error)
- func (m *MockCloudSpannerClient) DumpPings() []string
- func (m *MockCloudSpannerClient) DumpSessions() map[string]bool
- func (m *MockCloudSpannerClient) ExecuteBatchDml(ctx context.Context, r *sppb.ExecuteBatchDmlRequest, opts ...grpc.CallOption) (*sppb.ExecuteBatchDmlResponse, error)
- func (m *MockCloudSpannerClient) ExecuteSql(ctx context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (*sppb.ResultSet, error)
- func (m *MockCloudSpannerClient) ExecuteStreamingSql(ctx context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (sppb.Spanner_ExecuteStreamingSqlClient, error)
- func (m *MockCloudSpannerClient) Freeze()
- func (m *MockCloudSpannerClient) GetSession(ctx context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
- func (m *MockCloudSpannerClient) PartitionQuery(ctx context.Context, r *sppb.PartitionQueryRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error)
- func (m *MockCloudSpannerClient) PartitionRead(ctx context.Context, r *sppb.PartitionReadRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error)
- func (m *MockCloudSpannerClient) Rollback(ctx context.Context, r *sppb.RollbackRequest, opts ...grpc.CallOption) (*empty.Empty, error)
- func (m *MockCloudSpannerClient) StreamingRead(ctx context.Context, r *sppb.ReadRequest, opts ...grpc.CallOption) (sppb.Spanner_StreamingReadClient, error)
- func (m *MockCloudSpannerClient) Unfreeze()
- type MockCtlMsg
- type MockedSpannerInMemTestServer
- type PartialResultSetExecutionTime
- type SimulatedExecutionTime
- type StatementResult
- type StatementResultType
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" )
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.
Variables ¶
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 ¶
DecodeResumeToken decodes a mock resume token into an uint64 integer.
func EncodeResumeToken ¶
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) BatchCreateSessions ¶ added in v1.6.0
func (m *MockCloudSpanner) BatchCreateSessions(c context.Context, r *sppb.BatchCreateSessionsRequest) (*sppb.BatchCreateSessionsResponse, error)
BatchCreateSessions is a placeholder for SpannerServer.BatchCreateSessions.
func (*MockCloudSpanner) BeginTransaction ¶
func (m *MockCloudSpanner) BeginTransaction(_ context.Context, r *sppb.BeginTransactionRequest) (*sppb.Transaction, error)
BeginTransaction is a placeholder for SpannerServer.BeginTransaction.
func (*MockCloudSpanner) CreateSession ¶
func (m *MockCloudSpanner) CreateSession(c context.Context, r *sppb.CreateSessionRequest) (*sppb.Session, error)
CreateSession is a placeholder for SpannerServer.CreateSession.
func (*MockCloudSpanner) DeleteSession ¶
func (m *MockCloudSpanner) DeleteSession(c context.Context, r *sppb.DeleteSessionRequest) (*empty.Empty, error)
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 ¶
func (m *MockCloudSpanner) ExecuteStreamingSql(r *sppb.ExecuteSqlRequest, s sppb.Spanner_ExecuteStreamingSqlServer) error
ExecuteStreamingSql is a mock implementation of SpannerServer.ExecuteStreamingSql.
func (*MockCloudSpanner) GetSession ¶
func (m *MockCloudSpanner) GetSession(c context.Context, r *sppb.GetSessionRequest) (*sppb.Session, error)
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 ¶
func (m *MockCloudSpanner) StreamingRead(r *sppb.ReadRequest, s sppb.Spanner_StreamingReadServer) error
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 ¶
func (m *MockCloudSpannerClient) BeginTransaction(ctx context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error)
BeginTransaction is a placeholder for SpannerClient.BeginTransaction.
func (*MockCloudSpannerClient) Commit ¶
func (m *MockCloudSpannerClient) Commit(ctx context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error)
Commit is a placeholder for SpannerClient.Commit.
func (*MockCloudSpannerClient) CreateSession ¶
func (m *MockCloudSpannerClient) CreateSession(ctx context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
CreateSession is a placeholder for SpannerClient.CreateSession.
func (*MockCloudSpannerClient) DeleteSession ¶
func (m *MockCloudSpannerClient) DeleteSession(ctx context.Context, r *sppb.DeleteSessionRequest, opts ...grpc.CallOption) (*empty.Empty, error)
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 ¶
func (m *MockCloudSpannerClient) ExecuteBatchDml(ctx context.Context, r *sppb.ExecuteBatchDmlRequest, opts ...grpc.CallOption) (*sppb.ExecuteBatchDmlResponse, error)
ExecuteBatchDml is a placeholder for SpannerClient.ExecuteBatchDml.
func (*MockCloudSpannerClient) ExecuteSql ¶
func (m *MockCloudSpannerClient) ExecuteSql(ctx context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (*sppb.ResultSet, error)
ExecuteSql is a placeholder for SpannerClient.ExecuteSql.
func (*MockCloudSpannerClient) ExecuteStreamingSql ¶
func (m *MockCloudSpannerClient) ExecuteStreamingSql(ctx context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (sppb.Spanner_ExecuteStreamingSqlClient, error)
ExecuteStreamingSql is a mock implementation of SpannerClient.ExecuteStreamingSql.
func (*MockCloudSpannerClient) Freeze ¶
func (m *MockCloudSpannerClient) Freeze()
Freeze stalls all requests.
func (*MockCloudSpannerClient) GetSession ¶
func (m *MockCloudSpannerClient) GetSession(ctx context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
GetSession is a placeholder for SpannerClient.GetSession.
func (*MockCloudSpannerClient) PartitionQuery ¶
func (m *MockCloudSpannerClient) PartitionQuery(ctx context.Context, r *sppb.PartitionQueryRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error)
PartitionQuery is a placeholder for SpannerServer.PartitionQuery.
func (*MockCloudSpannerClient) PartitionRead ¶
func (m *MockCloudSpannerClient) PartitionRead(ctx context.Context, r *sppb.PartitionReadRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error)
PartitionRead is a placeholder for SpannerServer.PartitionRead.
func (*MockCloudSpannerClient) Rollback ¶
func (m *MockCloudSpannerClient) Rollback(ctx context.Context, r *sppb.RollbackRequest, opts ...grpc.CallOption) (*empty.Empty, error)
Rollback is a placeholder for SpannerClient.Rollback.
func (*MockCloudSpannerClient) StreamingRead ¶
func (m *MockCloudSpannerClient) StreamingRead(ctx context.Context, r *sppb.ReadRequest, opts ...grpc.CallOption) (sppb.Spanner_StreamingReadClient, error)
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 )