From 82529fc58dd073e7de5b171d47d553123b4aef1d Mon Sep 17 00:00:00 2001 From: "Peter Fichtner (pfichtner)" Date: Sun, 17 Sep 2023 15:30:28 +0200 Subject: [PATCH] await stopped --- .../ase/fourwins/udp/server/UdpServer.java | 9 ++- .../udp/server/UdpServerRealTournamentIT.java | 58 +++++++++++-------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java b/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java index d3dad7f..c9bdd48 100644 --- a/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java +++ b/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java @@ -46,7 +46,7 @@ public class UdpServer { public static final int MAX_CLIENT_NAME_LENGTH = 30; public static final int UNREGISTER_AFTER_N_TIMEOUTS = 10; - + @Setter private int port = 4446; @@ -222,6 +222,13 @@ public void stop() { keepSeasonRunning = false; } + public void stopAndAwaitSocketClosed() throws InterruptedException { + stop(); + while (!socket.isClosed()) { + TimeUnit.MILLISECONDS.sleep(100); + } + } + private void delay() { if (delayMillis > 0) { System.out.println("Delaying start for " + delayMillis + " millis"); diff --git a/udp/src/test/java/org/ase/fourwins/udp/server/UdpServerRealTournamentIT.java b/udp/src/test/java/org/ase/fourwins/udp/server/UdpServerRealTournamentIT.java index c297b00..fcf3093 100644 --- a/udp/src/test/java/org/ase/fourwins/udp/server/UdpServerRealTournamentIT.java +++ b/udp/src/test/java/org/ase/fourwins/udp/server/UdpServerRealTournamentIT.java @@ -68,14 +68,18 @@ private static void runInBackground(Runnable runnable) { @Test void canReRegister() throws IOException, InterruptedException { - udpServerInBackground(); - assertWelcomed(playingClient("1", 0)); - assertWelcomed(playingClient("1", 0)); + UdpServer udpServer = udpServerInBackground(); + try { + assertWelcomed(playingClient("1", 0)); + assertWelcomed(playingClient("1", 0)); + } finally { + udpServer.stopAndAwaitSocketClosed(); + } } @Test void canPlay_2() throws IOException, InterruptedException { - udpServerInBackground(); + UdpServer udpServer = udpServerInBackground(); SysoutTournamentListener scoreListener = new SysoutTournamentListener(); tournament.addTournamentListener(scoreListener); GameStateCollector stateListener = new GameStateCollector(); @@ -83,12 +87,15 @@ void canPlay_2() throws IOException, InterruptedException { DummyClient client1 = playingClient("1", 0); DummyClient client2 = playingClient("2", 1); + try { + assertWelcomed(client1); + assertWelcomed(client2); - assertWelcomed(client1); - assertWelcomed(client2); - - /// ...let it run for a while - SECONDS.sleep(5); + /// ...let it run for a while + SECONDS.sleep(5); + } finally { + udpServer.stopAndAwaitSocketClosed(); + } ScoreSheet scoreSheet = scoreListener.getScoreSheet(); Double score1 = scoreSheet.scoreOf(client1.getName()); @@ -134,23 +141,26 @@ private void assertWelcomed(DummyClient client) throws InterruptedException { @Disabled void canPlay_Multi() throws IOException, InterruptedException { // TEST fails since is canceled after timeout has reached ;-) - udpServerInBackground(); - tournament.addTournamentListener(new SysoutTournamentListener()); - IntStream.range(0, 10).forEach(i -> { - try { - playingClient(String.valueOf(i), i % tournament.getBoardInfo().getColumns()); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - uuidFaker(); - sometimesNoResponse(); - - /// ...let it run for a long while - TimeUnit.MINUTES.sleep(60); + UdpServer udpServer = udpServerInBackground(); + try { + tournament.addTournamentListener(new SysoutTournamentListener()); + IntStream.range(0, 10).forEach(i -> { + try { + playingClient(String.valueOf(i), i % tournament.getBoardInfo().getColumns()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + uuidFaker(); + sometimesNoResponse(); - fail("add more assertions"); + /// ...let it run for a long while + TimeUnit.MINUTES.sleep(60); + fail("add more assertions"); + } finally { + udpServer.stopAndAwaitSocketClosed(); + } } private PlayingClient uuidFaker() throws IOException {