17
17
package com .google .cloud .spanner ;
18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
+ import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .assertTrue ;
22
+ import static org .junit .Assert .fail ;
20
23
import static org .mockito .Mockito .verify ;
21
24
import static org .mockito .Mockito .when ;
22
25
import static org .mockito .MockitoAnnotations .initMocks ;
@@ -98,14 +101,24 @@ private com.google.spanner.admin.instance.v1.Instance getInstanceProto() {
98
101
.setConfig (CONFIG_NAME )
99
102
.setName (INSTANCE_NAME )
100
103
.setNodeCount (1 )
104
+ .setProcessingUnits (1000 )
105
+ .build ();
106
+ }
107
+
108
+ private com .google .spanner .admin .instance .v1 .Instance getInstanceProtoWithProcessingUnits () {
109
+ return com .google .spanner .admin .instance .v1 .Instance .newBuilder ()
110
+ .setConfig (CONFIG_NAME )
111
+ .setName (INSTANCE_NAME )
112
+ .setProcessingUnits (10 )
101
113
.build ();
102
114
}
103
115
104
116
private com .google .spanner .admin .instance .v1 .Instance getAnotherInstanceProto () {
105
117
return com .google .spanner .admin .instance .v1 .Instance .newBuilder ()
106
118
.setConfig (CONFIG_NAME )
107
119
.setName (INSTANCE_NAME2 )
108
- .setNodeCount (1 )
120
+ .setNodeCount (2 )
121
+ .setProcessingUnits (2000 )
109
122
.build ();
110
123
}
111
124
@@ -115,7 +128,10 @@ public void createInstance() throws Exception {
115
128
rawOperationFuture =
116
129
OperationFutureUtil .immediateOperationFuture (
117
130
"createInstance" , getInstanceProto (), CreateInstanceMetadata .getDefaultInstance ());
118
- when (rpc .createInstance ("projects/" + PROJECT_ID , INSTANCE_ID , getInstanceProto ()))
131
+ when (rpc .createInstance (
132
+ "projects/" + PROJECT_ID ,
133
+ INSTANCE_ID ,
134
+ getInstanceProto ().toBuilder ().setProcessingUnits (0 ).build ()))
119
135
.thenReturn (rawOperationFuture );
120
136
OperationFuture <Instance , CreateInstanceMetadata > op =
121
137
client .createInstance (
@@ -128,9 +144,50 @@ public void createInstance() throws Exception {
128
144
}
129
145
130
146
@ Test
131
- public void getInstance () {
147
+ public void testCreateInstanceWithProcessingUnits () throws Exception {
148
+ OperationFuture <com .google .spanner .admin .instance .v1 .Instance , CreateInstanceMetadata >
149
+ rawOperationFuture =
150
+ OperationFutureUtil .immediateOperationFuture (
151
+ "createInstance" ,
152
+ getInstanceProtoWithProcessingUnits (),
153
+ CreateInstanceMetadata .getDefaultInstance ());
154
+ when (rpc .createInstance (
155
+ "projects/" + PROJECT_ID , INSTANCE_ID , getInstanceProtoWithProcessingUnits ()))
156
+ .thenReturn (rawOperationFuture );
157
+ OperationFuture <Instance , CreateInstanceMetadata > operation =
158
+ client .createInstance (
159
+ InstanceInfo .newBuilder (InstanceId .of (PROJECT_ID , INSTANCE_ID ))
160
+ .setInstanceConfigId (InstanceConfigId .of (PROJECT_ID , CONFIG_ID ))
161
+ .setProcessingUnits (10 )
162
+ .build ());
163
+ assertTrue (operation .isDone ());
164
+ assertEquals (INSTANCE_NAME , operation .get ().getId ().getName ());
165
+ }
166
+
167
+ @ Test
168
+ public void testCreateInstanceWithBothNodeCountAndProcessingUnits () throws Exception {
169
+ try {
170
+ client .createInstance (
171
+ InstanceInfo .newBuilder (InstanceId .of (PROJECT_ID , INSTANCE_ID ))
172
+ .setInstanceConfigId (InstanceConfigId .of (PROJECT_ID , CONFIG_ID ))
173
+ .setNodeCount (1 )
174
+ .setProcessingUnits (100 )
175
+ .build ());
176
+ fail ("missing expected exception" );
177
+ } catch (IllegalArgumentException e ) {
178
+ assertTrue (
179
+ e .getMessage ()
180
+ .contains (
181
+ "Only one of nodeCount and processingUnits can be set when creating a new instance" ));
182
+ }
183
+ }
184
+
185
+ @ Test
186
+ public void testGetInstance () {
132
187
when (rpc .getInstance (INSTANCE_NAME )).thenReturn (getInstanceProto ());
133
- assertThat (client .getInstance (INSTANCE_ID ).getId ().getName ()).isEqualTo (INSTANCE_NAME );
188
+ Instance instance = client .getInstance (INSTANCE_ID );
189
+ assertEquals (INSTANCE_NAME , instance .getId ().getName ());
190
+ assertEquals (1000 , instance .getProcessingUnits ());
134
191
}
135
192
136
193
@ Test
@@ -165,7 +222,80 @@ public void updateInstanceMetadata() throws Exception {
165
222
}
166
223
167
224
@ Test
168
- public void listInstances () {
225
+ public void testUpdateInstanceProcessingUnits () throws Exception {
226
+ com .google .spanner .admin .instance .v1 .Instance instance =
227
+ com .google .spanner .admin .instance .v1 .Instance .newBuilder ()
228
+ .setName (INSTANCE_NAME )
229
+ .setConfig (CONFIG_NAME )
230
+ .setProcessingUnits (10 )
231
+ .build ();
232
+ OperationFuture <com .google .spanner .admin .instance .v1 .Instance , UpdateInstanceMetadata >
233
+ rawOperationFuture =
234
+ OperationFutureUtil .immediateOperationFuture (
235
+ "updateInstance" ,
236
+ getInstanceProtoWithProcessingUnits (),
237
+ UpdateInstanceMetadata .getDefaultInstance ());
238
+ when (rpc .updateInstance (instance , FieldMask .newBuilder ().addPaths ("processing_units" ).build ()))
239
+ .thenReturn (rawOperationFuture );
240
+ InstanceInfo instanceInfo =
241
+ InstanceInfo .newBuilder (InstanceId .of (INSTANCE_NAME ))
242
+ .setInstanceConfigId (InstanceConfigId .of (CONFIG_NAME ))
243
+ .setProcessingUnits (10 )
244
+ .build ();
245
+ OperationFuture <Instance , UpdateInstanceMetadata > operationWithFieldMask =
246
+ client .updateInstance (instanceInfo , InstanceInfo .InstanceField .PROCESSING_UNITS );
247
+ assertTrue (operationWithFieldMask .isDone ());
248
+ assertEquals (INSTANCE_NAME , operationWithFieldMask .get ().getId ().getName ());
249
+
250
+ when (rpc .updateInstance (
251
+ instance ,
252
+ FieldMask .newBuilder ()
253
+ .addAllPaths (Arrays .asList ("display_name" , "processing_units" , "labels" ))
254
+ .build ()))
255
+ .thenReturn (rawOperationFuture );
256
+ OperationFuture <Instance , UpdateInstanceMetadata > operation =
257
+ client .updateInstance (instanceInfo );
258
+ assertTrue (operation .isDone ());
259
+ assertEquals (INSTANCE_NAME , operation .get ().getId ().getName ());
260
+ }
261
+
262
+ @ Test
263
+ public void testUpdateInstanceWithNodeCountAndProcessingUnits () throws Exception {
264
+ com .google .spanner .admin .instance .v1 .Instance instance =
265
+ com .google .spanner .admin .instance .v1 .Instance .newBuilder ()
266
+ .setName (INSTANCE_NAME )
267
+ .setConfig (CONFIG_NAME )
268
+ .setNodeCount (3 )
269
+ .setProcessingUnits (3000 )
270
+ .build ();
271
+ OperationFuture <com .google .spanner .admin .instance .v1 .Instance , UpdateInstanceMetadata >
272
+ rawOperationFuture =
273
+ OperationFutureUtil .immediateOperationFuture (
274
+ "updateInstance" ,
275
+ getInstanceProtoWithProcessingUnits (),
276
+ UpdateInstanceMetadata .getDefaultInstance ());
277
+ // node_count should take precedence over processing_units when node_count>0 and no specific
278
+ // field mask is set by the caller.
279
+ when (rpc .updateInstance (
280
+ instance ,
281
+ FieldMask .newBuilder ()
282
+ .addAllPaths (Arrays .asList ("display_name" , "node_count" , "labels" ))
283
+ .build ()))
284
+ .thenReturn (rawOperationFuture );
285
+ InstanceInfo instanceInfo =
286
+ InstanceInfo .newBuilder (InstanceId .of (INSTANCE_NAME ))
287
+ .setInstanceConfigId (InstanceConfigId .of (CONFIG_NAME ))
288
+ .setNodeCount (3 )
289
+ .setProcessingUnits (3000 )
290
+ .build ();
291
+ OperationFuture <Instance , UpdateInstanceMetadata > operationWithFieldMask =
292
+ client .updateInstance (instanceInfo );
293
+ assertTrue (operationWithFieldMask .isDone ());
294
+ assertEquals (INSTANCE_NAME , operationWithFieldMask .get ().getId ().getName ());
295
+ }
296
+
297
+ @ Test
298
+ public void testListInstances () {
169
299
String nextToken = "token" ;
170
300
String filter = "env:dev" ;
171
301
when (rpc .listInstances (1 , null , filter ))
@@ -175,9 +305,11 @@ public void listInstances() {
175
305
List <Instance > instances =
176
306
Lists .newArrayList (
177
307
client .listInstances (Options .pageSize (1 ), Options .filter (filter )).iterateAll ());
178
- assertThat (instances .get (0 ).getId ().getName ()).isEqualTo (INSTANCE_NAME );
179
- assertThat (instances .get (1 ).getId ().getName ()).isEqualTo (INSTANCE_NAME2 );
180
- assertThat (instances .size ()).isEqualTo (2 );
308
+ assertEquals (INSTANCE_NAME , instances .get (0 ).getId ().getName ());
309
+ assertEquals (1000 , instances .get (0 ).getProcessingUnits ());
310
+ assertEquals (INSTANCE_NAME2 , instances .get (1 ).getId ().getName ());
311
+ assertEquals (2000 , instances .get (1 ).getProcessingUnits ());
312
+ assertEquals (2 , instances .size ());
181
313
}
182
314
183
315
@ Test
0 commit comments