@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 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 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")); } }
@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); }
/** * Parse from RFC 3339 date string to Timestamp. This method accepts all * outputs of {@link #toString(Timestamp)} and it also accepts any fractional * digits (or none) and any offset as long as they fit into nano-seconds * precision. * * <p>Example of accepted format: "1972-01-01T10:00:20.021-05:00" * * @return A Timestamp parsed from the string. * @throws ParseException if parsing fails. * @deprecated Use {@link Timestamps#parse} instead. */ @Deprecated public static Timestamp parseTimestamp(String value) throws ParseException { return Timestamps.parse(value); }
private void mergeTimestamp(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { try { Timestamp value = Timestamps.parse(json.getAsString()); builder.mergeFrom(value.toByteString()); } catch (ParseException e) { throw new InvalidProtocolBufferException("Failed to parse timestamp: " + json); } }
private void mergeTimestamp(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { try { Timestamp value = Timestamps.parse(json.getAsString()); builder.mergeFrom(value.toByteString()); } catch (ParseException e) { throw new InvalidProtocolBufferException("Failed to parse timestamp: " + json); } }
private void mergeTimestamp(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { try { Timestamp value = Timestamps.parse(json.getAsString()); builder.mergeFrom(value.toByteString()); } catch (ParseException e) { throw new InvalidProtocolBufferException("Failed to parse timestamp: " + json); } }
private void mergeTimestamp(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { try { Timestamp value = Timestamps.parse(json.getAsString()); builder.mergeFrom(value.toByteString()); } catch (ParseException e) { throw new InvalidProtocolBufferException("Failed to parse timestamp: " + json); } }
@Override public Timestamp deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.getCurrentToken()) { case VALUE_STRING: try { return Timestamps.parse(parser.getText()); } catch (ParseException e) { throw context.weirdStringException(parser.getText(), Timestamp.class, e.getMessage()); } default: throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, wrongTokenMessage(context)); } }