-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
214 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
package com.teketik.utils; | ||
|
||
import java.util.Optional; | ||
|
||
public interface Schedulable extends Runnable { | ||
|
||
/** | ||
* @return time in seconds between the termination of one execution and the commencement of the next. | ||
*/ | ||
int getRefreshRateInSeconds(); | ||
int getTimeoutInSeconds(); | ||
|
||
/** | ||
* @return maximum time in seconds this can run before being interrupted ({@link Optional#empty() empty} if never to be interrupted). | ||
*/ | ||
Optional<Integer> getTimeoutInSeconds(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/test/java/com/teketik/spring/health/TimeoutInterruptibleDisabledITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.teketik.spring.health; | ||
|
||
import org.assertj.core.matcher.AssertionMatcher; | ||
import org.awaitility.Awaitility; | ||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
@ActiveProfiles("with-timing-out-indicator-interruption-disabled") | ||
public class TimeoutInterruptibleDisabledITest extends BaseITest { | ||
|
||
@Test | ||
public void test() throws Exception { | ||
final LocalDateTime[] upIndicator1FirstLastChecked = new LocalDateTime[1]; | ||
final LocalDateTime[] timingOutSleepingIndicatorInterruptionDisabledFirstLastChecked = new LocalDateTime[1]; | ||
Awaitility.await().untilAsserted(() -> { | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/actuator/health")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.status").value(CoreMatchers.not("UNKNOWN"))); | ||
}); | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/actuator/health")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.status").value("UP")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.status").value("DOWN")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.error").doesNotExist()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.reason").value(Matchers.equalTo("Timeout"))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastDuration").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
Assert.assertEquals(6, actual.length()); | ||
Assert.assertTrue(actual.endsWith("ms")); | ||
Assert.assertThat(actual, CoreMatchers.startsWith("1")); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
upIndicator1FirstLastChecked[0] = LocalDateTime.parse(actual); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
timingOutSleepingIndicatorInterruptionDisabledFirstLastChecked[0] = LocalDateTime.parse(actual); | ||
} | ||
} | ||
)); | ||
Thread.sleep(1000); | ||
//still the same | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/actuator/health")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.status").value("UP")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.status").value("DOWN")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.error").doesNotExist()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.reason").value(Matchers.equalTo("Timeout"))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastDuration").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
Assert.assertEquals(6, actual.length()); | ||
Assert.assertTrue(actual.endsWith("ms")); | ||
Assert.assertThat(actual, CoreMatchers.startsWith("2")); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
final int difference = (int) Duration.between(upIndicator1FirstLastChecked[0], LocalDateTime.parse(actual)).toMillis(); | ||
Assert.assertThat(difference, Matchers.greaterThan(0)); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
final int difference = (int) Duration.between(timingOutSleepingIndicatorInterruptionDisabledFirstLastChecked[0], LocalDateTime.parse(actual)).toMillis(); | ||
Assert.assertThat(difference, Matchers.equalTo(0)); | ||
} | ||
} | ||
)); | ||
Thread.sleep(2000); | ||
//then update last check | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/actuator/health")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.status").value("UP")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.status").value("DOWN")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.error").doesNotExist()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.reason").value(Matchers.equalTo("Timeout"))) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastDuration").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
Assert.assertEquals(6, actual.length()); | ||
Assert.assertTrue(actual.endsWith("ms")); | ||
Assert.assertThat(actual, CoreMatchers.startsWith("1")); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.upIndicator1.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
final int difference = (int) Duration.between(upIndicator1FirstLastChecked[0], LocalDateTime.parse(actual)).toMillis(); | ||
Assert.assertThat(difference, Matchers.greaterThan(0)); | ||
} | ||
} | ||
)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("components.timingOutSleepingIndicatorInterruptionDisabled.details.lastChecked").value( | ||
new AssertionMatcher<String>() { | ||
@Override | ||
public void assertion(String actual) throws AssertionError { | ||
final int difference = (int) Duration.between(timingOutSleepingIndicatorInterruptionDisabledFirstLastChecked[0], LocalDateTime.parse(actual)).toMillis(); | ||
Assert.assertThat(difference, Matchers.greaterThan(0)); | ||
} | ||
} | ||
)); | ||
|
||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
.../com/teketik/spring/health/indicators/TimingOutSleepingIndicatorInterruptionDisabled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.teketik.spring.health.indicators; | ||
|
||
import com.teketik.spring.health.AsyncHealth; | ||
|
||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Profile("with-timing-out-indicator-interruption-disabled") | ||
@Component | ||
@AsyncHealth(refreshRate = 1, timeout = 1, interruptOnTimeout = false) | ||
public class TimingOutSleepingIndicatorInterruptionDisabled implements HealthIndicator { | ||
|
||
public static final int SLEEP_DURATION = 2000; | ||
|
||
@Override | ||
public Health health() { | ||
try { | ||
Thread.sleep(SLEEP_DURATION); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return Health | ||
.up() | ||
.withDetail("detailKey", "detailValue") | ||
.build(); | ||
} | ||
|
||
} |