-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
247 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ent/sourcing/subscription/catchup/consumer/manager/DefaultEventQueueProcessingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
import static java.lang.Integer.parseInt; | ||
|
||
import uk.gov.justice.services.common.configuration.GlobalValue; | ||
|
||
import javax.annotation.Priority; | ||
import javax.enterprise.inject.Alternative; | ||
import javax.inject.Inject; | ||
|
||
@Alternative | ||
@Priority(100) | ||
public class DefaultEventQueueProcessingConfig implements EventQueueProcessingConfig { | ||
|
||
@Inject | ||
@GlobalValue(key = "catchup.event.processing.max.total.events.in.process", defaultValue = "100000") | ||
private String maxTotalEventsInProcess; | ||
|
||
@Override | ||
public int getMaxTotalEventsInProcess() { | ||
return parseInt(maxTotalEventsInProcess); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ices/event/sourcing/subscription/catchup/consumer/manager/EventQueueProcessingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
public interface EventQueueProcessingConfig { | ||
int getMaxTotalEventsInProcess(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...services/event/sourcing/subscription/catchup/consumer/manager/EventsInProcessCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class EventsInProcessCounter { | ||
|
||
private final int maxTotalEventsInProcess; | ||
private AtomicInteger eventInProcessCount = new AtomicInteger(0); | ||
|
||
public EventsInProcessCounter(final int maxTotalEventsInProcess) { | ||
this.maxTotalEventsInProcess = maxTotalEventsInProcess; | ||
} | ||
|
||
public synchronized void incrementEventsInProcessCount() { | ||
eventInProcessCount.incrementAndGet(); | ||
} | ||
|
||
public synchronized void decrementEventsInProcessCount() { | ||
eventInProcessCount.decrementAndGet(); | ||
} | ||
|
||
public synchronized boolean maxNumberOfEventsInProcess() { | ||
return eventInProcessCount.get() >= maxTotalEventsInProcess; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../event/sourcing/subscription/catchup/consumer/manager/EventsInProcessCounterProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class EventsInProcessCounterProvider { | ||
|
||
@Inject | ||
private EventQueueProcessingConfig eventQueueProcessingConfig; | ||
|
||
private ConcurrentHashMap<String, EventsInProcessCounter> concurrentHashMap = new ConcurrentHashMap<>(); | ||
|
||
public EventsInProcessCounter getInstance() { | ||
return concurrentHashMap.computeIfAbsent("default", this::newInstance); | ||
} | ||
|
||
private EventsInProcessCounter newInstance(String s) { | ||
return new EventsInProcessCounter(eventQueueProcessingConfig.getMaxTotalEventsInProcess()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ervices/event/sourcing/subscription/catchup/consumer/DummyEventQueueProcessingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer; | ||
|
||
import uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager.DefaultEventQueueProcessingConfig; | ||
import uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager.EventQueueProcessingConfig; | ||
|
||
import org.apache.openejb.testing.Default; | ||
|
||
@Default | ||
public class DummyEventQueueProcessingConfig implements EventQueueProcessingConfig { | ||
|
||
@Override | ||
public int getMaxTotalEventsInProcess() { | ||
return 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...sourcing/subscription/catchup/consumer/manager/DefaultEventQueueProcessingConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.setField; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class DefaultEventQueueProcessingConfigTest { | ||
|
||
@InjectMocks | ||
private DefaultEventQueueProcessingConfig defaultEventQueueProcessingConfig; | ||
|
||
@Test | ||
public void shouldGetTheInjectedJndiValue() throws Exception { | ||
|
||
setField(defaultEventQueueProcessingConfig, "maxTotalEventsInProcess", "23"); | ||
|
||
assertThat(defaultEventQueueProcessingConfig.getMaxTotalEventsInProcess(), is(23)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...nt/sourcing/subscription/catchup/consumer/manager/EventsInProcessCounterProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package uk.gov.justice.services.event.sourcing.subscription.catchup.consumer.manager; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.getValueOfField; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class EventsInProcessCounterProviderTest { | ||
|
||
@Mock | ||
private EventQueueProcessingConfig eventQueueProcessingConfig; | ||
|
||
@InjectMocks | ||
private EventsInProcessCounterProvider eventsInProcessCounterProvider; | ||
|
||
@Test | ||
public void shouldCreateWithCorrectMaxTotalEventsInProcess() throws Exception { | ||
|
||
final int maxTotalEventsInProcess = 982734; | ||
|
||
when(eventQueueProcessingConfig.getMaxTotalEventsInProcess()).thenReturn(maxTotalEventsInProcess); | ||
|
||
final EventsInProcessCounter eventsInProcessCounter = eventsInProcessCounterProvider.getInstance(); | ||
|
||
assertThat(getValueOfField(eventsInProcessCounter, "maxTotalEventsInProcess", Integer.class), is(maxTotalEventsInProcess)); | ||
} | ||
|
||
@Test | ||
public void shouldAlwaysReturnTheSameInstance() throws Exception { | ||
|
||
final EventsInProcessCounter eventsInProcessCounter = eventsInProcessCounterProvider.getInstance(); | ||
|
||
assertThat(eventsInProcessCounterProvider.getInstance(), is(sameInstance(eventsInProcessCounter))); | ||
assertThat(eventsInProcessCounterProvider.getInstance(), is(sameInstance(eventsInProcessCounter))); | ||
assertThat(eventsInProcessCounterProvider.getInstance(), is(sameInstance(eventsInProcessCounter))); | ||
assertThat(eventsInProcessCounterProvider.getInstance(), is(sameInstance(eventsInProcessCounter))); | ||
assertThat(eventsInProcessCounterProvider.getInstance(), is(sameInstance(eventsInProcessCounter))); | ||
} | ||
} |
Oops, something went wrong.