From b80da71014503768792ab25ed5c5ff6d4165f5b4 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 14 Oct 2020 09:40:59 +0200 Subject: [PATCH] remove future from trip reader after closing Future was not removed from the reader instance after close() has been called, so re-opening was not possible. Remove the instance after termination allows calling open() again. --- CHANGELOG.md | 3 +++ .../pubtrans/ura/reader/AsyncUraTripReader.java | 2 ++ .../ura/reader/AsyncUraTripReaderTest.java | 17 ++++++++++------- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index befca39..ad80afd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ All notable changes to this project will be documented in this file. * Client configuration with separate `UraClientConfiguration` class and builder * Client throws custom checked exception `UraClientException` instead of runtime exceptions on errors (#10) +### Fixed +* Allow reopening an `AsyncUraTripReader` without raising an exception (#12) + ## 1.3.0 - 2019-12-04 ### Security diff --git a/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java b/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java index 40a29cf..d88e53b 100644 --- a/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java +++ b/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java @@ -124,6 +124,8 @@ public class AsyncUraTripReader implements AutoCloseable { } catch (TimeoutException e) { // Task failed to finish within 1 second. future.cancel(true); + } finally { + future = null; } } diff --git a/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java b/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java index d0b76e2..92a6ae2 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java @@ -30,9 +30,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.net.URI; -import java.net.URL; import java.util.Collections; import java.util.Deque; import java.util.concurrent.ConcurrentLinkedDeque; @@ -42,6 +40,8 @@ import java.util.concurrent.atomic.AtomicInteger; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assumptions.assumeTrue; /** @@ -78,10 +78,9 @@ public class AsyncUraTripReaderTest { * as 1s is most likely more than enough time on any reasonable build system to parse some simple JSON lines. * * @throws InterruptedException Thread interrupted. - * @throws IOException Error reading or writing mocked data. */ @Test - public void readerTest() throws InterruptedException, IOException { + public void readerTest() throws InterruptedException { // Callback counter for some unhandy async mockery. final AtomicInteger counter = new AtomicInteger(0); @@ -123,7 +122,7 @@ public class AsyncUraTripReaderTest { tr = new AsyncUraTripReader( URI.create(httpMock.baseUrl() + "/interfaces/ura/stream_V2"), - Collections.singletonList(trips::add) + trips::add ); // Open the reader. @@ -147,16 +146,20 @@ public class AsyncUraTripReaderTest { assertThat("Unexpected number of v2 trips after all lines have been flushed", trips.size(), is(7)); assertThat("Unexpected number of v2 trips in list 2 after all lines have been flushed", trips2.size(), is(5)); assertThat("Same object should have been pushed to both lists", trips.containsAll(trips2)); + + // Opening the reader twice should raise an exception. + assertDoesNotThrow(tr::open, "Opening the reader after closing should not fail"); + assertThrows(IllegalStateException.class, tr::open, "Opening the reader twice should raise an exception"); + tr.close(); } /** * Test behavior if the stream is closed. * * @throws InterruptedException Thread interrupted. - * @throws IOException Error reading or writing mocked data. */ @Test - public void streamClosedTest() throws InterruptedException, IOException { + public void streamClosedTest() throws InterruptedException { // Callback counter for some unhandy async mockery. final AtomicInteger counter = new AtomicInteger(0);