From: Kai Moritz Date: Fri, 15 Mar 2024 15:04:38 +0000 (+0100) Subject: test: Fixed `AbstractConfigurationIT#testPutMessageInNewChatRoom()` X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=c0797810e3222618e6d7be31beba34fc52c009ed;p=demos%2Fkafka%2Fchat test: Fixed `AbstractConfigurationIT#testPutMessageInNewChatRoom()` * In the case, that the request that adds a messag failed, the longly process of creating a new chat-room, until it has the correct shard, was unnecessarily repeated. * Different workarounds have to be applied at the same time for the two implementations: ** Because `ShardedChatHomeService` only throws a `ShardNotOwnedException`, if the shard, that is picked by the strategy is not owned, the result is retried until the requests succeeds by chance. ** The `KafkaChatHomeService` always creates the chat-room in the partition, that is derived from the randomly picked id. Hence, the loop, that is only left, if the randomly picked partition matches the partition `2`, that the test-instance owns. ** Since `SimpleChatHomeService` does not know the concept of sharding at all, the loop is also left, if no shard is set (shard is ``null``). --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java index c639f70a..452e4244 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java @@ -188,34 +188,39 @@ public abstract class AbstractConfigurationIT @DisplayName("A message can be put into a newly created chat-room") void testPutMessageInNewChatRoom() throws IOException { + ChatRoomInfoTo chatRoomInfo; + do + { + // The first request creates a new chat-room + // It must be repeated, until a chat-room was created, + // that is owned by the instance + chatRoomInfo = webTestClient + .post() + .uri("http://localhost:{port}/create", port) + .contentType(MediaType.TEXT_PLAIN) + .bodyValue("bar") + .accept(MediaType.APPLICATION_JSON) + .exchange() + .returnResult(ChatRoomInfoTo.class) + .getResponseBody() + .retry(30) + .blockFirst(); + } + while(!(chatRoomInfo.getShard() == null || chatRoomInfo.getShard().intValue() == 2)); + + UUID chatRoomId = chatRoomInfo.getId(); + Awaitility .await() .atMost(Duration.ofSeconds(15)) .untilAsserted(() -> { - // The first request creates a new chat-room - ChatRoomInfoTo chatRoomInfo = webTestClient - .post() - .uri("http://localhost:{port}/create", port) - .contentType(MediaType.TEXT_PLAIN) - .bodyValue("bar") - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .returnResult(ChatRoomInfoTo.class) - .getResponseBody() - .blockFirst(); - - // It must be repeated, until a chat-room was created, - // that is owned by the instance - assertThat(chatRoomInfo.getShard()).isIn(2, null); - webTestClient .put() .uri( "http://localhost:{port}/{chatRoomId}/nerd/7", port, - chatRoomInfo.getId()) + chatRoomId) .contentType(MediaType.TEXT_PLAIN) .accept(MediaType.APPLICATION_JSON) .bodyValue("Hello world!")