From 346946acf2edeb7fd35e4887d49d9e7d90cf8893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veli=20Tasal=C4=B1?= Date: Sat, 23 Sep 2023 17:33:32 +0300 Subject: [PATCH] chore(multichannel): Add tests for multithreaded operation --- .../coolsocket/core/PlainTransactionTest.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/src/test/java/org/monora/coolsocket/core/PlainTransactionTest.java b/src/test/java/org/monora/coolsocket/core/PlainTransactionTest.java index 67ec15d..22fed64 100644 --- a/src/test/java/org/monora/coolsocket/core/PlainTransactionTest.java +++ b/src/test/java/org/monora/coolsocket/core/PlainTransactionTest.java @@ -1,6 +1,7 @@ package org.monora.coolsocket.core; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; @@ -11,6 +12,8 @@ import java.io.IOException; import java.net.SocketException; +import java.util.ArrayList; +import java.util.List; public class PlainTransactionTest { @@ -266,4 +269,122 @@ public void onConnected(@NotNull ActiveConnection activeConnection) coolSocket.stop(); } } + + @Test + public void handlesMultichannelTransaction() throws IOException, InterruptedException + { + String message1 = "Hello, World!"; + String message2 = "FooBar"; + String message3 = "FuzzBuzz"; + CoolSocket coolSocket = new DefaultCoolSocket() + { + @Override + public void onConnected(@NotNull ActiveConnection activeConnection) + { + activeConnection.setMultichannel(true); + try { + // Put the read and write in the same order on both the reader and the sender. + activeConnection.reply(message1); + activeConnection.reply(message2); + activeConnection.reply(message3); + activeConnection.receive(); + } catch (IOException ignored) { + } + } + }; + + coolSocket.start(); + + try (ActiveConnection activeConnection = Connections.connect()) { + activeConnection.setMultichannel(true); + + activeConnection.reply(message1); + String receivedMessage1 = activeConnection.receive().getAsString(); + String receivedMessage2 = activeConnection.receive().getAsString(); + String receivedMessage3 = activeConnection.receive().getAsString(); + + Assert.assertEquals("The first message should match after the disorderly transaction", message1, receivedMessage1); + Assert.assertEquals("The second message should match after the disorderly transaction", message2, receivedMessage2); + Assert.assertEquals("The third message should match after the disorderly transaction", message3, receivedMessage3); + } finally { + coolSocket.stop(); + } + } + + @Test + public void handlesMultithreadedMultichannelCommunication() throws IOException, InterruptedException + { + List messages = new ArrayList() + {{ + add("hello"); + add("world"); + add("fuzz"); + add("buzz"); + add("quit"); + }}; + String lastMessage = messages.get(messages.size() - 1); + + CoolSocket coolSocket = new DefaultCoolSocket() + { + @Override + public void onConnected(@NotNull ActiveConnection activeConnection) + { + Thread messageReceiverThread = new Thread(() -> { + try { + String message; + do { + message = activeConnection.receive().getAsString(); + } while (!lastMessage.equals(message)); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + activeConnection.setMultichannel(true); + try { + messageReceiverThread.start(); + for (String message : messages) { + activeConnection.reply(message); + } + messageReceiverThread.join(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + activeConnection.setMultichannel(false); + } + } + }; + + coolSocket.start(); + + try (ActiveConnection activeConnection = Connections.connect()) { + List receivedMessages = new ArrayList<>(); + Thread messageReceiverThread = new Thread(() -> { + try { + String message; + do { + message = activeConnection.receive().getAsString(); + receivedMessages.add(message); + } while (!lastMessage.equals(message)); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + activeConnection.setMultichannel(true); + try { + messageReceiverThread.start(); + for (String message : messages) { + activeConnection.reply(message); + } + messageReceiverThread.join(); + } finally { + activeConnection.setMultichannel(false); + } + + Assert.assertArrayEquals("Messages should match", messages.toArray(), receivedMessages.toArray()); + } finally { + coolSocket.stop(); + } + } }