Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StepDecorator unit tests + jacoco plugin #12

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/imperva/stepping/AlgoDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -326,7 +328,6 @@ private void setRunningScheduledDelay(RunningScheduled runningScheduled, StepCon
private void initSteps() {
for (IStepDecorator step : cntr.<IStepDecorator>getSonOf(IStepDecorator.class)) {
step.init(cntrPublic);
step.setAlgoConfig(getConfig());
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/imperva/stepping/IStepDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ default void onTickCallBack() {

Step getStep();

void setAlgoConfig(AlgoConfig algoConfig);

void setDistributionNodeID(String name);

String getDistributionNodeID();
Expand Down
52 changes: 21 additions & 31 deletions src/main/java/com/imperva/stepping/StepDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
class StepDecorator implements IStepDecorator {
private final Logger logger = LoggerFactory.getLogger(StepDecorator.class);
protected Container container;
private Q<Message> q;
Q<Message> q;
private Step step;
private AlgoConfig algoConfig;
private StepConfig localStepConfig;
private String subjectDistributionID = "default";
private volatile boolean dead = false;
private Follower follower = null;
volatile boolean dead = false;
private Follower follower;
private CyclicBarrier cb;
private String id;
private HashMap<String, SubjectUpdateEvent> subjectUpdateEvents = new HashMap<>();
HashMap<String, SubjectUpdateEvent> subjectUpdateEvents = new HashMap<>();


StepDecorator(Step step) {
Expand Down Expand Up @@ -109,29 +108,27 @@ public void openDataSink() {
throw new InterruptedException();
}

if (message != null && message.getData() != null) {
if (!message.getSubjectType().equals(BuiltinSubjectType.STEPPING_TIMEOUT_CALLBACK.name())) {
if (!message.getSubjectType().equals(BuiltinSubjectType.STEPPING_TIMEOUT_CALLBACK.name())) {

SubjectUpdateEvent subjectUpdateEvent = subjectUpdateEvents.get(message.getSubjectType());
if (subjectUpdateEvent != null)
subjectUpdateEvent.onUpdate(message.getData());
SubjectUpdateEvent subjectUpdateEvent = subjectUpdateEvents.get(message.getSubjectType());
if (subjectUpdateEvent != null)
subjectUpdateEvent.onUpdate(message.getData());

onSubjectUpdate(message.getData(), message.getSubjectType());
onSubjectUpdate(message.getData(), message.getSubjectType());

} else {
try {
onTickCallBack();
if (getConfig().getRunningPeriodicCronDelay() != null) {
try {
changeTickCallBackDelay(getConfig().getRunningPeriodicCronDelay());
} catch (Exception x) {
throw new SteppingException(x.toString());
}
} else {
try {
onTickCallBack();
if (getConfig().getRunningPeriodicCronDelay() != null) {
try {
changeTickCallBackDelay(getConfig().getRunningPeriodicCronDelay());
} catch (Exception x) {
throw new SteppingException(x.toString());
}
} finally {
cb = (CyclicBarrier) message.getData().getValue();
cb.await();
}
} finally {
cb = (CyclicBarrier) message.getData().getValue();
cb.await();
}
}
}
Expand All @@ -156,7 +153,7 @@ private void setThreadName() {
@Override
public void attachSubjects() {
Follower follower = listSubjectsToFollow();
if (follower != null && follower.size() != 0) {
if (follower.size() != 0) {
for (FollowRequest followRequest : follower.get()) {
ISubject s = container.getById(followRequest.getSubjectType());
if (s == null) {
Expand Down Expand Up @@ -249,13 +246,6 @@ public Step getStep() {
return step;
}

@Override
public void setAlgoConfig(AlgoConfig algoConfig) {
if (algoConfig == null)
throw new IdentifiableSteppingException(this.step.getId(), "AlgoConfig is required");
this.algoConfig = algoConfig;
}

@Override
public StepConfig getConfig() {
if (localStepConfig != null)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/imperva/stepping/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.imperva.stepping;

// TODO remove this class and use org.springframework.util.StringUtils instead (already have the dependency)
public class StringUtils {
public static boolean isEmpty(String s) {
return s == null || "".equals(s);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/imperva/stepping/Consts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.imperva.stepping;

/**
* Author: Linda Nasredin
* Date: 22 Sep 2020
*/
class Consts {

static final int WAIT_TIMEOUT_MILLIS = 500;
static final String POINSON_PILL_SUBJECT_TYPE = "POISON-PILL";
}
9 changes: 3 additions & 6 deletions src/test/java/com/imperva/stepping/ContainerServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.imperva.stepping;

import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -27,7 +24,7 @@ void getTickCallbackRunning() {

RunningScheduled actual = containerService.getTickCallbackRunning("stepId3");

Assert.assertEquals(value, actual);
Assertions.assertEquals(value, actual);
}

@Test
Expand All @@ -43,6 +40,6 @@ void getQSize() {

int qSize = containerService.getQSize("stepId1");

Assert.assertEquals(34, qSize);
Assertions.assertEquals(34, qSize);
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/imperva/stepping/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.imperva.stepping;

/**
* A simple way to pass integer by reference which is used for basic counting functionality - add 1 or subtract 1
*
* Why not org.apache.commons.lang.mutable.MutableInt? We don't want to add commons-lang3 dependency just for one class
* Why not AtomicReference? It's an overkill, most of the time we don't need atomicity
* Why not sun.java2d.xr.MutableInteger? It's a java internal class and we can't reference it from our code (all sun.* classes are)
*
* Author: Linda Nasredin
* Date: 21 Sep 2020
*/
class Counter {

private int value;

Counter(int value) {
this.value = value;
}

void increment() {
value++;
}

void decrement() {
value--;
}

int get() {
return value;
}
}
Loading