Skip to content
Permalink
Browse files
Beat execution via discovery client. First draft.
  • Loading branch information
mifosio-04-04-2018 committed May 18, 2017
1 parent 55080ba commit 8d08e46c777a0efc4f28dc123eda5983334a43a6
Showing 18 changed files with 348 additions and 198 deletions.
@@ -2,7 +2,7 @@

[![Join the chat at https://gitter.im/mifos-initiative/mifos.io](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mifos-initiative/mifos.io?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

This project provides a heart beat for other microservices which need to start jobs to be executed once and only once at a defined time.
This project provides a heart beatPublish for other microservices which need to start jobs to be executed once and only once at a defined time.

## Abstract
Mifos I/O is an application framework for digital financial services, a system to support nationwide and cross-national financial transactions and help to level and speed the creation of an inclusive, interconnected digital economy for every nation in the world.
@@ -4,20 +4,27 @@ task publishApiToMavenLocal {
dependsOn gradle.includedBuild('api').task(':publishToMavenLocal')
}

task publishSpiToMavenLocal {
dependsOn gradle.includedBuild('spi').task(':publishToMavenLocal')
}

task publishServiceToMavenLocal {
mustRunAfter publishApiToMavenLocal
mustRunAfter publishSpiToMavenLocal
dependsOn gradle.includedBuild('service').task(':publishToMavenLocal')
}

task publishComponentTestToMavenLocal {
mustRunAfter publishApiToMavenLocal
mustRunAfter publishSpiToMavenLocal
mustRunAfter publishServiceToMavenLocal
dependsOn gradle.includedBuild('component-test').task(':publishToMavenLocal')
}

task publishToMavenLocal {
group 'all'
dependsOn publishApiToMavenLocal
dependsOn publishSpiToMavenLocal
dependsOn publishServiceToMavenLocal
dependsOn publishComponentTestToMavenLocal
}
@@ -31,6 +38,7 @@ task prepareForTest {
task licenseFormat {
group 'all'
dependsOn gradle.includedBuild('api').task(':licenseFormat')
dependsOn gradle.includedBuild('spi').task(':licenseFormat')
dependsOn gradle.includedBuild('service').task(':licenseFormat')
dependsOn gradle.includedBuild('component-test').task(':licenseFormat')
}
@@ -28,16 +28,17 @@
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 io.mifos.rhythm.service.internal.service.BeatPublisherService;
import org.junit.*;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
@@ -46,13 +47,18 @@
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
* @author Myrle Krantz
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = {AbstractRhythmTest.TestConfiguration.class},
properties = {"rhythm.user=homer", "rhythm.beatCheckRate=2000"}
properties = {"rhythm.user=homer", "rhythm.beatCheckRate=1000"}
)
public class AbstractRhythmTest {

@@ -100,6 +106,9 @@ public Logger logger() {
@Autowired
EventRecorder eventRecorder;

@MockBean
BeatPublisherService beatPublisherService;

@Before
public void prepTest() {
userContext = tenantApplicationSecurityEnvironment.createAutoUserContext(TEST_USER);
@@ -120,14 +129,33 @@ public boolean waitForInitialize() {
}

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

final Beat beat = new Beat();
beat.setIdentifier(beatIdentifier);
beat.setAlignmentHour(now.getHourOfDay());
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);

this.testSubject.createBeat(applicationName, beat);

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

TimeUnit.SECONDS.sleep(2);

Mockito.verify(beatPublisherService, Mockito.times(1)).publishBeat(applicationName, beatIdentifier, expectedBeatTimestamp);

return beat;
}

LocalDateTime getExpectedBeatTimestamp(final LocalDateTime fromTime, final Integer alignmentHour) {
final LocalDateTime midnight = fromTime.truncatedTo(ChronoUnit.DAYS);
return midnight.plusHours(alignmentHour);
}

LocalDateTime getNextTimeStamp(final LocalDateTime fromTime) {
return fromTime.plusDays(1);
}
}
@@ -21,8 +21,16 @@
import io.mifos.rhythm.api.v1.events.EventConstants;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

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
@@ -70,14 +78,4 @@ public void shouldDeleteApplication() throws InterruptedException {
final List<Beat> allEntities = this.testSubject.getAllBeatsForApplication(appName);
Assert.assertTrue(allEntities.isEmpty());
}

/*
@Test
public void shouldExecuteBeat() throws InterruptedException {
final Application application = createApplication("funnybusiness-v2");
final Beat beat = createBeat(application, "bebopthedowop");
TimeUnit.MINUTES.sleep(5);
}*/
}
@@ -31,6 +31,7 @@ dependencies {
[group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka'],
[group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'],
[group: 'io.mifos.rhythm', name: 'api', version: project.version],
[group: 'io.mifos.rhythm', name: 'spi', version: project.version],
[group: 'io.mifos.anubis', name: 'library', version: versions.frameworkanubis],
[group: 'com.google.code.gson', name: 'gson'],
[group: 'io.mifos.core', name: 'lang', version: versions.frameworklang],
@@ -16,6 +16,7 @@
package io.mifos.rhythm.service;

import io.mifos.anubis.config.EnableAnubis;
import io.mifos.core.api.config.EnableApiFactory;
import io.mifos.core.async.config.EnableAsync;
import io.mifos.core.cassandra.config.EnableCassandra;
import io.mifos.core.command.config.EnableCommandProcessing;
@@ -39,6 +40,7 @@
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableApiFactory
@EnableAsync
@EnableCassandra
@EnableMariaDB(forTenantContext = false)

This file was deleted.

This file was deleted.

@@ -45,9 +45,9 @@ public class BeatEntity {
@Column(name = "alignment_hour", nullable = false)
private Integer alignmentHour;

@Column(name = "last_published_on")
@Column(name = "last_published_for")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime lastPublishedOn;
private LocalDateTime lastPublishedFor;

public BeatEntity() {
super();
@@ -93,12 +93,12 @@ public void setAlignmentHour(Integer alignmentHour) {
this.alignmentHour = alignmentHour;
}

public LocalDateTime getLastPublishedOn() {
return lastPublishedOn;
public LocalDateTime getLastPublishedFor() {
return lastPublishedFor;
}

public void setLastPublishedOn(LocalDateTime lastPublishedOn) {
this.lastPublishedOn = lastPublishedOn;
public void setLastPublishedFor(LocalDateTime lastPublishedFor) {
this.lastPublishedFor = lastPublishedFor;
}

@Override
@@ -124,7 +124,7 @@ public String toString() {
", tenantIdentifier='" + tenantIdentifier + '\'' +
", applicationName='" + applicationName + '\'' +
", alignmentHour=" + alignmentHour +
", lastPublishedOn=" + lastPublishedOn +
", lastPublishedFor=" + lastPublishedFor +
'}';
}
}

0 comments on commit 8d08e46

Please sign in to comment.