Skip to content

Commit

Permalink
refactor testInterrupt to testStopped
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi-17 committed Jul 21, 2024
1 parent 643e09f commit 16c3361
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions twin/src/test/java/com/iluwatar/twin/BallThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
*/
package com.iluwatar.twin;

import static java.lang.Thread.UncaughtExceptionHandler;
import static java.lang.Thread.sleep;
import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -100,21 +97,27 @@ void testResume() {
}

/**
* Verify if the {@link BallThread} is interruptible
* Verify if the {@link BallThread} can be stopped
*/
@Test
void testInterrupt() {
void testStopped() {
assertTimeout(ofMillis(5000), () -> {
final var ballThread = new BallThread();
final var exceptionHandler = mock(UncaughtExceptionHandler.class);
ballThread.setUncaughtExceptionHandler(exceptionHandler);
ballThread.setTwin(mock(BallItem.class));
final var twin = mock(BallItem.class);
ballThread.setTwin(twin);
ballThread.start();
ballThread.interrupt();


sleep(300);
verify(twin, atLeastOnce()).draw();
verify(twin, atLeastOnce()).move();

// Stop the thread
ballThread.stopMe();
ballThread.join();

verify(exceptionHandler).uncaughtException(eq(ballThread), any(RuntimeException.class));
verifyNoMoreInteractions(exceptionHandler);
// Ensure that the thread has stopped and no more interactions occur
verifyNoMoreInteractions(twin);
});
}
}

0 comments on commit 16c3361

Please sign in to comment.