Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/phr-8877-ad…
Browse files Browse the repository at this point in the history
…d-correlationid-method
  • Loading branch information
r-oylo committed Sep 28, 2021
2 parents bda276b + 634a556 commit 23be3f7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 16 deletions.
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pkb.common.datetime;

import io.vavr.Tuple2;

import java.text.ParsePosition;
import java.time.Clock;
import java.time.Instant;
Expand All @@ -12,11 +14,10 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.Date;

import io.vavr.Tuple2;

@SuppressWarnings({ "UseOfObsoleteDateTimeApi", "SSBasedInspection" })
public interface DateTimeService {

Expand All @@ -33,11 +34,12 @@ public interface DateTimeService {

void moveTime(long amountToAdd, TemporalUnit unit);

void moveTime(TemporalAmount duration);

long nowNanoTime();

/**
* @throws IllegalStateException
* outside of testing environments
* @throws IllegalStateException outside of testing environments
*/
void forgetFixedCurrentTimeForTesting();

Expand Down Expand Up @@ -88,9 +90,6 @@ default ZonedDateTime firstDayOfMonth() {

/**
* DO NOT USE THIS! Only here temporarily to fix up some legacy (static) code.
* @param input
* @param formatter
* @return
*/
static Tuple2<Instant, ParsePosition> parseToInstantBackwardCompatibleWayStatic(String input, DateTimeFormatter formatter){
ParsePosition remainder = new ParsePosition(0);
Expand All @@ -103,9 +102,6 @@ default Tuple2<Instant, ParsePosition> parseToInstantBackwardCompatibleWay(Strin

/**
* @deprecated Use {@link java.time} instead of {@link Date}, then there is no need for this method.
* @param input
* @param formatter
* @return
*/
@Deprecated
default String dateToString(Date input, DateTimeFormatter formatter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pkb.common.datetime;

import java.time.Clock;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;

public class DefaultDateTimeService implements DateTimeService {
Expand All @@ -20,6 +21,11 @@ public void moveTime(long amountToAdd, TemporalUnit unit) {
throw new IllegalStateException("Not currently in a test environment");
}

@Override
public void moveTime(TemporalAmount duration) {
throw new IllegalStateException("Not currently in a test environment");
}

@Override
public long nowNanoTime() {
return System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.pkb.common.datetime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.time.Clock;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FakeDateTimeService implements DateTimeService {
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private volatile ZonedDateTime currentFixedTime;
private volatile Clock currentFixedClock;
protected volatile ZonedDateTime currentFixedTime;
protected volatile Clock currentFixedClock;

private final DateTimeService fallbackService;

Expand Down Expand Up @@ -44,6 +45,11 @@ public void moveTime(long amountToAdd, TemporalUnit unit) {
fixTime(currentFixedTime.plus(amountToAdd, unit));
}

@Override
public void moveTime(TemporalAmount duration) {
fixTime(currentFixedTime.plus(duration));
}

@Override
public long nowNanoTime() {
Instant now = now();
Expand Down
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)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public void configure() throws Exception {
if (config().getShouldStartListener()) {
from(testControlRequestSubscription)
.threads(1) //One thread is enough
// We really want to just enable timeout, we don't need circuit breaker in e2e
.circuitBreaker()
.faultToleranceConfiguration()
.timeoutEnabled(config().testControlTimeoutEnabled())
.timeoutDuration(config().testControlTimeoutMillis())
.delay(config().testControlHandlerDelayMillis())
.requestVolumeThreshold(config().testControlHandlerRequestVolumeThreshold())
.failureRatio(config().testControlHandlerFailureRatio())
.successThreshold(config().testControlHandlerSuccessThreshold())
.end()
.routeId("testSupportReceiver")
.unmarshal().avro(TestControlRequest.getClassSchema())
.log(config().getApplicationName() + ": receiving a ${mandatoryBodyAs(" + TestControlRequest.class.getCanonicalName() + ").getMessageType} request")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,36 @@ public interface ITestControlServiceConfig {
boolean getShouldStartListener();

DateTimeService getDateTimeService();

ConfigStorage getConfigStorage();

Set<ClearableInternalState> getClearables();

DetailLoggingProvider getTestLoggingService();

PubSubNamespaceService getNamespaceService();

default boolean testControlTimeoutEnabled() {
return true;
}

default long testControlTimeoutMillis() {
return 30000;
}

default long testControlHandlerDelayMillis() {
return 0;
}

default int testControlHandlerRequestVolumeThreshold() {
return 1000;
}

default int testControlHandlerFailureRatio() {
return 100;
}

default int testControlHandlerSuccessThreshold() {
return 100;
}
}

0 comments on commit 23be3f7

Please sign in to comment.