2017-08-05 18:17:15 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2018-03-30 09:41:12 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2017-08-05 18:17:15 +00:00
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2017-08-05 18:17:15 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
proto3 "github.com/golang/protobuf/ptypes/struct"
|
|
|
|
pbt "github.com/golang/protobuf/ptypes/timestamp"
|
|
|
|
|
|
|
|
sppb "google.golang.org/genproto/googleapis/spanner/v1"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Action is a mocked RPC activity that MockCloudSpannerClient will take.
|
|
|
|
type Action struct {
|
2018-01-23 18:40:42 +00:00
|
|
|
Method string
|
|
|
|
Err error
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MockCloudSpannerClient is a mock implementation of sppb.SpannerClient.
|
|
|
|
type MockCloudSpannerClient struct {
|
2018-01-23 18:40:42 +00:00
|
|
|
sppb.SpannerClient
|
|
|
|
|
2017-08-05 18:17:15 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
t *testing.T
|
|
|
|
// Live sessions on the client.
|
|
|
|
sessions map[string]bool
|
|
|
|
// Expected set of actions that will be executed by the client.
|
|
|
|
actions []Action
|
|
|
|
// Session ping history.
|
|
|
|
pings []string
|
|
|
|
// Injected error, will be returned by all APIs.
|
|
|
|
injErr map[string]error
|
|
|
|
// Client will not fail on any request.
|
|
|
|
nice bool
|
|
|
|
// Client will stall on any requests.
|
|
|
|
freezed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMockCloudSpannerClient creates new MockCloudSpannerClient instance.
|
|
|
|
func NewMockCloudSpannerClient(t *testing.T, acts ...Action) *MockCloudSpannerClient {
|
|
|
|
mc := &MockCloudSpannerClient{t: t, sessions: map[string]bool{}, injErr: map[string]error{}}
|
|
|
|
mc.SetActions(acts...)
|
|
|
|
// Produce a closed channel, so the default action of ready is to not block.
|
|
|
|
mc.Freeze()
|
|
|
|
mc.Unfreeze()
|
|
|
|
return mc
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeNice makes this a nice mock which will not fail on any request.
|
|
|
|
func (m *MockCloudSpannerClient) MakeNice() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.nice = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeStrict makes this a strict mock which will fail on any unexpected request.
|
|
|
|
func (m *MockCloudSpannerClient) MakeStrict() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.nice = false
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// InjectError injects a global error that will be returned by all calls to method
|
|
|
|
// regardless of the actions array.
|
2017-08-05 18:17:15 +00:00
|
|
|
func (m *MockCloudSpannerClient) InjectError(method string, err error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.injErr[method] = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetActions sets the new set of expected actions to MockCloudSpannerClient.
|
|
|
|
func (m *MockCloudSpannerClient) SetActions(acts ...Action) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-01-23 18:40:42 +00:00
|
|
|
m.actions = nil
|
2017-08-05 18:17:15 +00:00
|
|
|
for _, act := range acts {
|
|
|
|
m.actions = append(m.actions, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpPings dumps the ping history.
|
|
|
|
func (m *MockCloudSpannerClient) DumpPings() []string {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return append([]string(nil), m.pings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpSessions dumps the internal session table.
|
|
|
|
func (m *MockCloudSpannerClient) DumpSessions() map[string]bool {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
st := map[string]bool{}
|
|
|
|
for s, v := range m.sessions {
|
|
|
|
st[s] = v
|
|
|
|
}
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSession is a placeholder for SpannerClient.CreateSession.
|
|
|
|
func (m *MockCloudSpannerClient) CreateSession(c context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if err := m.injErr["CreateSession"]; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &sppb.Session{}
|
|
|
|
if r.Database != "mockdb" {
|
|
|
|
// Reject other databases
|
2018-03-30 09:41:12 +00:00
|
|
|
return s, status.Errorf(codes.NotFound, fmt.Sprintf("database not found: %v", r.Database))
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
// Generate & record session name.
|
|
|
|
s.Name = fmt.Sprintf("mockdb-%v", time.Now().UnixNano())
|
|
|
|
m.sessions[s.Name] = true
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession is a placeholder for SpannerClient.GetSession.
|
|
|
|
func (m *MockCloudSpannerClient) GetSession(c context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if err := m.injErr["GetSession"]; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.pings = append(m.pings, r.Name)
|
|
|
|
if _, ok := m.sessions[r.Name]; !ok {
|
2018-03-30 09:41:12 +00:00
|
|
|
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Session not found: %v", r.Name))
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
return &sppb.Session{Name: r.Name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSession is a placeholder for SpannerClient.DeleteSession.
|
|
|
|
func (m *MockCloudSpannerClient) DeleteSession(c context.Context, r *sppb.DeleteSessionRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if err := m.injErr["DeleteSession"]; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, ok := m.sessions[r.Name]; !ok {
|
|
|
|
// Session not found.
|
2018-03-30 09:41:12 +00:00
|
|
|
return &empty.Empty{}, status.Errorf(codes.NotFound, fmt.Sprintf("Session not found: %v", r.Name))
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
// Delete session from in-memory table.
|
|
|
|
delete(m.sessions, r.Name)
|
|
|
|
return &empty.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteStreamingSql is a mock implementation of SpannerClient.ExecuteStreamingSql.
|
|
|
|
func (m *MockCloudSpannerClient) ExecuteStreamingSql(c context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (sppb.Spanner_ExecuteStreamingSqlClient, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-01-23 18:40:42 +00:00
|
|
|
act, err := m.expectAction("ExecuteStreamingSql")
|
|
|
|
if err != nil {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wantReq := &sppb.ExecuteSqlRequest{
|
|
|
|
Session: "mocksession",
|
|
|
|
Transaction: &sppb.TransactionSelector{
|
|
|
|
Selector: &sppb.TransactionSelector_SingleUse{
|
|
|
|
SingleUse: &sppb.TransactionOptions{
|
|
|
|
Mode: &sppb.TransactionOptions_ReadOnly_{
|
|
|
|
ReadOnly: &sppb.TransactionOptions_ReadOnly{
|
|
|
|
TimestampBound: &sppb.TransactionOptions_ReadOnly_Strong{
|
|
|
|
Strong: true,
|
|
|
|
},
|
|
|
|
ReturnReadTimestamp: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Sql: "mockquery",
|
|
|
|
Params: &proto3.Struct{
|
|
|
|
Fields: map[string]*proto3.Value{"var1": &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: "abc"}}},
|
|
|
|
},
|
|
|
|
ParamTypes: map[string]*sppb.Type{"var1": &sppb.Type{Code: sppb.TypeCode_STRING}},
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if !proto.Equal(r, wantReq) {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, fmt.Errorf("got query request: %v, want: %v", r, wantReq)
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Err != nil {
|
|
|
|
return nil, act.Err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("query never succeeds on mock client")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamingRead is a placeholder for SpannerClient.StreamingRead.
|
|
|
|
func (m *MockCloudSpannerClient) StreamingRead(c context.Context, r *sppb.ReadRequest, opts ...grpc.CallOption) (sppb.Spanner_StreamingReadClient, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-01-23 18:40:42 +00:00
|
|
|
act, err := m.expectAction("StreamingRead", "StreamingReadIndex")
|
|
|
|
if err != nil {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wantReq := &sppb.ReadRequest{
|
|
|
|
Session: "mocksession",
|
|
|
|
Transaction: &sppb.TransactionSelector{
|
|
|
|
Selector: &sppb.TransactionSelector_SingleUse{
|
|
|
|
SingleUse: &sppb.TransactionOptions{
|
|
|
|
Mode: &sppb.TransactionOptions_ReadOnly_{
|
|
|
|
ReadOnly: &sppb.TransactionOptions_ReadOnly{
|
|
|
|
TimestampBound: &sppb.TransactionOptions_ReadOnly_Strong{
|
|
|
|
Strong: true,
|
|
|
|
},
|
|
|
|
ReturnReadTimestamp: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Table: "t_mock",
|
|
|
|
Columns: []string{"col1", "col2"},
|
|
|
|
KeySet: &sppb.KeySet{
|
2017-09-13 12:09:48 +00:00
|
|
|
Keys: []*proto3.ListValue{
|
2017-08-05 18:17:15 +00:00
|
|
|
&proto3.ListValue{
|
|
|
|
Values: []*proto3.Value{
|
|
|
|
&proto3.Value{Kind: &proto3.Value_StringValue{StringValue: "foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-13 12:09:48 +00:00
|
|
|
Ranges: []*sppb.KeyRange{},
|
|
|
|
All: false,
|
2017-08-05 18:17:15 +00:00
|
|
|
},
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Method == "StreamingIndexRead" {
|
2017-08-05 18:17:15 +00:00
|
|
|
wantReq.Index = "idx1"
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if !proto.Equal(r, wantReq) {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, fmt.Errorf("got query request: %v, want: %v", r, wantReq)
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Err != nil {
|
|
|
|
return nil, act.Err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("read never succeeds on mock client")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginTransaction is a placeholder for SpannerClient.BeginTransaction.
|
|
|
|
func (m *MockCloudSpannerClient) BeginTransaction(c context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if !m.nice {
|
2018-01-23 18:40:42 +00:00
|
|
|
act, err := m.expectAction("BeginTransaction")
|
|
|
|
if err != nil {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Err != nil {
|
|
|
|
return nil, act.Err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resp := &sppb.Transaction{Id: []byte("transaction-1")}
|
|
|
|
if _, ok := r.Options.Mode.(*sppb.TransactionOptions_ReadOnly_); ok {
|
|
|
|
resp.ReadTimestamp = &pbt.Timestamp{Seconds: 3, Nanos: 4}
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit is a placeholder for SpannerClient.Commit.
|
|
|
|
func (m *MockCloudSpannerClient) Commit(c context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if !m.nice {
|
2018-01-23 18:40:42 +00:00
|
|
|
act, err := m.expectAction("Commit")
|
|
|
|
if err != nil {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Err != nil {
|
|
|
|
return nil, act.Err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &sppb.CommitResponse{CommitTimestamp: &pbt.Timestamp{Seconds: 1, Nanos: 2}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback is a placeholder for SpannerClient.Rollback.
|
|
|
|
func (m *MockCloudSpannerClient) Rollback(c context.Context, r *sppb.RollbackRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
|
|
|
|
m.ready()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if !m.nice {
|
2018-01-23 18:40:42 +00:00
|
|
|
act, err := m.expectAction("Rollback")
|
|
|
|
if err != nil {
|
2017-08-05 18:17:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
if act.Err != nil {
|
|
|
|
return nil, act.Err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-30 09:41:12 +00:00
|
|
|
// PartitionQuery is a placeholder for SpannerServer.PartitionQuery.
|
|
|
|
func (m *MockCloudSpannerClient) PartitionQuery(ctx context.Context, r *sppb.PartitionQueryRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error) {
|
|
|
|
m.ready()
|
|
|
|
return nil, errors.New("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionRead is a placeholder for SpannerServer.PartitionRead.
|
|
|
|
func (m *MockCloudSpannerClient) PartitionRead(ctx context.Context, r *sppb.PartitionReadRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error) {
|
|
|
|
m.ready()
|
|
|
|
return nil, errors.New("Unimplemented")
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
func (m *MockCloudSpannerClient) expectAction(methods ...string) (Action, error) {
|
|
|
|
for _, me := range methods {
|
|
|
|
if err := m.injErr[me]; err != nil {
|
|
|
|
return Action{}, err
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
}
|
|
|
|
if len(m.actions) == 0 {
|
|
|
|
m.t.Fatalf("unexpected %v executed", methods)
|
|
|
|
}
|
|
|
|
act := m.actions[0]
|
|
|
|
m.actions = m.actions[1:]
|
|
|
|
for _, me := range methods {
|
|
|
|
if me == act.Method {
|
|
|
|
return act, nil
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
m.t.Fatalf("unexpected call of one of %v, want method %s", methods, act.Method)
|
|
|
|
return Action{}, nil
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Freeze stalls all requests.
|
|
|
|
func (m *MockCloudSpannerClient) Freeze() {
|
2018-01-23 18:40:42 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-08-05 18:17:15 +00:00
|
|
|
m.freezed = make(chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfreeze restores processing requests.
|
|
|
|
func (m *MockCloudSpannerClient) Unfreeze() {
|
2018-01-23 18:40:42 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-08-05 18:17:15 +00:00
|
|
|
close(m.freezed)
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// CheckActionsConsumed checks that all actions have been consumed.
|
|
|
|
func (m *MockCloudSpannerClient) CheckActionsConsumed() {
|
|
|
|
if len(m.actions) != 0 {
|
|
|
|
m.t.Fatalf("unconsumed mock client actions: %v", m.actions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:17:15 +00:00
|
|
|
// ready checks conditions before executing requests
|
2018-01-23 18:40:42 +00:00
|
|
|
// TODO: add checks for injected errors, actions
|
2017-08-05 18:17:15 +00:00
|
|
|
func (m *MockCloudSpannerClient) ready() {
|
2018-01-23 18:40:42 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
freezed := m.freezed
|
|
|
|
m.mu.Unlock()
|
2017-08-05 18:17:15 +00:00
|
|
|
// check if client should be freezed
|
2018-01-23 18:40:42 +00:00
|
|
|
<-freezed
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|