Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Fix flaky IrisMessageIntegrationTest #7317

Merged
merged 6 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.tum.in.www1.artemis.iris;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -141,36 +142,4 @@ protected void verifyErrorWasSentOverWebsocket(String user, Long sessionId) {
ArgumentMatchers.argThat(object -> object instanceof IrisWebsocketService.IrisWebsocketDTO websocketDTO
&& websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.ERROR));
}

/**
* Verify that nothing was sent through the websocket.
*
* @param user the user
* @param sessionId the session id
*/
protected void verifyNothingWasSentOverWebsocket(String user, Long sessionId) {
verify(websocketMessagingService, times(0)).sendMessageToUser(eq(user), eq("/topic/iris/sessions/" + sessionId), any());
}

/**
* Verify that nothing else was sent through the websocket.
*
* @param user the user
* @param sessionId the session id
*/
protected void verifyNothingElseWasSentOverWebsocket(String user, Long sessionId) {
verifyNoMoreInteractions(websocketMessagingService);
}

/**
* Verify that no error was sent through the websocket.
*
* @param user the user
* @param sessionId the session id
*/
protected void verifyNoErrorWasSentOverWebsocket(String user, Long sessionId) {
verify(websocketMessagingService, times(0)).sendMessageToUser(eq(user), eq("/topic/iris/sessions/" + sessionId),
ArgumentMatchers.argThat(object -> object instanceof IrisWebsocketService.IrisWebsocketDTO websocketDTO
&& websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.ERROR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import java.time.ZonedDateTime;
import java.util.List;
Expand Down Expand Up @@ -90,13 +94,12 @@ void sendOneMessage() throws Exception {

verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend);
verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), "Hello World");
verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
}

@Test
@WithMockUser(username = TEST_PREFIX + "student1", roles = "USER")
void sendOneMessageToWrongSession() throws Exception {
var irisSession1 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1"));
irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1"));
var irisSession2 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student2"));
IrisMessage messageToSend = createDefaultMockMessage(irisSession2);
request.postWithResponseBody("/api/iris/sessions/" + irisSession2.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.FORBIDDEN);
Expand Down Expand Up @@ -138,6 +141,8 @@ void sendTwoMessages() throws Exception {
.isEqualTo(messageToSend2.getContent().stream().map(IrisMessageContent::getTextContent).toList());
irisSessionFromDb = irisSessionRepository.findByIdWithMessagesElseThrow(irisSession.getId());
assertThat(irisSessionFromDb.getMessages()).hasSize(2).isEqualTo(List.of(irisMessage1, irisMessage2));

verify(websocketMessagingService, timeout(3000).times(4)).sendMessageToUser(eq(TEST_PREFIX + "student1"), any(), any());
}

@Test
Expand Down Expand Up @@ -245,7 +250,6 @@ void sendOneMessageBadRequest() throws Exception {

verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend);
verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
}

@Test
Expand All @@ -261,7 +265,6 @@ void sendOneMessageEmptyBody() throws Exception {

verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend);
verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
}

@Test
Expand All @@ -277,7 +280,6 @@ void resendMessage() throws Exception {
request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages/" + irisMessage.getId() + "/resend", null, IrisMessage.class, HttpStatus.OK);
await().until(() -> irisSessionRepository.findByIdWithMessagesElseThrow(irisSession.getId()).getMessages().size() == 2);
verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), "Hello World");
verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId());
}

@Test
Expand All @@ -302,7 +304,6 @@ void sendMessageRateLimitReached() throws Exception {
HttpStatus.TOO_MANY_REQUESTS);
verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student2", irisSession.getId(), messageToSend1);
verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student2", irisSession.getId(), "Hello World");
verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student2", irisSession.getId());
}
finally {
ReflectionTestUtils.setField(irisRateLimitService, "rateLimit", previousRateLimit);
Expand Down
Loading