|
| 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.internal.client; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import com.linecorp.armeria.client.ClientOptions; |
| 25 | +import com.linecorp.armeria.client.ClientRequestContext; |
| 26 | +import com.linecorp.armeria.client.Endpoint; |
| 27 | +import com.linecorp.armeria.client.RequestOptions; |
| 28 | +import com.linecorp.armeria.client.endpoint.DynamicEndpointGroup; |
| 29 | +import com.linecorp.armeria.client.endpoint.EndpointSelectionStrategy; |
| 30 | +import com.linecorp.armeria.common.HttpMethod; |
| 31 | +import com.linecorp.armeria.common.HttpRequest; |
| 32 | +import com.linecorp.armeria.common.RequestId; |
| 33 | +import com.linecorp.armeria.common.RequestTarget; |
| 34 | +import com.linecorp.armeria.common.SessionProtocol; |
| 35 | + |
| 36 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 37 | + |
| 38 | +class DerivedClientRequestContextClientTest { |
| 39 | + |
| 40 | + private final Endpoint endpointA = Endpoint.of("a.com", 8080); |
| 41 | + private final Endpoint endpointB = Endpoint.of("b.com", 8080); |
| 42 | + private final Endpoint endpointC = Endpoint.of("c.com", 8080); |
| 43 | + private SettableEndpointGroup group; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + group = new SettableEndpointGroup(); |
| 48 | + group.add(endpointA); |
| 49 | + group.add(endpointB); |
| 50 | + group.add(endpointC); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldAcquireNewEventLoopForNewEndpoint() { |
| 55 | + final HttpRequest request = HttpRequest.of(HttpMethod.GET, "/"); |
| 56 | + final DefaultClientRequestContext parent = new DefaultClientRequestContext( |
| 57 | + new SimpleMeterRegistry(), SessionProtocol.H2C, RequestId.random(), HttpMethod.GET, |
| 58 | + RequestTarget.forClient("/"), ClientOptions.of(), request, null, RequestOptions.of(), 0, 0); |
| 59 | + parent.init(group); |
| 60 | + assertThat(parent.endpoint()).isEqualTo(endpointA); |
| 61 | + final ClientRequestContext child = |
| 62 | + ClientUtil.newDerivedContext(parent, request, null, false); |
| 63 | + assertThat(child.endpoint()).isEqualTo(endpointB); |
| 64 | + assertThat(parent.endpoint()).isNotSameAs(child.endpoint()); |
| 65 | + assertThat(parent.eventLoop().withoutContext()).isNotSameAs(child.eventLoop().withoutContext()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void shouldAcquireSameEventLoopForSameEndpoint() { |
| 70 | + final HttpRequest request = HttpRequest.of(HttpMethod.GET, "/"); |
| 71 | + final DefaultClientRequestContext parent = new DefaultClientRequestContext( |
| 72 | + new SimpleMeterRegistry(), SessionProtocol.H2C, RequestId.random(), HttpMethod.GET, |
| 73 | + RequestTarget.forClient("/"), ClientOptions.of(), request, null, RequestOptions.of(), 0, 0); |
| 74 | + parent.init(group); |
| 75 | + assertThat(parent.endpoint()).isEqualTo(endpointA); |
| 76 | + final ClientRequestContext childA0 = |
| 77 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, true); |
| 78 | + assertThat(childA0.endpoint()).isEqualTo(endpointA); |
| 79 | + final ClientRequestContext childB0 = |
| 80 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, false); |
| 81 | + assertThat(childB0.endpoint()).isEqualTo(endpointB); |
| 82 | + final ClientRequestContext childC0 = |
| 83 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, false); |
| 84 | + assertThat(childC0.endpoint()).isEqualTo(endpointC); |
| 85 | + |
| 86 | + for (int i = 0; i < 3; i++) { |
| 87 | + final ClientRequestContext childA1 = |
| 88 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, false); |
| 89 | + assertThat(childA1.endpoint()).isEqualTo(endpointA); |
| 90 | + assertThat(childA1.eventLoop().withoutContext()).isSameAs(childA0.eventLoop().withoutContext()); |
| 91 | + final ClientRequestContext childB1 = |
| 92 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, false); |
| 93 | + assertThat(childB1.endpoint()).isEqualTo(endpointB); |
| 94 | + assertThat(childB1.eventLoop().withoutContext()).isSameAs(childB0.eventLoop().withoutContext()); |
| 95 | + final ClientRequestContext childC1 = |
| 96 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, false); |
| 97 | + assertThat(childC1.endpoint()).isEqualTo(endpointC); |
| 98 | + assertThat(childC1.eventLoop().withoutContext()).isSameAs(childC0.eventLoop().withoutContext()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void shouldNotAcquireNewEventLoopForInitialAttempt() { |
| 104 | + final HttpRequest request = HttpRequest.of(HttpMethod.GET, "/"); |
| 105 | + final DefaultClientRequestContext parent = new DefaultClientRequestContext( |
| 106 | + new SimpleMeterRegistry(), SessionProtocol.H2C, RequestId.random(), HttpMethod.GET, |
| 107 | + RequestTarget.forClient("/"), ClientOptions.of(), request, null, RequestOptions.of(), 0, 0); |
| 108 | + parent.init(group); |
| 109 | + assertThat(parent.endpoint()).isEqualTo(endpointA); |
| 110 | + final ClientRequestContext child = |
| 111 | + ClientUtil.newDerivedContext(parent, HttpRequest.of(HttpMethod.GET, "/"), null, true); |
| 112 | + assertThat(child.endpoint()).isEqualTo(endpointA); |
| 113 | + assertThat(parent.endpoint()).isSameAs(child.endpoint()); |
| 114 | + assertThat(parent.eventLoop().withoutContext()).isSameAs(child.eventLoop().withoutContext()); |
| 115 | + } |
| 116 | + |
| 117 | + private static class SettableEndpointGroup extends DynamicEndpointGroup { |
| 118 | + |
| 119 | + SettableEndpointGroup() { |
| 120 | + super(EndpointSelectionStrategy.roundRobin()); |
| 121 | + } |
| 122 | + |
| 123 | + void add(Endpoint endpoint) { |
| 124 | + addEndpoint(endpoint); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments