16
16
using Google . Api . Gax . Grpc ;
17
17
using Google . Apis . Auth . OAuth2 ;
18
18
using Google . Cloud . Bigtable . Common . V2 ;
19
+ using Google . Cloud . Bigtable . V2 ;
19
20
using Grpc . Auth ;
20
21
using Grpc . Core ;
21
- using System ;
22
- using System . Collections . Generic ;
23
- using System . Linq ;
24
- using System . Threading . Tasks ;
25
22
26
- namespace Google . Cloud . Bigtable . V2 . ConformanceTests ;
23
+ namespace google . bigtable . testproxy ;
27
24
28
25
public sealed class CloudBigtableV2TestProxyImpl : CloudBigtableV2TestProxy . CloudBigtableV2TestProxyBase
29
26
{
30
27
private class CbtClient
31
28
{
32
- public BigtableClient Client { get ; set ; }
33
- public ChannelBase LastCreatedChannel { get ; set ; }
34
- public InstanceName InstanceName { get ; set ; }
29
+ public BigtableClient Client { get ; private set ; }
30
+ public ChannelBase LastCreatedChannel { get ; private set ; }
31
+ public InstanceName InstanceName { get ; private set ; }
32
+
33
+ public CbtClient ( BigtableClient bigtableClient , ChannelBase channelBase , InstanceName instanceName )
34
+ {
35
+ Client = bigtableClient ;
36
+ LastCreatedChannel = channelBase ;
37
+ InstanceName = instanceName ;
38
+ }
35
39
}
36
40
37
- private readonly Dictionary < string , CbtClient > _idClientMap ;
41
+ private static readonly Dictionary < string , CbtClient > _idClientMap = new Dictionary < string , CbtClient > ( ) ;
38
42
39
43
public override async Task < CreateClientResponse > CreateClient ( CreateClientRequest request , ServerCallContext context )
40
44
{
@@ -48,15 +52,17 @@ public override async Task<CreateClientResponse> CreateClient(CreateClientReques
48
52
GaxPreconditions . CheckArgument ( projectId is not ( "" or null ) , "ProjectId" , "project id must be provided" ) ;
49
53
GaxPreconditions . CheckArgument ( instanceId is not ( "" or null ) , "InstanceId" , "instance id must be provided" ) ;
50
54
GaxPreconditions . CheckArgument ( dataTarget is not ( "" or null ) , "DataTarget" , "data target must be provided" ) ;
51
- GaxPreconditions . CheckArgument ( ! securityOptions . UseSsl
52
- || ( securityOptions . SslRootCertsPem is not ( "" or null ) ) , "SecurityOptions" ,
55
+ GaxPreconditions . CheckArgument ( securityOptions is null || ( ! securityOptions . UseSsl
56
+ || ( securityOptions . SslRootCertsPem is not ( "" or null ) ) ) , "SecurityOptions" ,
53
57
"security_options.ssl_root_certs_pem must be provided if security_options.use_ssl is true" ) ;
54
58
59
+ #pragma warning disable CS8604 // Possible null reference argument.
55
60
if ( _idClientMap . ContainsKey ( clientId ) )
56
61
{
57
62
context . Status = new Status ( StatusCode . AlreadyExists , $ "Client { clientId } already exists") ;
58
63
throw new RpcException ( context . Status ) ;
59
64
}
65
+ #pragma warning restore CS8604 // Possible null reference argument.
60
66
61
67
try
62
68
{
@@ -74,27 +80,25 @@ public override async Task<CreateClientResponse> CreateClient(CreateClientReques
74
80
{
75
81
Settings = settings
76
82
} ;
77
- if ( dataTarget != "emulator" )
83
+ if ( dataTarget != "emulator" && securityOptions is not null )
78
84
{
79
85
builder . Endpoint = dataTarget ;
86
+ #pragma warning disable CS8604 // Possible null reference argument.
80
87
builder . ChannelCredentials = GetChannelCredentials ( securityOptions . UseSsl , securityOptions . SslRootCertsPem , securityOptions . AccessToken ) ;
88
+ #pragma warning restore CS8604 // Possible null reference argument.
81
89
builder . GrpcChannelOptions = ( securityOptions . UseSsl && securityOptions . SslEndpointOverride is not null )
82
90
? GrpcChannelOptions . Empty . WithCustomOption ( "grpc.ssl_target_name_override" , securityOptions . SslEndpointOverride )
83
91
: GrpcChannelOptions . Empty ;
84
92
}
85
93
else
86
94
{
95
+ Environment . SetEnvironmentVariable ( "BIGTABLE_EMULATOR_HOST" , dataTarget ) ;
87
96
builder . EmulatorDetection = EmulatorDetection . EmulatorOnly ;
88
97
}
89
98
InstanceName instanceName = new InstanceName ( projectId , instanceId ) ;
90
99
BigtableServiceApiClient apiClient = await builder . BuildAsync ( ) ;
91
100
92
- CbtClient cbtClient = new CbtClient
93
- {
94
- Client = BigtableClient . Create ( apiClient ) ,
95
- LastCreatedChannel = builder . LastCreatedChannel ,
96
- InstanceName = instanceName
97
- } ;
101
+ CbtClient cbtClient = new CbtClient ( BigtableClient . Create ( apiClient ) , builder . LastCreatedChannel , instanceName ) ;
98
102
_idClientMap [ clientId ] = cbtClient ;
99
103
}
100
104
catch ( Exception e )
@@ -111,7 +115,10 @@ public override async Task<CloseClientResponse> CloseClient(CloseClientRequest r
111
115
CbtClient cbtClient = GetClient ( request . ClientId , context ) ;
112
116
try
113
117
{
114
- await cbtClient . LastCreatedChannel . ShutdownAsync ( ) ;
118
+ if ( cbtClient . LastCreatedChannel is not null )
119
+ {
120
+ await cbtClient . LastCreatedChannel . ShutdownAsync ( ) ;
121
+ }
115
122
}
116
123
catch ( Exception e )
117
124
{
@@ -126,7 +133,9 @@ public override Task<RemoveClientResponse> RemoveClient(RemoveClientRequest requ
126
133
{
127
134
string clientId = request . ClientId ;
128
135
GaxPreconditions . CheckArgument ( clientId is not ( "" or null ) , "ClientId" , "client id must be provided" , context ) ;
136
+ #pragma warning disable CS8604 // Possible null reference argument.
129
137
bool removed = _idClientMap . Remove ( clientId ) ;
138
+ #pragma warning restore CS8604 // Possible null reference argument.
130
139
if ( ! removed )
131
140
{
132
141
context . Status = new Status ( StatusCode . NotFound , $ "Client { clientId } not found.") ;
@@ -145,9 +154,9 @@ public override async Task<RowResult> ReadRow(ReadRowRequest request, ServerCall
145
154
context . Status = new Status ( StatusCode . InvalidArgument , "Invalid TableName" ) ;
146
155
return new RowResult
147
156
{
148
- Status = new Rpc . Status ( )
157
+ Status = new Google . Rpc . Status ( )
149
158
{
150
- Code = ( int ) Rpc . Code . InvalidArgument ,
159
+ Code = ( int ) Google . Rpc . Code . InvalidArgument ,
151
160
Message = "Invalid TableName"
152
161
}
153
162
} ;
@@ -182,9 +191,13 @@ public override async Task<RowsResult> ReadRows(ReadRowsRequest request, ServerC
182
191
ReadRowsStream stream = cbtClient . Client . ReadRows ( request . Request ) ;
183
192
IAsyncEnumerator < Row > enumerator = stream . GetAsyncEnumerator ( new System . Threading . CancellationToken ( false ) ) ;
184
193
RowsResult rowsResult = new RowsResult ( ) ;
185
- while ( await enumerator . MoveNextAsync ( ) )
194
+ while ( enumerator . Current is not null )
186
195
{
187
196
rowsResult . Rows . Add ( enumerator . Current ) ;
197
+ if ( ! await enumerator . MoveNextAsync ( ) )
198
+ {
199
+ break ;
200
+ }
188
201
}
189
202
string message = rowsResult . Rows . Count == 0 ? $ "ReadRows didn't find rows" : "ReadRows succeeded" ;
190
203
rowsResult . Status = SetSuccessStatus ( message , context ) ;
@@ -258,10 +271,12 @@ public override async Task<CheckAndMutateRowResult> CheckAndMutateRow(CheckAndMu
258
271
}
259
272
catch ( Exception e )
260
273
{
261
- return new CheckAndMutateRowResult
274
+ CheckAndMutateRowResult res = new CheckAndMutateRowResult
262
275
{
276
+ Result = new CheckAndMutateRowResponse ( ) ,
263
277
Status = SetExceptionStatus ( e , context )
264
278
} ;
279
+ return res ;
265
280
}
266
281
}
267
282
@@ -320,7 +335,7 @@ public override async Task<ExecuteQueryResult> ExecuteQuery(ExecuteQueryRequest
320
335
Metadata = new ResultSetMetadata ( )
321
336
} ;
322
337
IEnumerable < byte > bytes = Enumerable . Empty < byte > ( ) ;
323
- while ( await enumerator . MoveNextAsync ( ) )
338
+ while ( enumerator . Current is not null )
324
339
{
325
340
ExecuteQueryResponse response = enumerator . Current ;
326
341
if ( response . ResponseCase == ExecuteQueryResponse . ResponseOneofCase . Metadata )
@@ -340,6 +355,10 @@ public override async Task<ExecuteQueryResult> ExecuteQuery(ExecuteQueryRequest
340
355
{
341
356
break ;
342
357
}
358
+ if ( ! await enumerator . MoveNextAsync ( ) )
359
+ {
360
+ break ;
361
+ }
343
362
}
344
363
result . Status = SetSuccessStatus ( "ExecuteQuery succeeded" , context ) ;
345
364
return result ;
@@ -355,17 +374,17 @@ public override async Task<ExecuteQueryResult> ExecuteQuery(ExecuteQueryRequest
355
374
356
375
public static CloudBigtableV2TestProxyImpl Create ( ) => new ( ) ;
357
376
358
- private CloudBigtableV2TestProxyImpl ( ) => _idClientMap = new ( ) ;
359
-
360
377
private CbtClient GetClient ( string clientId , ServerCallContext context )
361
378
{
362
379
GaxPreconditions . CheckArgument ( clientId is not ( "" or null ) , "ClientId" , "client id must be provided" , context ) ;
363
380
381
+ #pragma warning disable CS8604 // Possible null reference argument.
364
382
if ( ! _idClientMap . ContainsKey ( clientId ) )
365
383
{
366
384
context . Status = new Status ( StatusCode . NotFound , $ "Client { clientId } not found.") ;
367
385
throw new RpcException ( context . Status ) ;
368
386
}
387
+ #pragma warning restore CS8604 // Possible null reference argument.
369
388
return _idClientMap [ clientId ] ;
370
389
}
371
390
@@ -394,22 +413,22 @@ private static ChannelCredentials GetChannelCredentials(bool encrypted, string r
394
413
return encrypted ? new SslCredentials ( rootCertsPem ) : ChannelCredentials . Insecure ;
395
414
}
396
415
397
- private static Rpc . Status SetExceptionStatus ( Exception e , ServerCallContext context )
416
+ private static Google . Rpc . Status SetExceptionStatus ( Exception e , ServerCallContext context )
398
417
{
399
418
context . Status = new Status ( StatusCode . Internal , e . Message , e ) ;
400
- return new Rpc . Status ( )
419
+ return new Google . Rpc . Status ( )
401
420
{
402
- Code = ( int ) Rpc . Code . Internal ,
421
+ Code = ( int ) Google . Rpc . Code . Internal ,
403
422
Message = e . Message
404
423
} ;
405
424
}
406
425
407
- private static Rpc . Status SetSuccessStatus ( string message , ServerCallContext context )
426
+ private static Google . Rpc . Status SetSuccessStatus ( string message , ServerCallContext context )
408
427
{
409
428
context . Status = new Status ( StatusCode . OK , message ) ;
410
- return new Rpc . Status ( )
429
+ return new Google . Rpc . Status ( )
411
430
{
412
- Code = ( int ) Rpc . Code . Ok ,
431
+ Code = ( int ) Google . Rpc . Code . Ok ,
413
432
Message = message
414
433
} ;
415
434
}
0 commit comments