Skip to content

Commit

Permalink
Fix AsyncContext to be completed on AsyncListener.onError()
Browse files Browse the repository at this point in the history
Closes gh-31541
  • Loading branch information
injae-kim committed Jan 5, 2024
1 parent 534d322 commit bdccd90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public void onTimeout(AsyncEvent event) throws IOException {

@Override
public void onError(AsyncEvent event) throws IOException {
if (this.asyncContext != null) {
this.asyncContext.complete();
}
this.exceptionHandlers.forEach(consumer -> consumer.accept(event.getThrowable()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.springframework.web.context.request.async;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import jakarta.servlet.AsyncEvent;
import jakarta.servlet.AsyncListener;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -124,6 +127,36 @@ public void onErrorHandler() throws Exception {
verify(errorHandler).accept(e);
}

@Test
public void onErrorShouldCompleteAsyncContext() throws Exception {
this.asyncRequest.startAsync();
AtomicInteger completeCounter = new AtomicInteger();
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
context.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent asyncEvent) throws IOException {
completeCounter.incrementAndGet();
}

@Override
public void onTimeout(AsyncEvent asyncEvent) throws IOException {

}

@Override
public void onError(AsyncEvent asyncEvent) throws IOException {

}

@Override
public void onStartAsync(AsyncEvent asyncEvent) throws IOException {

}
});
this.asyncRequest.onError(new AsyncEvent(new MockAsyncContext(this.request, this.response), new Exception()));
assertThat(completeCounter.get()).isOne();
}

@Test
public void setTimeoutDuringConcurrentHandling() {
this.asyncRequest.startAsync();
Expand Down

0 comments on commit bdccd90

Please sign in to comment.