Skip to content
Permalink
Browse files
Making sure a beat happens and is processed in the course of the test.
  • Loading branch information
mifosio-04-04-2018 committed May 17, 2017
1 parent 853a677 commit 55080bafad2dbd9858efd37d9a57e32a047ee215
Showing 4 changed files with 22 additions and 8 deletions.
@@ -28,6 +28,8 @@
import io.mifos.rhythm.api.v1.events.BeatEvent;
import io.mifos.rhythm.api.v1.events.EventConstants;
import io.mifos.rhythm.service.RhythmConfiguration;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.*;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
@@ -50,7 +52,7 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = {AbstractRhythmTest.TestConfiguration.class},
properties = {"rhythm.user=homer", "rhythm.beatCheckRate=4000"}
properties = {"rhythm.user=homer", "rhythm.beatCheckRate=2000"}
)
public class AbstractRhythmTest {

@@ -118,9 +120,11 @@ public boolean waitForInitialize() {
}

Beat createBeat(final String applicationName, final String beatIdentifier) throws InterruptedException {
final DateTime now = DateTime.now(DateTimeZone.UTC);

final Beat beat = new Beat();
beat.setIdentifier(beatIdentifier);
beat.setAlignmentHour(0);
beat.setAlignmentHour(now.getHourOfDay());
this.testSubject.createBeat(applicationName, beat);

Assert.assertTrue(this.eventRecorder.wait(EventConstants.POST_BEAT, new BeatEvent(applicationName, beat.getIdentifier())));
@@ -27,7 +27,7 @@
@ConfigurationProperties(prefix="rhythm")
public class RhythmProperties {
private String user;
private Long beatCheckRate = TimeUnit.MINUTES.toMillis(5);
private Long beatCheckRate = TimeUnit.MINUTES.toMillis(10);

public RhythmProperties() {
}
@@ -64,6 +64,7 @@ public void process(final CheckPublishBeatCommand checkPublishBeatCommand) {
checkPublishBeatCommand.getTenantIdentifier(),
checkPublishBeatCommand.getApplicationName(),
checkPublishBeatCommand.getBeatIdentifier()));
logger.info("Checking if beat {} should be published under user {}.", beat, properties.getUser());


getTimesNeedingEvents(beat.getLastPublishedOn(), checkPublishBeatCommand.getPublishedSince(), beat.getAlignmentHour())
@@ -56,7 +56,10 @@ public void checkForBeatsNeeded() {
try {
final LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));
int alignmentHour = now.getHour();
final Stream<BeatEntity> beats = beatRepository.findByAlignmentHour(alignmentHour);
//Get beats from the last two hours in case restart/start happens close to hour begin.
final Stream<BeatEntity> beats =
Stream.concat(beatRepository.findByAlignmentHour(alignmentHour),
beatRepository.findByAlignmentHour(minus1(alignmentHour)));
beats.forEach((beat) -> publishBeat(beat, now));
}
catch (final InvalidDataAccessResourceUsageException e) {
@@ -65,10 +68,16 @@ public void checkForBeatsNeeded() {
}
}

private int minus1(int alignmentHour) {
return alignmentHour != 0 ? alignmentHour-1 : 23;
}

private void publishBeat(final BeatEntity beat, final LocalDateTime now) {
final LocalDateTime topOfToday = now.truncatedTo(ChronoUnit.DAYS);
final LocalDateTime publishedSince = topOfToday.plusHours(beat.getAlignmentHour());
commandGateway.process(
new CheckPublishBeatCommand(beat.getTenantIdentifier(), beat.getApplicationName(), beat.getBeatIdentifier(), publishedSince));
try (final AutoTenantContext ignored = new AutoTenantContext((beat.getTenantIdentifier()))) {
final LocalDateTime topOfToday = now.truncatedTo(ChronoUnit.DAYS);
final LocalDateTime publishedSince = topOfToday.plusHours(beat.getAlignmentHour());
commandGateway.process(
new CheckPublishBeatCommand(beat.getTenantIdentifier(), beat.getApplicationName(), beat.getBeatIdentifier(), publishedSince));
}
}
}

0 comments on commit 55080ba

Please sign in to comment.