@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")); }
@Test public void testBatchReadOnlyTxnWithBound() throws Exception { Session sessionProto = Session.newBuilder().setName(SESSION_NAME).build(); when(gapicRpc.createSession( eq(DB_NAME), (Map<String, String>) anyMap(), optionsCaptor.capture())) .thenReturn(sessionProto); com.google.protobuf.Timestamp timestamp = Timestamps.parse(TIMESTAMP); Transaction txnMetadata = Transaction.newBuilder().setId(TXN_ID).setReadTimestamp(timestamp).build(); when(spannerOptions.getSpannerRpcV1()).thenReturn(gapicRpc); when(gapicRpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), optionsCaptor.capture())) .thenReturn(txnMetadata); BatchReadOnlyTransaction batchTxn = client.batchReadOnlyTransaction(TimestampBound.strong()); assertThat(batchTxn.getBatchTransactionId().getSessionId()).isEqualTo(SESSION_NAME); assertThat(batchTxn.getBatchTransactionId().getTransactionId()).isEqualTo(TXN_ID); Timestamp t = Timestamp.parseTimestamp(TIMESTAMP); assertThat(batchTxn.getReadTimestamp()).isEqualTo(t); assertThat(batchTxn.getReadTimestamp()) .isEqualTo(batchTxn.getBatchTransactionId().getTimestamp()); }
@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 @SuppressWarnings("all") public void beginTransactionTest() { ByteString id = ByteString.copyFromUtf8("27"); Transaction expectedResponse = Transaction.newBuilder().setId(id).build(); mockSpanner.addResponse(expectedResponse); SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]"); TransactionOptions options = TransactionOptions.newBuilder().build(); Transaction actualResponse = client.beginTransaction(session, options); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests(); Assert.assertEquals(1, actualRequests.size()); BeginTransactionRequest actualRequest = (BeginTransactionRequest) actualRequests.get(0); Assert.assertEquals(session, SessionName.parse(actualRequest.getSession())); Assert.assertEquals(options, actualRequest.getOptions()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test public void metadata() { Type rowType = Type.struct(Type.StructField.of("f", Type.string())); ResultSetMetadata.Builder metadataBuilder = ResultSetMetadata.newBuilder(); metadataBuilder .setRowType(rowType.toProto().getStructType()) .getTransactionBuilder() .setId(ByteString.copyFromUtf8("t1")); PartialResultSet partialResultSet = PartialResultSet.newBuilder().setMetadata(metadataBuilder.build()).build(); consumer.onPartialResultSet(partialResultSet); consumer.onCompleted(); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(rowType); }
@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")); }
public Builder mergeFrom(com.google.spanner.v1.Transaction other) { if (other == com.google.spanner.v1.Transaction.getDefaultInstance()) return this; if (other.getId() != com.google.protobuf.ByteString.EMPTY) { setId(other.getId()); } if (other.hasReadTimestamp()) { mergeReadTimestamp(other.getReadTimestamp()); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; }