|
| 1 | +/* |
| 2 | + * Copyright 2023 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.armeria.server; |
| 18 | + |
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.awaitility.Awaitility.await; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.stream.IntStream; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 29 | + |
| 30 | +import com.spotify.futures.CompletableFutures; |
| 31 | + |
| 32 | +import com.linecorp.armeria.client.ClientFactory; |
| 33 | +import com.linecorp.armeria.client.CountingConnectionPoolListener; |
| 34 | +import com.linecorp.armeria.client.WebClient; |
| 35 | +import com.linecorp.armeria.common.AggregatedHttpResponse; |
| 36 | +import com.linecorp.armeria.common.HttpMethod; |
| 37 | +import com.linecorp.armeria.common.HttpObject; |
| 38 | +import com.linecorp.armeria.common.HttpRequest; |
| 39 | +import com.linecorp.armeria.common.HttpResponse; |
| 40 | +import com.linecorp.armeria.common.RequestHeaders; |
| 41 | +import com.linecorp.armeria.common.SessionProtocol; |
| 42 | +import com.linecorp.armeria.common.stream.StreamMessage; |
| 43 | +import com.linecorp.armeria.testing.junit5.server.ServerExtension; |
| 44 | + |
| 45 | +class MaxResetFramesTest { |
| 46 | + @RegisterExtension |
| 47 | + static final ServerExtension server = new ServerExtension() { |
| 48 | + @Override |
| 49 | + protected void configure(ServerBuilder sb) { |
| 50 | + sb.idleTimeoutMillis(0); |
| 51 | + sb.http2MaxResetFramesPerWindow(10, 60); |
| 52 | + sb.service("/", (ctx, req) -> { |
| 53 | + return HttpResponse.of(req.aggregate().thenApply(unused -> HttpResponse.of(200))); |
| 54 | + }); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + @Test |
| 59 | + void shouldCloseConnectionWhenExceedingMaxResetFrames() { |
| 60 | + final CountingConnectionPoolListener listener = new CountingConnectionPoolListener(); |
| 61 | + try (ClientFactory factory = ClientFactory.builder() |
| 62 | + .connectionPoolListener(listener) |
| 63 | + .idleTimeoutMillis(0) |
| 64 | + .build()) { |
| 65 | + final WebClient client = WebClient.builder(server.uri(SessionProtocol.H2C)) |
| 66 | + .factory(factory) |
| 67 | + .build(); |
| 68 | + final List<CompletableFuture<AggregatedHttpResponse>> futures = |
| 69 | + IntStream.range(0, 11) |
| 70 | + .mapToObj(unused -> HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/"), |
| 71 | + StreamMessage.of(InvalidHttpObject.INSTANCE))) |
| 72 | + .map(client::execute) |
| 73 | + .map(HttpResponse::aggregate) |
| 74 | + .collect(toImmutableList()); |
| 75 | + |
| 76 | + CompletableFutures.successfulAsList(futures, cause -> null).join(); |
| 77 | + assertThat(listener.opened()).isEqualTo(1); |
| 78 | + await().untilAsserted(() -> assertThat(listener.closed()).isEqualTo(1)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@link WebClient} resets a stream when it receives an invalid {@link HttpObject} from |
| 84 | + * {@link HttpRequest}. |
| 85 | + */ |
| 86 | + private enum InvalidHttpObject implements HttpObject { |
| 87 | + |
| 88 | + INSTANCE; |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isEndOfStream() { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments