mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-03 18:13:30 +00:00
* Add Health Check pattern implementation The commit introduces Health Check pattern, providing a series of health indicators for system performance and stability monitoring, including checks for system CPU load, process CPU load, database health, memory usage, and garbage collection metrics. It also includes asynchronous execution and caching mechanisms for health checks, and retry configurations for resilience. Implements health checking components as per issue #2695. * Test cases and javadoc for HealthEndpointIntegrationTest * Added more log to test case to see why it returns 503 * Change config values to see if the system High system CPU load is resolved or not in CI. * Fixes for test cases. * some fixes for Sonar. * some fixes for Sonar. ADDED HIGH_PROCESS_CPU_LOAD_MESSAGE_WITHOUT_PARAM ADDED HIGH_SYSTEM_CPU_LOAD_MESSAGE_WITHOUT_PARAM * Sonar fixes address "Define and throw a dedicated exception instead of using a generic one." added HealthCheckInterruptedException refactored CustomHealthIndicator * fixes checkstyle violation.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.health.check.RetryConfig;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link RetryConfig} class.
|
||||
*
|
||||
* @author ydoksanbir
|
||||
*/
|
||||
@SpringBootTest(classes = RetryConfig.class)
|
||||
class RetryConfigTest {
|
||||
|
||||
/** Injected RetryTemplate instance. */
|
||||
@Autowired private RetryTemplate retryTemplate;
|
||||
|
||||
/**
|
||||
* Tests that the retry template retries three times with a two-second delay.
|
||||
*
|
||||
* <p>Verifies that the retryable operation is executed three times before throwing an exception,
|
||||
* and that the total elapsed time for the retries is at least four seconds.
|
||||
*/
|
||||
@Test
|
||||
void shouldRetryThreeTimesWithTwoSecondDelay() {
|
||||
AtomicInteger attempts = new AtomicInteger();
|
||||
Runnable retryableOperation =
|
||||
() -> {
|
||||
attempts.incrementAndGet();
|
||||
throw new RuntimeException("Test exception for retry");
|
||||
};
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
retryTemplate.execute(
|
||||
context -> {
|
||||
retryableOperation.run();
|
||||
return null;
|
||||
});
|
||||
} catch (Exception e) {
|
||||
// Expected exception
|
||||
}
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
assertEquals(3, attempts.get(), "Should have retried three times");
|
||||
assertTrue(
|
||||
(endTime - startTime) >= 4000,
|
||||
"Should have waited at least 4 seconds in total for backoff");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user