|
| 1 | +package apoc.util; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.*; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +public class ExtendedUtilTest { |
| 9 | + |
| 10 | + private static int i = 0; |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testWithLinearBackOffRetriesWithSuccess() { |
| 14 | + i = 0; |
| 15 | + long start = System.currentTimeMillis(); |
| 16 | + int result = ExtendedUtil.withBackOffRetries( |
| 17 | + this::testFunction, |
| 18 | + true, |
| 19 | + -1, // test backoffRetry default value -> 5 |
| 20 | + false, |
| 21 | + runEx -> { |
| 22 | + if(!runEx.getMessage().contains("Expected")) |
| 23 | + throw new RuntimeException("Some Bad News..."); |
| 24 | + } |
| 25 | + ); |
| 26 | + long time = System.currentTimeMillis() - start; |
| 27 | + |
| 28 | + assertEquals(4, result); |
| 29 | + |
| 30 | + // The method will attempt to execute the operation with a linear backoff strategy, |
| 31 | + // sleeping for 1 second, 2 seconds, and 3 seconds between retries. |
| 32 | + // This results in a total wait time of 6 seconds (1s + 2s + 3s + 4s) if the operation succeeds on the third attempt, |
| 33 | + // leading to an approximate execution time of 6 seconds. |
| 34 | + assertTrue("Current time is: " + time, |
| 35 | + time > 9000 && time < 11000); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testWithExponentialBackOffRetriesWithSuccess() { |
| 40 | + i = 0; |
| 41 | + long start = System.currentTimeMillis(); |
| 42 | + int result = ExtendedUtil.withBackOffRetries( |
| 43 | + this::testFunction, |
| 44 | + true, |
| 45 | + 0, // test backoffRetry default value -> 5 |
| 46 | + true, |
| 47 | + runEx -> {} |
| 48 | + ); |
| 49 | + long time = System.currentTimeMillis() - start; |
| 50 | + |
| 51 | + assertEquals(4, result); |
| 52 | + |
| 53 | + // The method will attempt to execute the operation with an exponential backoff strategy, |
| 54 | + // sleeping for 2 second, 4 seconds, and 8 seconds between retries. |
| 55 | + // This results in a total wait time of 30 seconds (2s + 4s + 8s + 16s) if the operation succeeds on the third attempt, |
| 56 | + // leading to an approximate execution time of 14 seconds. |
| 57 | + assertTrue("Current time is: " + time, |
| 58 | + time > 29000 && time < 31000); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testBackOffRetriesWithError() { |
| 63 | + i = 0; |
| 64 | + long start = System.currentTimeMillis(); |
| 65 | + assertThrows( |
| 66 | + RuntimeException.class, |
| 67 | + () -> ExtendedUtil.withBackOffRetries( |
| 68 | + this::testFunction, |
| 69 | + true, |
| 70 | + 2, |
| 71 | + false, |
| 72 | + runEx -> {} |
| 73 | + ) |
| 74 | + ); |
| 75 | + long time = System.currentTimeMillis() - start; |
| 76 | + |
| 77 | + // The method is configured to retry the operation twice. |
| 78 | + // So, it will make two extra-attempts, waiting for 1 second and 2 seconds before failing and throwing an exception. |
| 79 | + // Resulting in an approximate execution time of 3 seconds. |
| 80 | + assertTrue("Current time is: " + time, |
| 81 | + time > 2000 && time < 4000); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testBackOffRetriesWithErrorAndExponential() { |
| 86 | + i = 0; |
| 87 | + long start = System.currentTimeMillis(); |
| 88 | + assertThrows( |
| 89 | + RuntimeException.class, |
| 90 | + () -> ExtendedUtil.withBackOffRetries( |
| 91 | + this::testFunction, |
| 92 | + true, |
| 93 | + 2, |
| 94 | + true, |
| 95 | + runEx -> {} |
| 96 | + ) |
| 97 | + ); |
| 98 | + long time = System.currentTimeMillis() - start; |
| 99 | + |
| 100 | + // The method is configured to retry the operation twice. |
| 101 | + // So, it will make two extra-attempts, waiting for 2 second and 4 seconds before failing and throwing an exception. |
| 102 | + // Resulting in an approximate execution time of 6 seconds. |
| 103 | + assertTrue("Current time is: " + time, |
| 104 | + time > 5000 && time < 7000); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testWithoutBackOffRetriesWithError() { |
| 109 | + i = 0; |
| 110 | + assertThrows( |
| 111 | + RuntimeException.class, |
| 112 | + () -> ExtendedUtil.withBackOffRetries( |
| 113 | + this::testFunction, |
| 114 | + false, 30, |
| 115 | + false, |
| 116 | + runEx -> {} |
| 117 | + ) |
| 118 | + ); |
| 119 | + |
| 120 | + // Retry strategy is not active and the testFunction is executed only once by raising an exception. |
| 121 | + assertEquals(1, i); |
| 122 | + } |
| 123 | + |
| 124 | + private int testFunction() { |
| 125 | + if (i == 4) { |
| 126 | + return i; |
| 127 | + } |
| 128 | + i++; |
| 129 | + throw new RuntimeException("Expected i not equal to 4"); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments