-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…0729) * Reproducer for Writeable responses not being offloaded * Wrap all writeTo calls in io executor * backported from 4.4.x --------- Co-authored-by: yawkat <[email protected]>
- Loading branch information
Showing
2 changed files
with
137 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
http-server-netty/src/test/groovy/io/micronaut/http/body/WriteableOffloadSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.micronaut.http.body | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.io.Writable | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.MutableHttpResponse | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Filter | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.http.filter.HttpServerFilter | ||
import io.micronaut.http.filter.ServerFilterChain | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.reactivestreams.Publisher | ||
import reactor.core.publisher.Flux | ||
import spock.lang.Specification | ||
|
||
@MicronautTest | ||
@Property(name = "spec.name", value = "WriteableOffloadSpec") | ||
class WriteableOffloadSpec extends Specification { | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient client | ||
|
||
void "writeable is offloaded"() { | ||
when: | ||
def result = client.toBlocking().retrieve("/writeable") | ||
|
||
then: | ||
result.startsWith(expectedThreadNamePrefix) | ||
result.endsWith("Hello") | ||
} | ||
|
||
String getExpectedThreadNamePrefix() { | ||
Runtime.version().feature() > 17 ? 'virtual-executor' : 'io-executor' | ||
} | ||
|
||
@SuppressWarnings('unused') | ||
@Requires(property = "spec.name", value = "WriteableOffloadSpec") | ||
@Filter(Filter.MATCH_ALL_PATTERN) | ||
static class WriteableOffloadFilter implements HttpServerFilter { | ||
|
||
@Override | ||
Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) { | ||
return Flux.from(chain.proceed(request)) | ||
.map { response -> | ||
response.contentType(MediaType.TEXT_PLAIN) | ||
response.body(new ThreadWriteable(response.body())) | ||
} | ||
} | ||
} | ||
|
||
@Requires(property = "spec.name", value = "WriteableOffloadSpec") | ||
@Controller | ||
static class WriteableOffloadController { | ||
|
||
@Get("/writeable") | ||
String get() { | ||
"Hello" | ||
} | ||
} | ||
|
||
|
||
static class ThreadWriteable<B> implements Writable { | ||
|
||
private final B body; | ||
|
||
ThreadWriteable(B body) { | ||
this.body = body; | ||
} | ||
|
||
@Override | ||
void writeTo(Writer out) throws IOException { | ||
out.write(Thread.currentThread().getName()) | ||
out.write(" ") | ||
out.write(body.toString()) | ||
} | ||
} | ||
} |