Skip to content
Permalink
Browse files
Verifying correct retry of single beat execution.
  • Loading branch information
mifosio-04-04-2018 committed May 18, 2017
1 parent 8d08e46 commit 17be673e31583cd717171f80e33d3440357b8db4
Showing 3 changed files with 33 additions and 8 deletions.
@@ -33,6 +33,8 @@
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.mockito.AdditionalMatchers;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -136,8 +138,9 @@ Beat createBeat(final String applicationName, final String beatIdentifier) throw
beat.setAlignmentHour(now.getHour());

final LocalDateTime expectedBeatTimestamp = getExpectedBeatTimestamp(now, beat.getAlignmentHour());
Mockito.when(beatPublisherService.publishBeat(applicationName, beatIdentifier, expectedBeatTimestamp)).thenReturn(true);
Mockito.when(beatPublisherService.publishBeat(applicationName, beatIdentifier, getNextTimeStamp(expectedBeatTimestamp))).thenReturn(true);
Mockito.when(beatPublisherService.publishBeat(Matchers.eq(applicationName), Matchers.eq(beatIdentifier),
AdditionalMatchers.or(Matchers.eq(expectedBeatTimestamp), Matchers.eq(getNextTimeStamp(expectedBeatTimestamp)))))
.thenReturn(true);

this.testSubject.createBeat(applicationName, beat);

@@ -155,7 +158,7 @@ LocalDateTime getExpectedBeatTimestamp(final LocalDateTime fromTime, final Integ
return midnight.plusHours(alignmentHour);
}

LocalDateTime getNextTimeStamp(final LocalDateTime fromTime) {
private LocalDateTime getNextTimeStamp(final LocalDateTime fromTime) {
return fromTime.plusDays(1);
}
}
@@ -25,13 +25,9 @@

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;

/**
* @author Myrle Krantz
*/
@@ -67,6 +63,7 @@ public void shouldDeleteBeat() throws InterruptedException {
}
catch (final NotFoundException ignored) { }
}

@Test
public void shouldDeleteApplication() throws InterruptedException {
final String appName = "funnybusiness-v3";
@@ -78,4 +75,28 @@ public void shouldDeleteApplication() throws InterruptedException {
final List<Beat> allEntities = this.testSubject.getAllBeatsForApplication(appName);
Assert.assertTrue(allEntities.isEmpty());
}

@Test
public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedException {
final String appName = "funnybusiness-v4";
final String beatId = "bebopthedowop";

final LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));

final Beat beat = new Beat();
beat.setIdentifier(beatId);
beat.setAlignmentHour(now.getHour());

final LocalDateTime expectedBeatTimestamp = getExpectedBeatTimestamp(now, beat.getAlignmentHour());

Mockito.when(beatPublisherService.publishBeat(appName, beatId, expectedBeatTimestamp)).thenReturn(false, false, true);

this.testSubject.createBeat(appName, beat);

Assert.assertTrue(this.eventRecorder.wait(EventConstants.POST_BEAT, new BeatEvent(appName, beat.getIdentifier())));

TimeUnit.SECONDS.sleep(8);

Mockito.verify(beatPublisherService, Mockito.times(3)).publishBeat(appName, beatId, expectedBeatTimestamp);
}
}
@@ -75,7 +75,7 @@ static private int minus1(int alignmentHour) {
return alignmentHour != 0 ? alignmentHour-1 : 23;
}

private void checkBeatForPublish(final BeatEntity beat, final LocalDateTime now) {
public void checkBeatForPublish(final BeatEntity beat, final LocalDateTime now) {
try (final AutoTenantContext ignored = new AutoTenantContext((beat.getTenantIdentifier()))) {
final LocalDateTime topOfToday = now.truncatedTo(ChronoUnit.DAYS);
final LocalDateTime mostRecentEventRequired = topOfToday.plusHours(beat.getAlignmentHour());
@@ -87,6 +87,7 @@ private void checkBeatForPublish(final BeatEntity beat, final LocalDateTime now)
.filter(x -> {
if (beatPublisherService.publishBeat(beat.getApplicationName(), beat.getBeatIdentifier(), x)) {
beat.setLastPublishedFor(x);
beatRepository.save(beat);
return false;
}
return true;

0 comments on commit 17be673

Please sign in to comment.