Skip to content

Commit

Permalink
hide containerService.getTickCallbackRunning method. Expose changedel…
Browse files Browse the repository at this point in the history
…ay() instead, one public and the cron version for internal usage only + add test
  • Loading branch information
gabibeyo committed Sep 30, 2020
1 parent e38cdc7 commit f89ffd8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
19 changes: 16 additions & 3 deletions src/main/java/com/imperva/stepping/ContainerService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.imperva.stepping;

import java.util.concurrent.TimeUnit;

public class ContainerService extends ContainerDefaultImpl {

static final String RUNNING_SCHEDULED = ".runningScheduled";
static final String DECORATOR = ".decorator";
static final String STEPPING_PRIVATE_CONTAINER = "__STEPPING_PRIVATE_CONTAINER__";

public RunningScheduled getTickCallbackRunning(String stepID) {
public void changeDelay(String stepID, int delay, int initialDelay, TimeUnit timeUnit) {
try {
String runningID = stepID + RUNNING_SCHEDULED;
return ((RunningScheduled) ((Container) getById(STEPPING_PRIVATE_CONTAINER)).getById(runningID));
((RunningScheduled) ((Container) getById(STEPPING_PRIVATE_CONTAINER)).getById(runningID)).changeDelay(delay, initialDelay, timeUnit);
} catch (Exception e) {
throw new SteppingException("Running object not found. Please make sure that you enabled TickCallback for this step");
throw new SteppingException("Change Delay Failed. Please make sure that you enabled TickCallback for this step");
}
}

Expand All @@ -23,5 +25,16 @@ public int getQSize(String stepID) {
throw new SteppingException("Q size not found. Please make sure you use the correct Step ID");
}
}

//* FOR INTERNAL USAGE ONLY - cron expression must be re-evaluated every tickcallback, this is possible only with StepConfig API
void changeDelay(String stepID, String cron) {
try {
String runningID = stepID + RUNNING_SCHEDULED;
((RunningScheduled) ((Container) getById(STEPPING_PRIVATE_CONTAINER)).getById(runningID)).changeDelay(cron);
} catch (Exception e) {
throw new SteppingException("Change Delay Failed. Please make sure that you enabled TickCallback for this step");
}
}

}

4 changes: 4 additions & 0 deletions src/main/java/com/imperva/stepping/RunningScheduled.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public void changeDelay(String cronExpression){
scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(runnable, delay, delay, TimeUnit.MILLISECONDS);
}

public long getDelay(TimeUnit timeUnit){
return scheduledFuture.getDelay(timeUnit);
}

private long calculateDelay(String cronExpression) {
try {
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/imperva/stepping/StepDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public void openDataSink() {
}

private void changeTickCallBackDelay(String cronExpression) {
RunningScheduled runningScheduled = ((ContainerService) container).getTickCallbackRunning(getStep().getId());
runningScheduled.changeDelay(cronExpression);
((ContainerService) container).changeDelay(getStep().getId(), cronExpression);
}

private void setThreadName() {
Expand Down
17 changes: 11 additions & 6 deletions src/test/java/com/imperva/stepping/ContainerServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -13,18 +15,21 @@
class ContainerServiceTest {

@Test
void getTickCallbackRunning() {
RunningScheduled value = mock(RunningScheduled.class);
void changeDelay_delay_changed() {
RunningScheduled runningScheduled = new RunningScheduled("test", 2, 2, TimeUnit.SECONDS, () -> {
});

Container innerContainer = new ContainerService();
innerContainer.add(value, "stepId3" + ContainerService.RUNNING_SCHEDULED);
innerContainer.add(runningScheduled, "stepId3" + ContainerService.RUNNING_SCHEDULED);

ContainerService containerService = new ContainerService();
containerService.add(innerContainer, ContainerService.STEPPING_PRIVATE_CONTAINER);
runningScheduled.awake();
containerService.changeDelay("stepId3", 20, 20, TimeUnit.SECONDS);
RunningScheduled e = ((RunningScheduled) ((Container) containerService.getById(ContainerService.STEPPING_PRIVATE_CONTAINER)).getById("stepId3" + ContainerService.RUNNING_SCHEDULED));
runningScheduled.close();

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

Assertions.assertEquals(value, actual);
Assertions.assertNotEquals(2, e.getDelay(TimeUnit.SECONDS));
}

@Test
Expand Down

0 comments on commit f89ffd8

Please sign in to comment.