test: Fixed `AbstractConfigurationIT#testPutMessageInNewChatRoom()`
authorKai Moritz <kai@juplo.de>
Fri, 15 Mar 2024 15:04:38 +0000 (16:04 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 15 Mar 2024 20:55:38 +0000 (21:55 +0100)
* 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``).

src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java

index c639f70..452e424 100644 (file)
@@ -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!")