private static Duration systemProperty(String name, int defaultValue) { String stringValue = System.getProperty(name, ""); return Duration.ofSeconds(stringValue.isEmpty() ? defaultValue : Integer.parseInt(stringValue)); } }
/** * Stops the Datastore emulator. The same as {@link stop(Duration)} but with timeout duration of * 20 seconds. * * <p>It is important to stop the emulator. Since the emulator runs in its own process, not * stopping it might cause it to become orphan. * * <p>It is not required to call {@link #reset()} before {@code stop()}. */ public void stop() throws IOException, InterruptedException, TimeoutException { stop(Duration.ofSeconds(20)); }
MockRetryingFuture() { this(Duration.ofSeconds(5)); }
/** Gets the configured maximum age */ public Duration getMaxAge() { return Duration.ofSeconds(builder.getSeconds(), builder.getNanos()); }
public Publisher getPublisherWithCustomRetrySettings(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_retry_settings] // Retry settings control how the publisher handles retryable failures Duration retryDelay = Duration.ofMillis(100); // default : 1 ms double retryDelayMultiplier = 2.0; // back off for repeated failures Duration maxRetryDelay = Duration.ofSeconds(5); // default : 10 seconds Duration totalTimeout = Duration.ofSeconds(1); // default: 0 Duration initialRpcTimeout = Duration.ofSeconds(1); // default: 0 Duration maxRpcTimeout = Duration.ofSeconds(10); // default: 0 RetrySettings retrySettings = RetrySettings.newBuilder() .setInitialRetryDelay(retryDelay) .setRetryDelayMultiplier(retryDelayMultiplier) .setMaxRetryDelay(maxRetryDelay) .setTotalTimeout(totalTimeout) .setInitialRpcTimeout(initialRpcTimeout) .setMaxRpcTimeout(maxRpcTimeout) .build(); Publisher publisher = Publisher.newBuilder(topicName).setRetrySettings(retrySettings).build(); // [END pubsub_publisher_retry_settings] return publisher; }
@Test public void durationSeconds() { GcRule actual = GCRULES.maxAge(Duration.ofSeconds(1)).toProto(); GcRule expected = buildAgeRule(1, 0); assertThat(actual).isEqualTo(expected); }
/** Example extracting data to single Google Cloud Storage file. */ // [TARGET extract(String, String, JobOption...)] // [VARIABLE "CSV"] // [VARIABLE "gs://my_bucket/filename.csv"] public Job extractSingle(String format, String gcsUrl) { // [START bigquery_extract_table] Job job = table.extract(format, gcsUrl); // Wait for the job to complete try { Job completedJob = job.waitFor( RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait } // [END bigquery_extract_table] return job; }
/** Example loading data from a single Google Cloud Storage file. */ // [TARGET load(FormatOptions, String, JobOption...)] // [VARIABLE "gs://my_bucket/filename.csv"] public Job loadSingle(String sourceUri) { // [START bigquery_load_table_gcs_csv] Job job = table.load(FormatOptions.csv(), sourceUri); // Wait for the job to complete try { Job completedJob = job.waitFor( RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait } // [END bigquery_load_table_gcs_csv] return job; } }
@Test public void duration() { DurationRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)); GcRule expected = buildAgeRule(61, 9); assertNotNull(actual.getMaxAge()); assertThat(actual.toProto()).isEqualTo(expected); }
@Test public void testSettingsArePreserved() { String endpoint = "some.other.host:123"; CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); Duration watchdogInterval = Duration.ofSeconds(12); WatchdogProvider watchdogProvider = Mockito.mock(WatchdogProvider.class); EnhancedBigQueryStorageStubSettings.Builder builder = EnhancedBigQueryStorageStubSettings.newBuilder() .setEndpoint(endpoint) .setCredentialsProvider(credentialsProvider) .setStreamWatchdogCheckInterval(watchdogInterval) .setStreamWatchdogProvider(watchdogProvider); verifyBuilder(builder, endpoint, credentialsProvider, watchdogInterval, watchdogProvider); verifySettings( builder.build(), endpoint, credentialsProvider, watchdogInterval, watchdogProvider); verifyBuilder( builder.build().toBuilder(), endpoint, credentialsProvider, watchdogInterval, watchdogProvider); }
@Test public void testErrorPropagation() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); testPublisherServiceImpl.addPublishError(Status.DATA_LOSS.asException()); try { sendTestMessage(publisher, "A").get(); fail("should throw exception"); } catch (ExecutionException e) { assertThat(e.getCause()).isInstanceOf(DataLossException.class); } }
MockRetryingFuture(Duration totalTimeout) { this.timedAttemptSettings = TimedAttemptSettings.newBuilder() .setRpcTimeout(Duration.ofSeconds(1)) .setRetryDelay(Duration.ZERO) .setRandomizedRetryDelay(Duration.ZERO) .setAttemptCount(0) .setFirstAttemptStartTimeNanos(0) .setGlobalSettings(RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build()) .build(); }
@Before public void setUp() { ClientContext clientContext = ClientContext.newBuilder().setDefaultCallContext(CALL_CONTEXT).build(); RetrySettings retrySettings = RetrySettings.newBuilder() .setTotalTimeout(Duration.ofMillis(100)) // Delay settings: 1 ms const .setInitialRetryDelay(Duration.ofMillis(1)) .setMaxRetryDelay(Duration.ofMillis(1)) .setRetryDelayMultiplier(1.0) // RPC timeout: ignored const 1 s .setInitialRpcTimeout(Duration.ofSeconds(1)) .setMaxRpcTimeout(Duration.ofSeconds(1)) .setRpcTimeoutMultiplier(1.0) .build(); callable = AwaitReplicationCallable.create( mockGenerateConsistencyTokenCallable, mockCheckConsistencyCallable, clientContext, retrySettings); }
@Test public void testPublishFailureRetries_maxRetriesSetUnlimited() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setRetrySettings( Publisher.Builder.DEFAULT_RETRY_SETTINGS .toBuilder() .setTotalTimeout(Duration.ofSeconds(10)) .setMaxAttempts(0) .build()) .build(); testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishResponse(PublishResponse.newBuilder().addMessageIds("1")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); assertEquals("1", publishFuture1.get()); assertEquals(3, testPublisherServiceImpl.getCapturedRequests().size()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test public void testPublishFailureRetries_maxRetriesSetup() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setRetrySettings( Publisher.Builder.DEFAULT_RETRY_SETTINGS .toBuilder() .setTotalTimeout(Duration.ofSeconds(10)) .setMaxAttempts(3) .build()) .build(); testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishResponse(PublishResponse.newBuilder().addMessageIds("1")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); assertEquals("1", publishFuture1.get()); assertEquals(3, testPublisherServiceImpl.getCapturedRequests().size()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test public void unionTwo() { GcRule actual = GCRULES .union() .rule(GCRULES.maxVersions(1)) .rule(GCRULES.maxAge(Duration.ofSeconds(1))) .toProto(); GcRule expected = GcRule.newBuilder() .setUnion( Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, 0))) .build(); assertThat(actual).isEqualTo(expected); }
@Test public void testPublishFailureRetries() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); // To demonstrate that reaching duration will trigger publish testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishResponse(PublishResponse.newBuilder().addMessageIds("1")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); assertEquals("1", publishFuture1.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().size()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test public void intersectionTwo() { GcRule actual = GCRULES .intersection() .rule(GCRULES.maxVersions(1)) .rule(GCRULES.maxAge(Duration.ofSeconds(1))) .toProto(); GcRule expected = GcRule.newBuilder() .setIntersection( Intersection.newBuilder() .addRules(buildVersionsRule(1)) .addRules(buildAgeRule(1, 0))) .build(); assertThat(actual).isEqualTo(expected); }
@Test public void intersectionThree() { GcRule actual = GCRULES .intersection() .rule(GCRULES.maxVersions(1)) .rule(GCRULES.maxAge(Duration.ofSeconds(1))) .rule(GCRULES.maxAge(Duration.ofNanos(1))) .toProto(); GcRule expected = GcRule.newBuilder() .setIntersection( Intersection.newBuilder() .addRules(buildVersionsRule(1)) .addRules(buildAgeRule(1, 0)) .addRules(buildAgeRule(0, 1))) .build(); assertThat(actual).isEqualTo(expected); }
@Test public void unionThree() { GcRule actual = GCRULES .union() .rule(GCRULES.maxVersions(1)) .rule(GCRULES.maxAge(Duration.ofSeconds(1))) .rule(GCRULES.maxAge(Duration.ofNanos(1))) .toProto(); GcRule expected = GcRule.newBuilder() .setUnion( Union.newBuilder() .addRules(buildVersionsRule(1)) .addRules(buildAgeRule(1, 0)) .addRules(buildAgeRule(0, 1))) .build(); assertThat(actual).isEqualTo(expected); }