From d18d1dbbe519a17e8c334947492fb7e7fb1aa915 Mon Sep 17 00:00:00 2001 From: Pierre-Alexandre Date: Sun, 31 Dec 2023 13:21:03 +0100 Subject: [PATCH] [#122] Tests added. Code refactored. --- .../b3dgs/lionengine/network/UtilNetwork.java | 22 ++ .../lionengine/network/NetworkTypeTest.java | 58 ++++ .../lionengine/network/ServerClientTest.java | 302 +++++++++++++++++- 3 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 lionengine-core/src/test/java/com/b3dgs/lionengine/network/NetworkTypeTest.java diff --git a/lionengine-core/src/main/java/com/b3dgs/lionengine/network/UtilNetwork.java b/lionengine-core/src/main/java/com/b3dgs/lionengine/network/UtilNetwork.java index 44ba8a526..872cddebb 100644 --- a/lionengine-core/src/main/java/com/b3dgs/lionengine/network/UtilNetwork.java +++ b/lionengine-core/src/main/java/com/b3dgs/lionengine/network/UtilNetwork.java @@ -25,6 +25,7 @@ import com.b3dgs.lionengine.Constant; import com.b3dgs.lionengine.LionEngineException; import com.b3dgs.lionengine.UtilConversion; +import com.b3dgs.lionengine.network.client.InfoGet; /** * Network utility. @@ -110,6 +111,27 @@ public static ByteBuffer getBuffer(DatagramPacket packet) return buffer; } + /** + * Get the server info. + * + * @param ip The server ip. + * @param port The server port. + * @return The info data. + * @throws IOException If error. + */ + public static ByteBuffer getInfo(String ip, int port) throws IOException + { + try (DatagramSocket socket = new DatagramSocket()) + { + final ByteBuffer buffer = ByteBuffer.allocate(1); + buffer.put(UtilNetwork.toByte(MessageType.INFO)); + final ByteBuffer send = UtilNetwork.createPacket(buffer); + socket.send(new DatagramPacket(send.array(), send.capacity(), InetAddress.getByName(ip), port)); + + return InfoGet.decode(socket); + } + } + /** * Create full packet from data with header. * diff --git a/lionengine-core/src/test/java/com/b3dgs/lionengine/network/NetworkTypeTest.java b/lionengine-core/src/test/java/com/b3dgs/lionengine/network/NetworkTypeTest.java new file mode 100644 index 000000000..843472d20 --- /dev/null +++ b/lionengine-core/src/test/java/com/b3dgs/lionengine/network/NetworkTypeTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013-2023 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.b3dgs.lionengine.network; + +import static com.b3dgs.lionengine.UtilAssert.assertEquals; + +import org.junit.jupiter.api.Test; + +import com.b3dgs.lionengine.UtilTests; + +/** + * Test {@link NetworkType}. + */ +final class NetworkTypeTest +{ + /** + * Test the enum. + * + * @throws Exception If error. + */ + @Test + void testEnum() throws Exception + { + UtilTests.testEnum(NetworkType.class); + } + + /** + * Test from. + */ + @Test + void testFrom() + { + assertEquals(NetworkType.SERVER, NetworkType.from("server")); + assertEquals(NetworkType.SERVER, NetworkType.from("SERVER")); + + assertEquals(NetworkType.CLIENT, NetworkType.from("client")); + assertEquals(NetworkType.CLIENT, NetworkType.from("CLIENT")); + + assertEquals(NetworkType.NONE, NetworkType.from(null)); + assertEquals(NetworkType.NONE, NetworkType.from("")); + assertEquals(NetworkType.NONE, NetworkType.from("none")); + assertEquals(NetworkType.NONE, NetworkType.from("void")); + } +} diff --git a/lionengine-core/src/test/java/com/b3dgs/lionengine/network/ServerClientTest.java b/lionengine-core/src/test/java/com/b3dgs/lionengine/network/ServerClientTest.java index f3c9aa35b..b7899ca6f 100644 --- a/lionengine-core/src/test/java/com/b3dgs/lionengine/network/ServerClientTest.java +++ b/lionengine-core/src/test/java/com/b3dgs/lionengine/network/ServerClientTest.java @@ -18,14 +18,18 @@ import static com.b3dgs.lionengine.UtilAssert.assertEquals; import static com.b3dgs.lionengine.UtilAssert.assertTimeout; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.Test; import com.b3dgs.lionengine.UtilTests; +import com.b3dgs.lionengine.network.client.ClientListener; import com.b3dgs.lionengine.network.client.ClientUdp; import com.b3dgs.lionengine.network.server.ServerListener; import com.b3dgs.lionengine.network.server.ServerUdp; @@ -35,6 +39,111 @@ */ final class ServerClientTest { + /** + * Test server start stop. + * + * @throws IOException If error. + */ + @Test + public void testDefault() throws IOException + { + final ChannelBuffer channel = new ChannelBuffer(); + final ServerUdp server = new ServerUdp(channel); + final AtomicBoolean started = new AtomicBoolean(); + server.addListener(new ServerListener() + { + @Override + public void notifyServerStarted(String ip, int port) + { + started.set(true); + } + }); + server.start("127.0.0.1", 1000); + + assertTimeout(1000L, () -> + { + while (!started.get()) + { + UtilTests.pause(100L); + } + }); + + final ClientUdp client = new ClientUdp(channel); + final AtomicBoolean connected = new AtomicBoolean(); + client.addListener(new ClientListener() + { + @Override + public void notifyConnected(String ip, int port, Integer id) + { + connected.set(true); + } + }); + client.connect("127.0.0.1", 1000); + client.setName("name"); + + assertTimeout(1000L, () -> + { + while (!connected.get()) + { + UtilTests.pause(100L); + } + }); + + client.disconnect(); + + assertTimeout(1000L, () -> + { + while (server.getClients() > 0) + { + UtilTests.pause(100L); + } + }); + + server.stop(); + } + + /** + * Test server with info. + * + * @throws IOException If error. + */ + @Test + public void testInfo() throws IOException + { + final ChannelBuffer channel = new ChannelBuffer(); + final ServerUdp server = new ServerUdp(channel); + final AtomicBoolean started = new AtomicBoolean(); + server.setInfoSupplier(() -> + { + final ByteBuffer buffer = ByteBuffer.allocate(1); + buffer.put((byte) 1); + return buffer; + }); + server.addListener(new ServerListener() + { + @Override + public void notifyServerStarted(String ip, int port) + { + started.set(true); + } + }); + server.start("127.0.0.1", 1000); + + assertTimeout(1000L, () -> + { + while (!started.get()) + { + UtilTests.pause(100L); + } + }); + + final ByteBuffer info = UtilNetwork.getInfo("127.0.0.1", 1000); + + assertEquals(1, info.get()); + + server.stop(); + } + /** * Test client connect to server. * @@ -92,6 +201,22 @@ public void notifyServerStopped() assertEquals(1000, startedPort.get()); final ClientUdp client = new ClientUdp(channel); + final AtomicReference clientId = new AtomicReference<>(); + final AtomicReference other = new AtomicReference<>(); + client.addListener(new ClientListener() + { + @Override + public void notifyConnected(String ip, int port, Integer id) + { + clientId.set(id); + } + + @Override + public void notifyClientConnected(Integer id) + { + other.set(id); + } + }); client.connect("127.0.0.1", 1000); client.setName("name"); @@ -106,8 +231,59 @@ public void notifyServerStopped() assertEquals(1, server.getClients()); + final ClientUdp client2 = new ClientUdp(channel); + final AtomicReference disconnected = new AtomicReference<>(); + client2.addListener(new ClientListener() + { + @Override + public void notifyConnected(String ip, int port, Integer id) + { + // Skip + } + + @Override + public void notifyClientDisconnected(Integer id) + { + disconnected.set(id); + } + }); + client2.connect("127.0.0.1", 1000); + client2.setName("name2"); + + assertTimeout(1000L, () -> + { + while (server.getClients() < 2) + { + UtilTests.pause(100L); + } + }); + + assertTimeout(1000L, () -> + { + while (!client2.getClientId().equals(other.get())) + { + UtilTests.pause(100L); + } + }); + client.disconnect(); + assertTimeout(1000L, () -> + { + while (!clientId.get().equals(disconnected.get())) + { + UtilTests.pause(100L); + } + }); + + assertTimeout(1000L, () -> + { + while (server.getClients() != 1) + { + UtilTests.pause(100L); + } + }); + assertTimeout(1000L, () -> { while (clientIp.get() != null) @@ -116,7 +292,7 @@ public void notifyServerStopped() } }); - assertEquals(0, server.getClients()); + assertEquals(1, server.getClients()); server.stop(); @@ -135,4 +311,128 @@ public void notifyServerStopped() } }); } + + /** + * Test ping. + * + * @throws IOException If error. + */ + @Test + public void testPing() throws IOException + { + final ChannelBuffer channel = new ChannelBuffer(); + final ServerUdp server = new ServerUdp(channel); + final AtomicBoolean started = new AtomicBoolean(); + server.addListener(new ServerListener() + { + @Override + public void notifyServerStarted(String ip, int port) + { + started.set(true); + } + + @Override + public void notifyServerStopped() + { + started.set(false); + } + }); + final ClientUdp client = new ClientUdp(channel); + final AtomicReference connected = new AtomicReference<>(); + client.addListener(new ClientListener() + { + @Override + public void notifyConnected(String ip, int port, Integer id) + { + connected.set(id); + } + }); + + server.start("127.0.0.1", 1000); + + assertTimeout(1000, () -> + { + while (!started.get()) + { + UtilTests.pause(100L); + } + }); + + client.connect("127.0.0.1", 1000); + + assertTimeout(1000, () -> + { + while (connected.get() == null) + { + UtilTests.pause(100L); + } + }); + + assertTrue(client.ping() > -1); + + server.stop(); + + assertTimeout(1000, () -> + { + while (started.get()) + { + UtilTests.pause(100L); + } + }); + } + + /** + * Test alive. + * + * @throws IOException If error. + */ + @Test + public void testAlive() throws IOException + { + final ChannelBuffer channel = new ChannelBuffer(); + final ServerUdp server = new ServerUdp(channel); + final AtomicBoolean started = new AtomicBoolean(); + server.addListener(new ServerListener() + { + @Override + public void notifyServerStarted(String ip, int port) + { + started.set(true); + } + }); + final ClientUdp client = new ClientUdp(channel); + final AtomicReference connected = new AtomicReference<>(); + client.addListener(new ClientListener() + { + @Override + public void notifyConnected(String ip, int port, Integer id) + { + connected.set(id); + } + }); + + server.start("127.0.0.1", 1000); + + assertTimeout(1000, () -> + { + while (!started.get()) + { + UtilTests.pause(100L); + } + }); + + client.connect("127.0.0.1", 1000); + + assertTimeout(1000, () -> + { + while (connected.get() == null) + { + UtilTests.pause(100L); + } + }); + + UtilTests.pause(5000); + + server.stop(); + } }