This is an automated email from the ASF dual-hosted git repository. jbonofre pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git
The following commit(s) were added to refs/heads/master by this push: new 1b1524e Fix socket appender test random failure new 479a8ec Merge pull request #193 from jbonofre/SOCKET_APPENDER_TEST_FIX 1b1524e is described below commit 1b1524ed21535a78f98b101d3f5e9dcae4c0e6fe Author: jbonofre <jbono...@apache.org> AuthorDate: Wed Sep 2 14:46:03 2020 +0200 Fix socket appender test random failure --- appender/socket/pom.xml | 5 ++ .../appender/socket/SocketAppenderTest.java | 61 ++++------------------ 2 files changed, 15 insertions(+), 51 deletions(-) diff --git a/appender/socket/pom.xml b/appender/socket/pom.xml index bd32c1d..2297f16 100644 --- a/appender/socket/pom.xml +++ b/appender/socket/pom.xml @@ -49,6 +49,11 @@ <artifactId>org.apache.karaf.decanter.marshaller.csv</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/appender/socket/src/test/java/org/apache/karaf/decanter/appender/socket/SocketAppenderTest.java b/appender/socket/src/test/java/org/apache/karaf/decanter/appender/socket/SocketAppenderTest.java index 0c59b59..8f8bb8b 100644 --- a/appender/socket/src/test/java/org/apache/karaf/decanter/appender/socket/SocketAppenderTest.java +++ b/appender/socket/src/test/java/org/apache/karaf/decanter/appender/socket/SocketAppenderTest.java @@ -50,15 +50,17 @@ public class SocketAppenderTest { public void run() { try { ServerSocket server = new ServerSocket(44445); - while (true) { - Socket socket = server.accept(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { - String line; - while ((line = reader.readLine()) != null) { - received.add(line); + while (received.size() < 1) { + try (Socket socket = server.accept()) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + received.add(line); + } } } } + server.close(); } catch (Exception e) { e.printStackTrace(); } @@ -79,7 +81,7 @@ public class SocketAppenderTest { Assert.assertEquals(1, received.size()); Assert.assertEquals("type=test,first=1,event.topics=test", received.get(0)); - serverThread.interrupt(); + appender.deactivate(); } @Test @@ -98,50 +100,7 @@ public class SocketAppenderTest { // expected } - // no exception there as the socket is bound when sending message - - final List<String> received = new ArrayList<>(); - - Runnable server = new Runnable() { - @Override - public void run() { - try { - ServerSocket server = new ServerSocket(44444); - while (true) { - Socket socket = server.accept(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { - String line; - while ((line = reader.readLine()) != null) { - received.add(line); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - }; - Thread serverThread = new Thread(server); - serverThread.start(); - - // gives time to server thread to start - Thread.sleep(500); - - appender.activate(config); - - Map<String, String> data = new HashMap<>(); - data.put("type", "test"); - data.put("first", "1"); - appender.handleEvent(new Event("test", data)); - - while (received.size() != 1) { - Thread.sleep(200); - } - - Assert.assertEquals(1, received.size()); - Assert.assertEquals("type=test,first=1,event.topics=test", received.get(0)); - - serverThread.interrupt(); + appender.deactivate(); } }