This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into feature/phr-8877-ad…
…d-correlationid-method
- Loading branch information
Showing
7 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
date-time/src/main/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeService.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,28 @@ | ||
package com.pkb.common.datetime; | ||
|
||
import java.time.Clock; | ||
import java.time.temporal.TemporalAmount; | ||
|
||
/** | ||
* Override of FakeDateTimeService that supports automatically advancing | ||
* time by a fixed amount each time the services is asked for a time. | ||
* DO NOT USE in E2E under any circumstances, it will cause extreme | ||
* flakiness. This is intended for use in unit and integration tests only | ||
* for classes/subsystems that use temporal ordering. | ||
*/ | ||
public class AutoAdvanceFakeDateTimeService extends FakeDateTimeService { | ||
|
||
private TemporalAmount autoAdvanceDuration = null; | ||
|
||
@Override | ||
public Clock clock() { | ||
if (this.currentFixedClock != null && autoAdvanceDuration != null) { | ||
moveTime(autoAdvanceDuration); | ||
} | ||
return super.clock(); | ||
} | ||
|
||
public void setAutoAdvanceDuration(TemporalAmount autoAdvanceDuration) { | ||
this.autoAdvanceDuration = autoAdvanceDuration; | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
date-time/src/test/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeServiceTest.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,51 @@ | ||
package com.pkb.common.datetime; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.lessThan; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
class AutoAdvanceFakeDateTimeServiceTest { | ||
|
||
private AutoAdvanceFakeDateTimeService underTest = new AutoAdvanceFakeDateTimeService(); | ||
|
||
@Test | ||
void testAdvancesTimeBetweenCalls() { | ||
underTest.setFixedCurrentTimeForTesting("2020-01-01T00:00:00Z"); | ||
underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); | ||
|
||
assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); | ||
assertThat(underTest.now(), equalTo(Instant.parse("2020-01-03T00:00:00Z"))); | ||
} | ||
|
||
@Test | ||
void testStopsAdvancingWhenCleared() { | ||
underTest.setFixedCurrentTimeForTesting("2020-01-01T00:00:00Z"); | ||
underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); | ||
|
||
assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); | ||
underTest.setAutoAdvanceDuration(null); | ||
assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); | ||
} | ||
|
||
@Test | ||
void testDoesNothingIfTimeNotFixed() throws InterruptedException { | ||
|
||
underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); | ||
|
||
Instant start = underTest.now(); | ||
Thread.sleep(1); | ||
Instant end = underTest.now(); | ||
|
||
|
||
assertThat(start, not(end)); | ||
assertThat(Duration.between(start, end), lessThan(Duration.of(1, ChronoUnit.SECONDS))); | ||
} | ||
|
||
} |
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