From 7a78afdfa6ac01593d4c6bd5eb8142d203a00ef1 Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Tue, 27 Jun 2017 10:00:49 +0100 Subject: [PATCH 1/2] Make Server Tests more careful to ensure they pass Increase time for server start to 5 minutes, and avoid NullPointerException if client does not connect --- .../bmwcarit/barefoot/matcher/ServerTest.java | 8 +++++--- .../barefoot/tracker/TrackerServerTest.java | 20 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/bmwcarit/barefoot/matcher/ServerTest.java b/src/test/java/com/bmwcarit/barefoot/matcher/ServerTest.java index a1d6827d..baafd20b 100644 --- a/src/test/java/com/bmwcarit/barefoot/matcher/ServerTest.java +++ b/src/test/java/com/bmwcarit/barefoot/matcher/ServerTest.java @@ -58,8 +58,8 @@ public void stop() { private void sendRequest(InetAddress host, int port, JSONArray samples) throws InterruptedException, IOException, JSONException { - int trials = 120; - int timeout = 500; + int trials = 300; + int timeout = 1000; Socket client = null; while (client == null || !client.isConnected()) { @@ -69,7 +69,9 @@ private void sendRequest(InetAddress host, int port, JSONArray samples) Thread.sleep(timeout); if (trials == 0) { - client.close(); + if (client != null) { + client.close(); + } throw new IOException(e.getMessage()); } else { trials -= 1; diff --git a/src/test/java/com/bmwcarit/barefoot/tracker/TrackerServerTest.java b/src/test/java/com/bmwcarit/barefoot/tracker/TrackerServerTest.java index 3f07ed18..e7d7663e 100644 --- a/src/test/java/com/bmwcarit/barefoot/tracker/TrackerServerTest.java +++ b/src/test/java/com/bmwcarit/barefoot/tracker/TrackerServerTest.java @@ -61,8 +61,8 @@ public void stop() { public void sendSample(InetAddress host, int port, JSONObject sample) throws InterruptedException, IOException { - int trials = 120; - int timeout = 500; + int trials = 300; + int timeout = 1000; Socket client = null; while (client == null || !client.isConnected()) { @@ -73,8 +73,10 @@ public void sendSample(InetAddress host, int port, JSONObject sample) if (trials == 0) { logger.error(e.getMessage()); - client.close(); - throw new IOException(); + if (client != null) { + client.close(); + } + throw new IOException(e.getMessage()); } else { trials -= 1; } @@ -92,8 +94,8 @@ public void sendSample(InetAddress host, int port, JSONObject sample) public MatcherKState requestState(InetAddress host, int port, String id) throws JSONException, InterruptedException, IOException { - int trials = 120; - int timeout = 500; + int trials = 300; + int timeout = 1000; Socket client = null; while (client == null || !client.isConnected()) { @@ -104,8 +106,10 @@ public MatcherKState requestState(InetAddress host, int port, String id) if (trials == 0) { logger.error(e.getMessage()); - client.close(); - throw new IOException(); + if (client != null) { + client.close(); + } + throw new IOException(e.getMessage()); } else { trials -= 1; } From a44bc941d05a29b4c8b5f36fcf0b8178d33aae06 Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Tue, 27 Jun 2017 11:38:55 +0100 Subject: [PATCH 2/2] Prevent KState remove from causing a StackOverflow --- .../com/bmwcarit/barefoot/markov/KState.java | 21 +++++++++++-------- .../bmwcarit/barefoot/markov/KStateTest.java | 16 ++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bmwcarit/barefoot/markov/KState.java b/src/main/java/com/bmwcarit/barefoot/markov/KState.java index 594e38a7..3a69e2f2 100755 --- a/src/main/java/com/bmwcarit/barefoot/markov/KState.java +++ b/src/main/java/com/bmwcarit/barefoot/markov/KState.java @@ -234,15 +234,18 @@ public void update(Set vector, S sample) { } protected void remove(C candidate, int index) { - Set vector = sequence.get(index).one(); - counters.remove(candidate); - vector.remove(candidate); - - C predecessor = candidate.predecessor(); - if (predecessor != null) { - counters.put(predecessor, counters.get(predecessor) - 1); - if (counters.get(predecessor) == 0) { - remove(predecessor, index - 1); + while (candidate != null) { + Set vector = sequence.get(index).one(); + counters.remove(candidate); + vector.remove(candidate); + candidate = candidate.predecessor(); + if (candidate != null) { + counters.put(candidate, counters.get(candidate) - 1); + if (counters.get(candidate) == 0) { + index -= 1; + } else { + candidate = null; + } } } } diff --git a/src/test/java/com/bmwcarit/barefoot/markov/KStateTest.java b/src/test/java/com/bmwcarit/barefoot/markov/KStateTest.java index 8bbf1381..cf4834ca 100644 --- a/src/test/java/com/bmwcarit/barefoot/markov/KStateTest.java +++ b/src/test/java/com/bmwcarit/barefoot/markov/KStateTest.java @@ -251,6 +251,22 @@ public void TestKState() { } } + @Test + public void TestKStateRemoveLongSequence() { + // KState remove code was originally recursive, but this could cause a + // stack overflow. This tests that removing a long sequence succeeds + // without causing an exception. + KState state = new KState<>(-1, -1); + MockElem candidate = null; + int i = 0; + for (i = 0; i < 10000; ++i) { + candidate = new MockElem(i, Math.log10(0.3), 0.3, candidate); + Set vector = new HashSet<>(Arrays.asList(candidate)); + state.update(vector, new Sample(i)); + } + state.remove(candidate, i - 1); + } + @Test public void TestTState() { Map elements = new HashMap<>();