|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Michael Zhang <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.devh.boot.grpc.test.setup; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.springframework.boot.test.context.SpringBootTest; |
| 26 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 27 | +import org.springframework.test.annotation.DirtiesContext; |
| 28 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 29 | + |
| 30 | +import io.grpc.Channel; |
| 31 | +import io.grpc.ConnectivityState; |
| 32 | +import io.grpc.ManagedChannel; |
| 33 | +import lombok.extern.slf4j.Slf4j; |
| 34 | +import net.devh.boot.grpc.client.config.GrpcChannelProperties; |
| 35 | +import net.devh.boot.grpc.client.inject.GrpcClient; |
| 36 | +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; |
| 37 | +import net.devh.boot.grpc.test.config.ServiceConfiguration; |
| 38 | + |
| 39 | +/** |
| 40 | + * These tests check the property {@link GrpcChannelProperties#getImmediateConnectTimeout()}. They check for |
| 41 | + * backwards-compatibility when this property didn't existed and various cases when it's enabled (for successful |
| 42 | + * connection and failed one). |
| 43 | + */ |
| 44 | +public class ImmediateConnectTests { |
| 45 | + |
| 46 | + @Slf4j |
| 47 | + @SpringBootTest(properties = { |
| 48 | + "grpc.client.GLOBAL.address=localhost:9090", |
| 49 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 50 | + "grpc.client.GLOBAL.immediateConnectTimeout=10s", |
| 51 | + }) |
| 52 | + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) |
| 53 | + @DirtiesContext |
| 54 | + static class ImmediateConnectEnabledAndSuccessfulTest extends AbstractSimpleServerClientTest { |
| 55 | + |
| 56 | + ImmediateConnectEnabledAndSuccessfulTest() { |
| 57 | + log.info("--- ImmediateConnectEnabledAnsSuccessfulTest ---"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @DirtiesContext |
| 62 | + void immediateConnectEnabledAndSuccessful() { |
| 63 | + assumeTrue(channel instanceof ManagedChannel, |
| 64 | + "To run this test channel must be ManagedChannel"); |
| 65 | + ManagedChannel managedChannel = (ManagedChannel) channel; |
| 66 | + |
| 67 | + ConnectivityState state = managedChannel.getState(false); |
| 68 | + assertEquals( |
| 69 | + "When immediateConnectTimeout property is set to positive duration channel must be in READY state if connection was successful", |
| 70 | + ConnectivityState.READY, state); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Slf4j |
| 75 | + @SpringBootTest(properties = { |
| 76 | + "grpc.client.GLOBAL.address=localhost:9090", |
| 77 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 78 | + }) |
| 79 | + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) |
| 80 | + @DirtiesContext |
| 81 | + static class ImmediateConnectDisabledTest extends AbstractSimpleServerClientTest { |
| 82 | + |
| 83 | + ImmediateConnectDisabledTest() { |
| 84 | + log.info("--- ImmediateConnectDisabledTest ---"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @DirtiesContext |
| 89 | + void immediateConnectDisabled() { |
| 90 | + assumeTrue(channel instanceof ManagedChannel, |
| 91 | + "To run this test channel must be ManagedChannel"); |
| 92 | + ManagedChannel managedChannel = (ManagedChannel) channel; |
| 93 | + |
| 94 | + ConnectivityState state = managedChannel.getState(false); |
| 95 | + assertEquals( |
| 96 | + "When immediateConnectTimeout property is set to zero or unset grpc must not attempt to connect until first request", |
| 97 | + ConnectivityState.IDLE, state); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Slf4j |
| 102 | + static class ImmediateConnectEnabledAndFailedToConnectTest { |
| 103 | + |
| 104 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 105 | + .withPropertyValues( |
| 106 | + "grpc.client.GLOBAL.address=localhost:9999", |
| 107 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 108 | + "grpc.client.GLOBAL.immediateConnectTimeout=1s") |
| 109 | + .withUserConfiguration(ServiceConfiguration.class, BaseAutoConfiguration.class) |
| 110 | + .withBean(FailedChannelHolder.class); |
| 111 | + |
| 112 | + public ImmediateConnectEnabledAndFailedToConnectTest() { |
| 113 | + log.info("--- ImmediateConnectEnabledAndFailedToConnectTest ---"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void immediateConnectEnabledAndFailedToConnect() { |
| 118 | + contextRunner.run(context -> { |
| 119 | + assertThat(context).hasFailed(); |
| 120 | + assertThat(context.getStartupFailure()) |
| 121 | + .getCause() |
| 122 | + .isOfAnyClassIn(IllegalStateException.class); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + private static class FailedChannelHolder { |
| 127 | + @GrpcClient("test") |
| 128 | + private Channel channel; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments