Skip to content
Permalink
Browse files
Tightening scope of tenant context.
  • Loading branch information
mifosio-04-04-2018 committed May 19, 2017
1 parent 53b3be3 commit e059586243eb0eaeb37dd975eae1ed7743e69b37
Showing 3 changed files with 8 additions and 10 deletions.
@@ -88,7 +88,7 @@ public Logger logger() {
private final static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
private final static CassandraInitializer cassandraInitializer = new CassandraInitializer();
private final static MariaDBInitializer mariaDBInitializer = new MariaDBInitializer();
private final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer);
final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer);

@ClassRule
public static TestRule orderClassRules = RuleChain
@@ -139,7 +139,7 @@ Beat createBeat(final String applicationName, final String beatIdentifier) throw
beat.setAlignmentHour(now.getHour());

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

this.testSubject.createBeat(applicationName, beat);
@@ -149,7 +149,7 @@ Beat createBeat(final String applicationName, final String beatIdentifier) throw
Mockito.verify(beatPublisherServiceSpy, Mockito.timeout(2_000).atLeastOnce())
.checkBeatForPublish(anyObject(), eq(beatIdentifier), anyString(), eq(applicationName), eq(beat.getAlignmentHour()), anyObject());

Mockito.verify(beatPublisherServiceSpy, Mockito.times(1)).publishBeat(applicationName, beatIdentifier, expectedBeatTimestamp);
Mockito.verify(beatPublisherServiceSpy, Mockito.times(1)).publishBeat(beatIdentifier, tenantDataStoreContext.getTenantName(), applicationName, expectedBeatTimestamp);

return beat;
}
@@ -90,7 +90,7 @@ public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedExcept

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

Mockito.when(beatPublisherServiceSpy.publishBeat(appName, beatId, expectedBeatTimestamp)).thenReturn(false, false, true);
Mockito.when(beatPublisherServiceSpy.publishBeat(beatId, tenantDataStoreContext.getTenantName(), appName, expectedBeatTimestamp)).thenReturn(false, false, true);

this.testSubject.createBeat(appName, beat);

@@ -99,6 +99,6 @@ public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedExcept
Mockito.verify(super.beatPublisherServiceSpy, Mockito.timeout(10_000).atLeast(3))
.checkBeatForPublish(anyObject(), eq(beatId), anyString(), eq(appName), eq(beat.getAlignmentHour()), anyObject());

Mockito.verify(beatPublisherServiceSpy, Mockito.times(3)).publishBeat(appName, beatId, expectedBeatTimestamp);
Mockito.verify(beatPublisherServiceSpy, Mockito.times(3)).publishBeat(beatId, tenantDataStoreContext.getTenantName(), appName, expectedBeatTimestamp);
}
}
@@ -60,7 +60,7 @@ public BeatPublisherService(
}

@SuppressWarnings("WeakerAccess") //Access is public for spying in component test.
public boolean publishBeat(final String applicationName, final String beatIdentifier, final LocalDateTime timestamp) {
public boolean publishBeat(final String beatIdentifier, final String tenantIdentifier, final String applicationName, final LocalDateTime timestamp) {
final BeatPublish beatPublish = new BeatPublish(beatIdentifier, DateConverter.toIsoString(timestamp));
logger.info("Attempting publish {} with timestamp {} under user {}.", beatPublish, timestamp, properties.getUser());

@@ -71,7 +71,7 @@ public boolean publishBeat(final String applicationName, final String beatIdenti
final ServiceInstance beatListenerService = applicationsByName.get(0);
final BeatListener beatListener = apiFactory.create(BeatListener.class, beatListenerService.getUri().toString());

try {
try (final AutoTenantContext ignored = new AutoTenantContext(tenantIdentifier)) {
beatListener.publishBeat(beatPublish);
return true;
}
@@ -87,14 +87,12 @@ public Optional<LocalDateTime> checkBeatForPublish(
final String applicationName,
final Integer alignmentHour,
final LocalDateTime nextBeat) {
try (final AutoTenantContext ignored = new AutoTenantContext(tenantIdentifier)) {

return getTimesNeedingEvents(nextBeat, now, alignmentHour)
.filter(x ->
x.isAfter(now)
|| !publishBeat(applicationName, beatIdentifier, x))
|| !publishBeat(beatIdentifier, tenantIdentifier, applicationName, x))
.findFirst();
}
}

static Stream<LocalDateTime> getTimesNeedingEvents(

0 comments on commit e059586

Please sign in to comment.