77import org .junit .jupiter .api .BeforeEach ;
88import org .junit .jupiter .api .Test ;
99import org .mockito .ArgumentCaptor ;
10+ import org .mockito .internal .stubbing .answers .AnswersWithDelay ;
11+ import org .mockito .internal .stubbing .answers .Returns ;
1012import org .mockito .stubbing .Answer ;
1113import org .slf4j .Logger ;
1214import org .slf4j .LoggerFactory ;
2426import static org .assertj .core .api .Assertions .assertThat ;
2527import static org .mockito .ArgumentMatchers .eq ;
2628import static org .mockito .ArgumentMatchers .isNull ;
27- import static org .mockito .Mockito .any ;
28- import static org .mockito .Mockito .mock ;
29- import static org .mockito .Mockito .never ;
30- import static org .mockito .Mockito .spy ;
31- import static org .mockito .Mockito .timeout ;
32- import static org .mockito .Mockito .times ;
33- import static org .mockito .Mockito .verify ;
34- import static org .mockito .Mockito .when ;
29+ import static org .mockito .Mockito .*;
3530
3631class EventProcessorTest {
3732
@@ -40,6 +35,7 @@ class EventProcessorTest {
4035 public static final int FAKE_CONTROLLER_EXECUTION_DURATION = 250 ;
4136 public static final int SEPARATE_EXECUTION_TIMEOUT = 450 ;
4237 public static final String TEST_NAMESPACE = "default-event-handler-test" ;
38+
4339 private ReconciliationDispatcher reconciliationDispatcherMock =
4440 mock (ReconciliationDispatcher .class );
4541 private EventSourceManager eventSourceManagerMock = mock (EventSourceManager .class );
@@ -188,24 +184,27 @@ void scheduleTimedEventIfInstructedByPostExecutionControl() {
188184 }
189185
190186 @ Test
191- void reScheduleOnlyIfNotExecutedEventsReceivedMeanwhile () {
187+ void reScheduleOnlyIfNotExecutedEventsReceivedMeanwhile () throws InterruptedException {
192188 var testDelay = 10000L ;
193- when (reconciliationDispatcherMock .handleExecution (any ()))
194- .thenReturn (PostExecutionControl .defaultDispatch ().withReSchedule (testDelay ));
195-
196- eventProcessor .handleEvent (prepareCREvent ());
197- eventProcessor .handleEvent (prepareCREvent ());
198-
199- verify (retryTimerEventSourceMock , timeout (SEPARATE_EXECUTION_TIMEOUT ).times (0 ))
200- .scheduleOnce (any (), eq (testDelay ));
189+ doAnswer (new AnswersWithDelay (FAKE_CONTROLLER_EXECUTION_DURATION ,
190+ new Returns (PostExecutionControl .defaultDispatch ().withReSchedule (testDelay ))))
191+ .when (reconciliationDispatcherMock ).handleExecution (any ());
192+ var resourceId = new ResourceID ("test1" , "default" );
193+ eventProcessor .handleEvent (prepareCREvent (resourceId ));
194+ Thread .sleep (FAKE_CONTROLLER_EXECUTION_DURATION / 3 );
195+ eventProcessor .handleEvent (prepareCREvent (resourceId ));
196+
197+ verify (retryTimerEventSourceMock ,
198+ after ((long ) (FAKE_CONTROLLER_EXECUTION_DURATION * 1.5 )).times (0 ))
199+ .scheduleOnce (any (), eq (testDelay ));
201200 }
202201
203202 @ Test
204203 void doNotFireEventsIfClosing () {
205204 eventProcessor .stop ();
206205 eventProcessor .handleEvent (prepareCREvent ());
207206
208- verify (reconciliationDispatcherMock , timeout (50 ).times (0 )).handleExecution (any ());
207+ verify (reconciliationDispatcherMock , after (50 ).times (0 )).handleExecution (any ());
209208 }
210209
211210 @ Test
0 commit comments