-
Notifications
You must be signed in to change notification settings - Fork 8
/
mockserver.go
138 lines (119 loc) · 4.95 KB
/
mockserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2022 Google LLC
//
// 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
//
// https://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.
// This file is copied from
// https://github.com/googleapis/google-cloud-go/blob/main/bigtable/internal/mockserver/inmem.go
// with minor modification.
package tests
import (
"context"
"net"
btpb "google.golang.org/genproto/googleapis/bigtable/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Server is an in-memory Cloud Bigtable fake.
// It is unauthenticated, and only a rough approximation.
type Server struct {
Addr string
l net.Listener
srv *grpc.Server
// Any unimplemented methods will cause a panic when called.
btpb.BigtableServer
// Assign new functions to these parameters to implement specific mock
// functionality.
// ReadRowsFn mocks ReadRows.
ReadRowsFn func(*btpb.ReadRowsRequest, btpb.Bigtable_ReadRowsServer) error
// SampleRowKeysFn mocks SampleRowKeys.
SampleRowKeysFn func(*btpb.SampleRowKeysRequest, btpb.Bigtable_SampleRowKeysServer) error
// MutateRowFn mocks MutateRow.
MutateRowFn func(context.Context, *btpb.MutateRowRequest) (*btpb.MutateRowResponse, error)
// MutateRowsFn mocks MutateRows.
MutateRowsFn func(*btpb.MutateRowsRequest, btpb.Bigtable_MutateRowsServer) error
// CheckAndMutateRowFn mocks CheckAndMutateRow.
CheckAndMutateRowFn func(context.Context, *btpb.CheckAndMutateRowRequest) (*btpb.CheckAndMutateRowResponse, error)
// ReadModifyWriteRowFn mocks ReadModifyWriteRow.
ReadModifyWriteRowFn func(context.Context, *btpb.ReadModifyWriteRowRequest) (*btpb.ReadModifyWriteRowResponse, error)
}
// NewServer creates a new Server.
// The Server will be listening for gRPC connections, without TLS,
// on the provided address. The resolved address is named by the Addr field.
func NewServer(laddr string, opt ...grpc.ServerOption) (*Server, error) {
l, err := net.Listen("tcp", laddr)
if err != nil {
return nil, err
}
srv := grpc.NewServer(opt...)
s := &Server{
Addr: l.Addr().String(),
l: l,
srv: srv,
}
return s, nil
}
// Start starts the server
func (s *Server) Start() {
btpb.RegisterBigtableServer(s.srv, s)
go s.srv.Serve(s.l)
}
// Close closes the server.
func (s *Server) Close() error {
if err := s.l.Close(); err != nil {
return err
}
s.srv.Stop()
return nil
}
// ReadRows implements ReadRows of the BigtableServer interface.
func (s *Server) ReadRows(req *btpb.ReadRowsRequest, srv btpb.Bigtable_ReadRowsServer) error {
if s.ReadRowsFn != nil {
return s.ReadRowsFn(req, srv)
}
return status.Error(codes.Unimplemented, "unimplemented - you need to attach a ReadRowsFn to the server")
}
// SampleRowKeys implements SampleRowKeys of the BigtableServer interface.
func (s *Server) SampleRowKeys(req *btpb.SampleRowKeysRequest, srv btpb.Bigtable_SampleRowKeysServer) error {
if s.SampleRowKeysFn != nil {
return s.SampleRowKeysFn(req, srv)
}
return status.Error(codes.Unimplemented, "unimplemented - you need to attach a SampleRowKeysFn to the server")
}
// MutateRow implements MutateRow of the BigtableServer interface.
func (s *Server) MutateRow(ctx context.Context, req *btpb.MutateRowRequest) (*btpb.MutateRowResponse, error) {
if s.MutateRowFn != nil {
return s.MutateRowFn(ctx, req)
}
return nil, status.Error(codes.Unimplemented, "unimplemented - you need to attach a MutateRowFn to the server")
}
// MutateRows implements MutateRows of the BigtableServer interface.
func (s *Server) MutateRows(req *btpb.MutateRowsRequest, srv btpb.Bigtable_MutateRowsServer) error {
if s.MutateRowsFn != nil {
return s.MutateRowsFn(req, srv)
}
return status.Error(codes.Unimplemented, "unimplemented - you need to attach a MutateRowsFn to the server")
}
// CheckAndMutateRow implements CheckAndMutateRow of the BigtableServer interface.
func (s *Server) CheckAndMutateRow(ctx context.Context, srv *btpb.CheckAndMutateRowRequest) (*btpb.CheckAndMutateRowResponse, error) {
if s.CheckAndMutateRowFn != nil {
return s.CheckAndMutateRowFn(ctx, srv)
}
return nil, status.Error(codes.Unimplemented, "unimplemented - you need to attach a CheckAndMutateRowFn to the server")
}
// ReadModifyWriteRow implements ReadModifyWriteRow of the BigtableServer interface.
func (s *Server) ReadModifyWriteRow(ctx context.Context, srv *btpb.ReadModifyWriteRowRequest) (*btpb.ReadModifyWriteRowResponse, error) {
if s.ReadModifyWriteRowFn != nil {
return s.ReadModifyWriteRowFn(ctx, srv)
}
return nil, status.Error(codes.Unimplemented, "unimplemented - you need to attach a ReadModifyWriteRowFn to the server")
}