Skip to content

Commit

Permalink
ConversationHandler tests
Browse files Browse the repository at this point in the history
Now testing that the threading parts of ConversationHandler works
  • Loading branch information
Zargess committed Nov 24, 2024
1 parent affab03 commit 75b6e59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void tick() {
conversation.addMessage(message);
data.storeConversation(conversation);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
ie.printStackTrace();
}
}
Expand Down
24 changes: 24 additions & 0 deletions fagiServer/src/test/java/TextMessageIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,28 @@ void sendingAMessageToConversation_ShouldResultInConversationBeingStored() {
.verify(data, times(1))
.storeConversation(conversation);
}

@Test
void callingRun_ShouldCallTickAndSendMesasage() throws InterruptedException {
when(data.getConversation(Mockito.anyLong())).thenReturn(conversation);

inputHandler.handleInput(message);

var conversationThread = new Thread(conversationHandler);
conversationThread.setDaemon(true);
conversationThread.start();

while (conversationHandler.queueSize() > 0 && conversationThread.isAlive()) {
Thread.sleep(10); // Minimal sleep to avoid busy-waiting
}

conversationThread.interrupt();

conversationThread.join(1000);

Assertions.assertAll(
() -> Assertions.assertTrue(conversationThread.isInterrupted()),
() -> Assertions.assertTrue(conversation.getMessages().contains(message))
);
}
}

0 comments on commit 75b6e59

Please sign in to comment.