Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public <E> void jsonWriteStream(Iterator<E> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.avaje.jex.jdk;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

public class AutoCloseIterator<E> implements Iterator<E>, AutoCloseable {

private final Iterator<E> it;
private volatile boolean closed;
private final AtomicBoolean closed = new AtomicBoolean(false);

public AutoCloseIterator(Iterator<E> it) {
this.it = it;
Expand All @@ -23,10 +24,10 @@ public E next() {

@Override
public void close() {
closed = true;
closed.set(true);
}

public boolean isClosed() {
return closed;
return closed.get();
}
}
12 changes: 7 additions & 5 deletions avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> afterAll = new AtomicReference<>();
static AtomicReference<String> afterTwo = new AtomicReference<>();
static final TestPair pair = init();
static final AtomicReference<String> afterAll = new AtomicReference<>();
static final AtomicReference<String> afterTwo = new AtomicReference<>();

static TestPair init() {
final Jex app =
Expand All @@ -33,15 +35,14 @@ 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"))
.filter(
(ctx, chain) -> {
chain.proceed();
if (ctx.url().contains("/two/")) {

afterTwo.set("set");
}
})
Expand Down Expand Up @@ -74,6 +75,7 @@ void get() {

clearAfter();
res = pair.request().path("two").GET().asString();
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10));
assertHasBeforeAfterAll(res);
assertNoBeforeAfterTwo(res);
}
Expand Down
3 changes: 3 additions & 0 deletions avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down