From c314b0c6d4c444de3525f5f2f8f54e633532a31c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 14 Oct 2020 09:40:59 +0200 Subject: [PATCH] [backport] 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 | 5 +++++ .../stklcode/pubtrans/ura/reader/AsyncUraTripReader.java | 2 ++ .../pubtrans/ura/reader/AsyncUraTripReaderTest.java | 9 ++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1379606..4d8d086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. +## unreleased +### Fixed +* Allow reopening an `AsyncUraTripReader` without raising an exception (#13) + + ## 1.3.0 - 2019-12-04 ### Security * Updated dependencies 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 1a3bc13..d87cc32 100644 --- a/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java +++ b/src/main/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReader.java @@ -134,6 +134,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 0f62da0..cb709dd 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java @@ -41,6 +41,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; /** @@ -122,7 +124,7 @@ public class AsyncUraTripReaderTest { tr = new AsyncUraTripReader( new URL(httpMock.baseUrl() + "/interfaces/ura/stream_V2"), - Collections.singletonList(trips::add) + trips::add ); // Open the reader. @@ -146,6 +148,11 @@ 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(); } /**