From f89ffd8a4d8a9ff110537bbb1493ac2334a8438e Mon Sep 17 00:00:00 2001 From: "gabi.beyo" Date: Wed, 30 Sep 2020 11:25:02 +0300 Subject: [PATCH] hide containerService.getTickCallbackRunning method. Expose changedelay() instead, one public and the cron version for internal usage only + add test --- .../imperva/stepping/ContainerService.java | 19 ++++++++++++++++--- .../imperva/stepping/RunningScheduled.java | 4 ++++ .../com/imperva/stepping/StepDecorator.java | 3 +-- .../stepping/ContainerServiceTest.java | 17 +++++++++++------ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/imperva/stepping/ContainerService.java b/src/main/java/com/imperva/stepping/ContainerService.java index 10fde22..7f9c59c 100644 --- a/src/main/java/com/imperva/stepping/ContainerService.java +++ b/src/main/java/com/imperva/stepping/ContainerService.java @@ -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"); } } @@ -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"); + } + } + } diff --git a/src/main/java/com/imperva/stepping/RunningScheduled.java b/src/main/java/com/imperva/stepping/RunningScheduled.java index 83d9ddd..eceee0f 100644 --- a/src/main/java/com/imperva/stepping/RunningScheduled.java +++ b/src/main/java/com/imperva/stepping/RunningScheduled.java @@ -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); diff --git a/src/main/java/com/imperva/stepping/StepDecorator.java b/src/main/java/com/imperva/stepping/StepDecorator.java index 7311486..71b77b2 100644 --- a/src/main/java/com/imperva/stepping/StepDecorator.java +++ b/src/main/java/com/imperva/stepping/StepDecorator.java @@ -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() { diff --git a/src/test/java/com/imperva/stepping/ContainerServiceTest.java b/src/test/java/com/imperva/stepping/ContainerServiceTest.java index b307db1..11f258e 100644 --- a/src/test/java/com/imperva/stepping/ContainerServiceTest.java +++ b/src/test/java/com/imperva/stepping/ContainerServiceTest.java @@ -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; @@ -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