Files
Vladislav.Yaroshchuk 0e71c30b2e [artifact-repository-manager] RetryProvider: do not log retry limit exceeded exception as err, use warn instead
Otherwise, some tests behaviour may change by an error thrown from logger:
`com.intellij.testFramework.TestLoggerFactory$TestLoggerAssertionError: Retry attempts limit exceeded`

GitOrigin-RevId: c167353951d0d263bb5a457727eb6ba18999f204
2023-11-15 21:13:29 +00:00

95 lines
3.0 KiB
Java

// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.idea.maven.aether;
import com.intellij.openapi.diagnostic.Logger;
import org.junit.jupiter.api.Test;
import static org.jetbrains.idea.maven.aether.RetryProvider.disabled;
import static org.jetbrains.idea.maven.aether.RetryProvider.withExponentialBackOff;
import static org.junit.jupiter.api.Assertions.*;
class RetryProviderTest {
private final Logger logger = Logger.getInstance(RetryProviderTest.class);
private final Retry retryDisabled = disabled();
private final Retry retryWithExpBackOff = withExponentialBackOff(1000, 5000, 5);
@Test
public void disabled_testIsSingleton() {
Retry first = disabled();
Retry second = disabled();
assertSame(first, second);
}
@Test
public void disabled_testReturnsCorrectValue() throws Exception {
int expected = 42;
int actual = retryDisabled.retry(() -> expected, logger);
assertEquals(expected, actual);
}
@Test
public void disabled_testCanBeReused() throws Exception {
int expected = 42;
int ignored = retryDisabled.retry(() -> expected, logger);
int actual = retryDisabled.retry(() -> expected, logger);
assertEquals(expected, actual);
}
@Test
public void disabled_testRethrowsException() {
String expected = "Value42";
assertThrows(Exception.class, () -> retryDisabled.retry(() -> {
throw new Exception(expected);
}, logger), expected);
}
@Test
public void expBackOff_testThrowsOnIllegalArguments() {
assertThrows(IllegalArgumentException.class, () -> withExponentialBackOff(0, 1, 1));
assertThrows(IllegalArgumentException.class, () -> withExponentialBackOff(1, 0, 1));
assertThrows(IllegalArgumentException.class, () -> withExponentialBackOff(1, 1, 0));
}
@Test
public void expBackOff_testOneAttempt() throws Exception {
int expected = 42;
int actual = retryWithExpBackOff.retry(() -> expected, logger);
assertEquals(expected, actual);
}
@Test
public void expBackOff_testRetry() throws Exception {
int attempts = retryWithExpBackOff.retry(new ThrowingSupplier<>() {
private int attempts = 0;
@Override
public Integer get() throws Exception {
if (attempts == 0) {
/* Simulate a fail */
++attempts;
throw new Exception();
}
return attempts;
}
}, logger);
assertTrue(attempts > 0, "attempts > 0");
}
@Test
public void expBackOff_testCanBeReused() throws Exception {
int expected = 42;
int ignored = retryWithExpBackOff.retry(() -> expected, logger);
int actual = retryWithExpBackOff.retry(() -> expected, logger);
assertEquals(expected, actual);
}
@Test
public void expBackOff_testRethrowsException() {
String expected = "Value42";
assertThrows(Exception.class, () -> retryWithExpBackOff.retry(() -> {
throw new Exception(expected);
}, logger), expected);
}
}