|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.longrunning; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertThrows; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | +import static org.junit.Assert.fail; |
| 35 | + |
| 36 | +import com.google.api.gax.core.FakeApiClock; |
| 37 | +import com.google.api.gax.retrying.RetrySettings; |
| 38 | +import com.google.api.gax.retrying.TimedAttemptSettings; |
| 39 | +import com.google.api.gax.util.FakeLogHandler; |
| 40 | +import java.util.concurrent.CancellationException; |
| 41 | +import org.junit.After; |
| 42 | +import org.junit.Before; |
| 43 | +import org.junit.Test; |
| 44 | +import org.threeten.bp.Duration; |
| 45 | + |
| 46 | +public class OperationTimedPollAlgorithmTest { |
| 47 | + |
| 48 | + private static final RetrySettings FAST_RETRY_SETTINGS = |
| 49 | + RetrySettings.newBuilder() |
| 50 | + .setInitialRetryDelay(Duration.ofMillis(1L)) |
| 51 | + .setRetryDelayMultiplier(1) |
| 52 | + .setMaxRetryDelay(Duration.ofMillis(1L)) |
| 53 | + .setInitialRpcTimeout(Duration.ofMillis(1L)) |
| 54 | + .setMaxAttempts(0) |
| 55 | + .setJittered(false) |
| 56 | + .setRpcTimeoutMultiplier(1) |
| 57 | + .setMaxRpcTimeout(Duration.ofMillis(1L)) |
| 58 | + .setTotalTimeout(Duration.ofMillis(5L)) |
| 59 | + .build(); |
| 60 | + private TimedAttemptSettings timedAttemptSettings; |
| 61 | + private FakeApiClock clock; |
| 62 | + |
| 63 | + private FakeLogHandler logHandler; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setUp() { |
| 67 | + logHandler = new FakeLogHandler(); |
| 68 | + OperationTimedPollAlgorithm.LOGGER.addHandler(logHandler); |
| 69 | + clock = new FakeApiClock(System.nanoTime()); |
| 70 | + timedAttemptSettings = |
| 71 | + TimedAttemptSettings.newBuilder() |
| 72 | + .setGlobalSettings(FAST_RETRY_SETTINGS) |
| 73 | + .setRetryDelay(Duration.ofMillis(1l)) |
| 74 | + .setRpcTimeout(Duration.ofMillis(1l)) |
| 75 | + .setRandomizedRetryDelay(Duration.ofMillis(1l)) |
| 76 | + .setAttemptCount(0) |
| 77 | + .setFirstAttemptStartTimeNanos(clock.nanoTime()) |
| 78 | + .build(); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void tearDown() { |
| 83 | + OperationTimedPollAlgorithm.LOGGER.removeHandler(logHandler); |
| 84 | + // redundant null assignment for readability - a new log handler will be used |
| 85 | + logHandler = null; |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testAlgorithmThatShouldRetry_doesNotLogTimeoutHelpMessage() { |
| 90 | + OperationTimedPollAlgorithm algorithm = |
| 91 | + OperationTimedPollAlgorithm.create(FAST_RETRY_SETTINGS, clock); |
| 92 | + try { |
| 93 | + algorithm.shouldRetry(timedAttemptSettings); |
| 94 | + } catch (CancellationException ex) { |
| 95 | + fail("Unexpected unsuccessful shouldRetry()"); |
| 96 | + } |
| 97 | + assertTrue( |
| 98 | + logHandler.getAllMessages().stream() |
| 99 | + .noneMatch( |
| 100 | + entry -> entry.contains(OperationTimedPollAlgorithm.LRO_TROUBLESHOOTING_LINK))); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testAlgorithmThatShouldNotRetry_logsTimeoutHelpMessage() { |
| 105 | + OperationTimedPollAlgorithm algorithm = |
| 106 | + OperationTimedPollAlgorithm.create(FAST_RETRY_SETTINGS, clock); |
| 107 | + clock.incrementNanoTime(1 * 1000 * 1000 * 1000); // force rpc timeout |
| 108 | + assertThrows(CancellationException.class, () -> algorithm.shouldRetry(timedAttemptSettings)); |
| 109 | + assertTrue( |
| 110 | + logHandler.getAllMessages().stream() |
| 111 | + .anyMatch( |
| 112 | + entry -> entry.contains(OperationTimedPollAlgorithm.LRO_TROUBLESHOOTING_LINK))); |
| 113 | + } |
| 114 | +} |
0 commit comments