diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/CoreServiceManager.java b/avaje-jex/src/main/java/io/avaje/jex/core/CoreServiceManager.java index 503ef50b..c57f8048 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/core/CoreServiceManager.java +++ b/avaje-jex/src/main/java/io/avaje/jex/core/CoreServiceManager.java @@ -76,8 +76,8 @@ public void jsonWriteStream(Iterator iterator, OutputStream os) { @Override public void maybeClose(Object iterator) { if (iterator instanceof AutoCloseable closeable) { - try (closeable) { - // nothing + try { + closeable.close(); } catch (Exception e) { throw new RuntimeException("Error closing iterator " + iterator, e); } diff --git a/avaje-jex/src/test/java/io/avaje/jex/jdk/AutoCloseIterator.java b/avaje-jex/src/test/java/io/avaje/jex/jdk/AutoCloseIterator.java index 01528d66..74541670 100644 --- a/avaje-jex/src/test/java/io/avaje/jex/jdk/AutoCloseIterator.java +++ b/avaje-jex/src/test/java/io/avaje/jex/jdk/AutoCloseIterator.java @@ -1,11 +1,12 @@ package io.avaje.jex.jdk; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicBoolean; public class AutoCloseIterator implements Iterator, AutoCloseable { private final Iterator it; - private volatile boolean closed; + private final AtomicBoolean closed = new AtomicBoolean(false); public AutoCloseIterator(Iterator it) { this.it = it; @@ -23,10 +24,10 @@ public E next() { @Override public void close() { - closed = true; + closed.set(true); } public boolean isClosed() { - return closed; + return closed.get(); } } diff --git a/avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java b/avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java index afce6f70..74d82a70 100644 --- a/avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java +++ b/avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java @@ -6,15 +6,17 @@ import java.net.http.HttpHeaders; import java.net.http.HttpResponse; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.LockSupport; import static org.assertj.core.api.Assertions.assertThat; class FilterTest { - static TestPair pair = init(); - static AtomicReference afterAll = new AtomicReference<>(); - static AtomicReference afterTwo = new AtomicReference<>(); + static final TestPair pair = init(); + static final AtomicReference afterAll = new AtomicReference<>(); + static final AtomicReference afterTwo = new AtomicReference<>(); static TestPair init() { final Jex app = @@ -33,7 +35,7 @@ static TestPair init() { if (ctx.url().contains("/two/")) { ctx.header("before-two", "set"); } - ctx.jdkExchange().getRequestURI().getPath(); + // ctx.jdkExchange().getRequestURI().getPath(); chain.proceed(); }) .after(ctx -> afterAll.set("set")) @@ -41,7 +43,6 @@ static TestPair init() { (ctx, chain) -> { chain.proceed(); if (ctx.url().contains("/two/")) { - afterTwo.set("set"); } }) @@ -74,6 +75,7 @@ void get() { clearAfter(); res = pair.request().path("two").GET().asString(); + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10)); assertHasBeforeAfterAll(res); assertNoBeforeAfterTwo(res); } diff --git a/avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java b/avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java index e52c8f14..1ab2dcab 100644 --- a/avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java +++ b/avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java @@ -7,6 +7,8 @@ import java.net.http.HttpHeaders; import java.net.http.HttpResponse; import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; import java.util.stream.Stream; import org.junit.jupiter.api.AfterAll; @@ -69,6 +71,7 @@ void stream_viaIterator() { // expect client gets the expected stream of beans assertCollectedStream(beanStream); // assert AutoCloseable iterator on the server-side was closed + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10)); assertThat(ITERATOR.isClosed()).isTrue(); }