/
SessionImplTest.java
477 lines (431 loc) · 19.1 KB
/
SessionImplTest.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright 2017 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
*
* 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 com.google.cloud.spanner;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.api.core.ApiFutures;
import com.google.api.core.NanoClock;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.Timestamp;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import com.google.protobuf.ListValue;
import com.google.protobuf.util.Timestamps;
import com.google.spanner.v1.BeginTransactionRequest;
import com.google.spanner.v1.CommitRequest;
import com.google.spanner.v1.CommitResponse;
import com.google.spanner.v1.Mutation.Write;
import com.google.spanner.v1.PartialResultSet;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.RollbackRequest;
import com.google.spanner.v1.Session;
import com.google.spanner.v1.Transaction;
import io.opencensus.trace.Span;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
/** Unit tests for {@link com.google.cloud.spanner.SessionImpl}. */
@RunWith(JUnit4.class)
public class SessionImplTest {
@Mock private SpannerRpc rpc;
@Mock private SpannerOptions spannerOptions;
private com.google.cloud.spanner.Session session;
@Captor private ArgumentCaptor<Map<SpannerRpc.Option, Object>> optionsCaptor;
private Map<SpannerRpc.Option, Object> options;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(spannerOptions.getNumChannels()).thenReturn(4);
when(spannerOptions.getPrefetchChunks()).thenReturn(1);
when(spannerOptions.getRetrySettings()).thenReturn(RetrySettings.newBuilder().build());
when(spannerOptions.getClock()).thenReturn(NanoClock.getDefaultClock());
when(spannerOptions.getSessionLabels()).thenReturn(Collections.emptyMap());
GrpcTransportOptions transportOptions = mock(GrpcTransportOptions.class);
when(transportOptions.getExecutorFactory()).thenReturn(mock(ExecutorFactory.class));
when(spannerOptions.getTransportOptions()).thenReturn(transportOptions);
when(spannerOptions.getSessionPoolOptions()).thenReturn(mock(SessionPoolOptions.class));
@SuppressWarnings("resource")
SpannerImpl spanner = new SpannerImpl(rpc, spannerOptions);
String dbName = "projects/p1/instances/i1/databases/d1";
String sessionName = dbName + "/sessions/s1";
DatabaseId db = DatabaseId.of(dbName);
Session sessionProto = Session.newBuilder().setName(sessionName).build();
Mockito.when(rpc.createSession(Mockito.eq(dbName), Mockito.anyMap(), optionsCaptor.capture()))
.thenReturn(sessionProto);
Transaction txn = Transaction.newBuilder().setId(ByteString.copyFromUtf8("TEST")).build();
Mockito.when(
rpc.beginTransactionAsync(
Mockito.any(BeginTransactionRequest.class), Mockito.any(Map.class)))
.thenReturn(ApiFutures.immediateFuture(txn));
CommitResponse commitResponse =
CommitResponse.newBuilder()
.setCommitTimestamp(com.google.protobuf.Timestamp.getDefaultInstance())
.build();
Mockito.when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.any(Map.class)))
.thenReturn(ApiFutures.immediateFuture(commitResponse));
Mockito.when(rpc.rollbackAsync(Mockito.any(RollbackRequest.class), Mockito.anyMap()))
.thenReturn(ApiFutures.immediateFuture(Empty.getDefaultInstance()));
session = spanner.getSessionClient(db).createSession();
((SessionImpl) session).setCurrentSpan(mock(Span.class));
// We expect the same options, "options", on all calls on "session".
options = optionsCaptor.getValue();
}
private void doNestedRwTransaction() {
session
.readWriteTransaction()
.run(
transaction -> {
session.readWriteTransaction().run(transaction1 -> null);
return null;
});
}
@Test
public void nestedReadWriteTxnThrows() {
SpannerException e = assertThrows(SpannerException.class, () -> doNestedRwTransaction());
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
assertThat(e.getMessage()).contains("not supported");
}
@Test
public void nestedReadOnlyTxnThrows() {
SpannerException e =
assertThrows(
SpannerException.class,
() ->
session
.readWriteTransaction()
.run(
transaction -> {
session.readOnlyTransaction().getReadTimestamp();
return null;
}));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
assertThat(e.getMessage()).contains("not supported");
}
@Test
public void nestedSingleUseReadTxnThrows() {
SpannerException e =
assertThrows(
SpannerException.class,
() ->
session
.readWriteTransaction()
.run(
transaction -> {
session.singleUseReadOnlyTransaction();
return null;
}));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
assertThat(e.getMessage()).contains("not supported");
}
@Test
public void nestedTxnSucceedsWhenAllowed() {
session
.readWriteTransaction()
.allowNestedTransaction()
.run(
transaction -> {
session.singleUseReadOnlyTransaction();
return null;
});
}
@Test
public void writeAtLeastOnce() throws ParseException {
String timestampString = "2015-10-01T10:54:20.021Z";
ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class);
CommitResponse response =
CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build();
Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response);
Timestamp timestamp =
session.writeAtLeastOnce(
Collections.singletonList(Mutation.newInsertBuilder("T").set("C").to("x").build()));
assertThat(timestamp.getSeconds())
.isEqualTo(utcTimeSeconds(2015, Calendar.OCTOBER, 1, 10, 54, 20));
assertThat(timestamp.getNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(21));
CommitRequest request = commit.getValue();
assertThat(request.getSingleUseTransaction()).isNotNull();
assertThat(request.getSingleUseTransaction().getReadWrite()).isNotNull();
com.google.spanner.v1.Mutation mutation =
com.google.spanner.v1.Mutation.newBuilder()
.setInsert(
Write.newBuilder()
.setTable("T")
.addColumns("C")
.addValues(
ListValue.newBuilder()
.addValues(com.google.protobuf.Value.newBuilder().setStringValue("x"))))
.build();
assertThat(request.getMutationsList()).containsExactly(mutation);
}
@Test
public void writeAtLeastOnceWithOptions() throws ParseException {
String tag = "app=spanner,env=test";
String timestampString = "2015-10-01T10:54:20.021Z";
ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class);
CommitResponse response =
CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build();
Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response);
session.writeAtLeastOnceWithOptions(
Collections.singletonList(Mutation.newInsertBuilder("T").set("C").to("x").build()),
Options.tag(tag));
CommitRequest request = commit.getValue();
assertThat(request.getRequestOptions().getTransactionTag()).isEqualTo(tag);
com.google.spanner.v1.Mutation mutation =
com.google.spanner.v1.Mutation.newBuilder()
.setInsert(
Write.newBuilder()
.setTable("T")
.addColumns("C")
.addValues(
ListValue.newBuilder()
.addValues(com.google.protobuf.Value.newBuilder().setStringValue("x"))))
.build();
assertThat(request.getMutationsList()).containsExactly(mutation);
}
private static long utcTimeSeconds(int year, int month, int day, int hour, int min, int secs) {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.set(year, month, day, hour, min, secs);
return calendar.getTimeInMillis() / 1000;
}
@Test
public void newSingleUseContextClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
session.singleUse(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void newSingleUseContextClosesOldSingleUseReadOnlyTransactionContext() {
ReadContext ctx = session.singleUseReadOnlyTransaction(TimestampBound.strong());
session.singleUse(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void newSingleUseContextClosesOldMultiUseReadOnlyTransactionContext() {
ReadContext ctx = session.singleUseReadOnlyTransaction(TimestampBound.strong());
session.singleUse(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void newSingleUseReadOnlyTransactionContextClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
session.singleUseReadOnlyTransaction(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void newMultiUseReadOnlyTransactionContextClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
session.readOnlyTransaction(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void writeClosesOldSingleUseContext() throws ParseException {
ReadContext ctx = session.singleUse(TimestampBound.strong());
Mockito.when(rpc.commit(Mockito.any(), Mockito.eq(options)))
.thenReturn(
CommitResponse.newBuilder()
.setCommitTimestamp(Timestamps.parse("2015-10-01T10:54:20.021Z"))
.build());
session.writeAtLeastOnce(Collections.emptyList());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void transactionClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
// Note that we don't even run the transaction - just preparing the runner is sufficient.
session.readWriteTransaction();
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void singleUseContextClosesTransaction() {
TransactionRunner runner = session.readWriteTransaction();
session.singleUse(TimestampBound.strong());
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() ->
runner.run(
transaction -> {
fail("Unexpected call to transaction body");
return null;
}));
assertThat(e.getMessage()).contains("invalidated");
}
@Test
public void prepareClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
Mockito.when(rpc.beginTransaction(Mockito.any(), Mockito.eq(options)))
.thenReturn(Transaction.newBuilder().setId(ByteString.copyFromUtf8("t1")).build());
session.prepareReadWriteTransaction();
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> ctx.read("Dummy", KeySet.all(), Collections.singletonList("C")));
assertThat(e.getMessage()).contains("invalidated");
}
private static ResultSetMetadata newMetadata(Type type) {
return ResultSetMetadata.newBuilder().setRowType(type.toProto().getStructType()).build();
}
@Test
public void singleUseReadOnlyTransactionDoesntReturnTransactionMetadata() {
PartialResultSet resultSet =
PartialResultSet.newBuilder()
.setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string()))))
.build();
mockRead(resultSet);
ReadOnlyTransaction txn = session.singleUseReadOnlyTransaction(TimestampBound.strong());
assertThat(txn.readRow("Dummy", Key.of(), Collections.singletonList("C"))).isNull();
// For now, getReadTimestamp() will raise an ISE because it hasn't seen a timestamp. It would
// be better for the read to fail with an INTERNAL error, but we can't do that until txn
// metadata is returned for failed reads (e.g., table-not-found) as well as successful ones.
// TODO(user): Fix this.
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> txn.getReadTimestamp());
assertNotNull(e.getMessage());
}
@Test
public void singleUseReadOnlyTransactionReturnsEmptyTransactionMetadata() {
PartialResultSet resultSet =
PartialResultSet.newBuilder()
.setMetadata(
newMetadata(Type.struct(Type.StructField.of("C", Type.string())))
.toBuilder()
.setTransaction(Transaction.getDefaultInstance()))
.build();
mockRead(resultSet);
ReadOnlyTransaction txn = session.singleUseReadOnlyTransaction(TimestampBound.strong());
SpannerException e =
assertThrows(
SpannerException.class,
() -> txn.readRow("Dummy", Key.of(), Collections.singletonList("C")));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
private static class NoOpStreamingCall implements SpannerRpc.StreamingCall {
@Override
public void cancel(@Nullable String message) {}
@Override
public void request(int numMessages) {}
}
private void mockRead(final PartialResultSet myResultSet) {
final ArgumentCaptor<SpannerRpc.ResultStreamConsumer> consumer =
ArgumentCaptor.forClass(SpannerRpc.ResultStreamConsumer.class);
Mockito.when(rpc.read(Mockito.any(), consumer.capture(), Mockito.eq(options)))
.then(
invocation -> {
consumer.getValue().onPartialResultSet(myResultSet);
consumer.getValue().onCompleted();
return new NoOpStreamingCall();
});
}
@Test
public void multiUseReadOnlyTransactionReturnsEmptyTransactionMetadata() {
Transaction txnMetadata = Transaction.newBuilder().setId(ByteString.copyFromUtf8("x")).build();
PartialResultSet resultSet =
PartialResultSet.newBuilder()
.setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string()))))
.build();
Mockito.when(rpc.beginTransaction(Mockito.any(), Mockito.eq(options))).thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
SpannerException e =
assertThrows(
SpannerException.class,
() -> txn.readRow("Dummy", Key.of(), Collections.singletonList("C")));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
@Test
public void multiUseReadOnlyTransactionReturnsMissingTimestamp() {
Transaction txnMetadata = Transaction.newBuilder().setId(ByteString.copyFromUtf8("x")).build();
PartialResultSet resultSet =
PartialResultSet.newBuilder()
.setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string()))))
.build();
Mockito.when(rpc.beginTransaction(Mockito.any(), Mockito.eq(options))).thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
SpannerException e =
assertThrows(
SpannerException.class,
() -> txn.readRow("Dummy", Key.of(), Collections.singletonList("C")));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
@Test
public void multiUseReadOnlyTransactionReturnsMissingTransactionId() throws ParseException {
com.google.protobuf.Timestamp t = Timestamps.parse("2015-10-01T10:54:20.021Z");
Transaction txnMetadata = Transaction.newBuilder().setReadTimestamp(t).build();
PartialResultSet resultSet =
PartialResultSet.newBuilder()
.setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string()))))
.build();
Mockito.when(rpc.beginTransaction(Mockito.any(), Mockito.eq(options))).thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
SpannerException e =
assertThrows(
SpannerException.class,
() -> txn.readRow("Dummy", Key.of(), Collections.singletonList("C")));
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
}