Repository navigation
Expand file tree
/
Copy pathSessionImplTest.java
More file actions
346 lines (294 loc) · 13.6 KB
/
Copy pathSessionImplTest.java
File metadata and controls
346 lines (294 loc) · 13.6 KB
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
/*
* 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 com.google.cloud.spanner;
import static com.google.common.truth.Truth.assertThat;
import static com.google.spanner.v1.Mutation.Write;
import static org.junit.Assert.fail;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.protobuf.ByteString;
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.PartialResultSet;
import com.google.spanner.v1.ReadRequest;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.Session;
import com.google.spanner.v1.Transaction;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
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.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/** Unit tests for {@link com.google.cloud.spanner.SpannerImpl.SessionImpl}. */
@RunWith(JUnit4.class)
public class SessionImplTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
@Mock private SpannerRpc rpc;
private com.google.cloud.spanner.Session session;
@Captor private ArgumentCaptor<Map<SpannerRpc.Option, Object>> optionsCaptor;
private Map<SpannerRpc.Option, Object> options;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
SpannerImpl spanner = new SpannerImpl(rpc, 1, null);
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), optionsCaptor.capture()))
.thenReturn(sessionProto);
session = spanner.createSession(db);
// We expect the same options, "options", on all calls on "session".
options = optionsCaptor.getValue();
Mockito.reset(rpc);
}
@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(
Arrays.asList(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);
}
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());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void newSingleUseContextClosesOldSingleUseReadOnlyTransactionContext() {
ReadContext ctx = session.singleUseReadOnlyTransaction(TimestampBound.strong());
session.singleUse(TimestampBound.strong());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void newSingleUseContextClosesOldMultiUseReadOnlyTransactionContext() {
ReadContext ctx = session.singleUseReadOnlyTransaction(TimestampBound.strong());
session.singleUse(TimestampBound.strong());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void newSingleUseReadOnlyTransactionContextClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
session.singleUseReadOnlyTransaction(TimestampBound.strong());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void newMultiUseReadOnlyTransactionContextClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
session.readOnlyTransaction(TimestampBound.strong());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void writeClosesOldSingleUseContext() throws ParseException {
ReadContext ctx = session.singleUse(TimestampBound.strong());
Mockito.when(rpc.commit(Mockito.<CommitRequest>any(), Mockito.eq(options)))
.thenReturn(
CommitResponse.newBuilder()
.setCommitTimestamp(Timestamps.parse("2015-10-01T10:54:20.021Z"))
.build());
session.writeAtLeastOnce(Arrays.<Mutation>asList());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@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();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
@Test
public void singleUseContextClosesTransaction() {
TransactionRunner runner = session.readWriteTransaction();
session.singleUse(TimestampBound.strong());
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
runner.run(
new TransactionRunner.TransactionCallable<Void>() {
@Nullable
@Override
public Void run(TransactionContext transaction) throws SpannerException {
fail("Unexpected call to transaction body");
return null;
}
});
}
@Test
public void prepareClosesOldSingleUseContext() {
ReadContext ctx = session.singleUse(TimestampBound.strong());
Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options)))
.thenReturn(Transaction.newBuilder().setId(ByteString.copyFromUtf8("t1")).build());
session.prepareReadWriteTransaction();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("invalidated");
ctx.read("Dummy", KeySet.all(), Arrays.asList("C"));
}
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(), Arrays.asList("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.
expectedException.expect(IllegalStateException.class);
txn.getReadTimestamp();
}
@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());
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
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.<ReadRequest>any(), consumer.capture(), Mockito.eq(options)))
.then(
new Answer<SpannerRpc.StreamingCall>() {
@Override
public SpannerRpc.StreamingCall answer(InvocationOnMock invocation) throws Throwable {
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.<BeginTransactionRequest>any(), Mockito.eq(options)))
.thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
@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.<BeginTransactionRequest>any(), Mockito.eq(options)))
.thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
@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.<BeginTransactionRequest>any(), Mockito.eq(options)))
.thenReturn(txnMetadata);
mockRead(resultSet);
ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
}