|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License |
| 15 | + */ |
| 16 | +package net.jodah.failsafe.functional; |
| 17 | + |
| 18 | +import net.jodah.failsafe.*; |
| 19 | +import net.jodah.failsafe.Testing.Stats; |
| 20 | +import org.testng.annotations.Test; |
| 21 | + |
| 22 | +import static net.jodah.failsafe.Testing.*; |
| 23 | +import static org.testng.Assert.assertEquals; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests scenarios where a PolicyExecutor's preExecute provides an alternative result, which prevents the user's |
| 27 | + * Supplier from being called. This occurs when a CircuitBreaker is open. |
| 28 | + */ |
| 29 | +@Test |
| 30 | +public class AlternativeResultTest { |
| 31 | + public void testRejectedSyncAndAsync() { |
| 32 | + Stats rpStats = new Stats(); |
| 33 | + Stats cbStats = new Stats(); |
| 34 | + RetryPolicy<Object> rp = withStats(new RetryPolicy<>().withMaxAttempts(7), rpStats, true); |
| 35 | + CircuitBreaker<Object> cb = withStats(new CircuitBreaker<>().withFailureThreshold(3), cbStats, true); |
| 36 | + |
| 37 | + testSyncAndAsync(Failsafe.with(rp, cb), () -> { |
| 38 | + rpStats.reset(); |
| 39 | + cbStats.reset(); |
| 40 | + cb.close(); |
| 41 | + }, () -> { |
| 42 | + System.out.println("Executing"); |
| 43 | + throw new Exception(); |
| 44 | + }, e -> { |
| 45 | + assertEquals(e.getAttemptCount(), 7); |
| 46 | + assertEquals(e.getExecutionCount(), 3); |
| 47 | + assertEquals(rpStats.failedAttemptCount, 7); |
| 48 | + assertEquals(rpStats.retryCount, 6); |
| 49 | + assertEquals(cb.getExecutionCount(), 3); |
| 50 | + assertEquals(cb.getFailureCount(), 3); |
| 51 | + }, CircuitBreakerOpenException.class); |
| 52 | + } |
| 53 | + |
| 54 | + public void testRejectedAsyncExecutionWithRetry() { |
| 55 | + Stats rpStats = new Stats(); |
| 56 | + Stats cbStats = new Stats(); |
| 57 | + RetryPolicy<Object> rp = withStats(new RetryPolicy<>().withMaxAttempts(7), rpStats, true); |
| 58 | + CircuitBreaker<Object> cb = withStats(new CircuitBreaker<>().withFailureThreshold(3), cbStats, true); |
| 59 | + |
| 60 | + // Test with retryOn() |
| 61 | + testAsyncExecution(Failsafe.with(rp, cb), ex -> { |
| 62 | + runAsync(() -> { |
| 63 | + System.out.println("Executing"); |
| 64 | + ex.retryOn(new IllegalStateException()); |
| 65 | + }); |
| 66 | + }, e -> { |
| 67 | + assertEquals(e.getAttemptCount(), 7); |
| 68 | + assertEquals(e.getExecutionCount(), 3); |
| 69 | + assertEquals(rpStats.failedAttemptCount, 7); |
| 70 | + assertEquals(rpStats.retryCount, 6); |
| 71 | + assertEquals(cb.getExecutionCount(), 3); |
| 72 | + assertEquals(cb.getFailureCount(), 3); |
| 73 | + }, CircuitBreakerOpenException.class); |
| 74 | + } |
| 75 | + |
| 76 | + @Test(enabled = false) |
| 77 | + public void testRejectedAsyncExecutionWithCompleteAndRetry() { |
| 78 | + Stats rpStats = new Stats(); |
| 79 | + Stats cbStats = new Stats(); |
| 80 | + RetryPolicy<Object> rp = withStats(new RetryPolicy<>().withMaxAttempts(7), rpStats, true); |
| 81 | + CircuitBreaker<Object> cb = withStats(new CircuitBreaker<>().withFailureThreshold(3), cbStats, true); |
| 82 | + |
| 83 | + // Test with complete() and retry() |
| 84 | + rpStats.reset(); |
| 85 | + cbStats.reset(); |
| 86 | + cb.close(); |
| 87 | + testAsyncExecution(Failsafe.with(rp, cb), ex -> { |
| 88 | + runAsync(() -> { |
| 89 | + System.out.println("Executing"); |
| 90 | + if (!ex.complete(null, new IllegalStateException())) { |
| 91 | + ex.retry(); |
| 92 | + } |
| 93 | + }); |
| 94 | + }, e -> { |
| 95 | + assertEquals(e.getAttemptCount(), 7); |
| 96 | + assertEquals(e.getExecutionCount(), 3); |
| 97 | + assertEquals(rpStats.failedAttemptCount, 7); |
| 98 | + assertEquals(rpStats.retryCount, 6); |
| 99 | + assertEquals(cb.getExecutionCount(), 3); |
| 100 | + assertEquals(cb.getFailureCount(), 3); |
| 101 | + }, CircuitBreakerOpenException.class); |
| 102 | + } |
| 103 | +} |
0 commit comments