|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2022 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.client.channelfactory; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.time.Duration; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 33 | + |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | + |
| 36 | +import io.grpc.ConnectivityState; |
| 37 | +import io.grpc.ManagedChannel; |
| 38 | +import io.grpc.Server; |
| 39 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 40 | +import net.devh.boot.grpc.client.config.GrpcChannelProperties; |
| 41 | +import net.devh.boot.grpc.client.config.GrpcChannelsProperties; |
| 42 | +import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry; |
| 43 | + |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +class InProcessChannelFactoryTest { |
| 46 | + |
| 47 | + private static final String CHANNEL_NAME = "test"; |
| 48 | + |
| 49 | + @Mock |
| 50 | + GrpcChannelsProperties channelsProperties; |
| 51 | + @Mock |
| 52 | + GrpcChannelProperties channelProperties; |
| 53 | + @Mock |
| 54 | + GlobalClientInterceptorRegistry registry; |
| 55 | + |
| 56 | + Server server; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + void setupServer() throws IOException { |
| 60 | + server = InProcessServerBuilder.forName(CHANNEL_NAME) |
| 61 | + .directExecutor() |
| 62 | + .build(); |
| 63 | + server.start(); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + void tearDownServer() throws InterruptedException { |
| 68 | + server.shutdown(); |
| 69 | + server.awaitTermination(); |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void setupMocks() { |
| 74 | + Mockito.doReturn(channelProperties) |
| 75 | + .when(channelsProperties) |
| 76 | + .getChannel(CHANNEL_NAME); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void checkIdleStateWithoutTimeout() { |
| 81 | + InProcessChannelFactory factory = createFactory(); |
| 82 | + Mockito.doReturn(ImmutableList.of()) |
| 83 | + .when(registry) |
| 84 | + .getClientInterceptors(); |
| 85 | + Mockito.doReturn(Duration.ZERO) |
| 86 | + .when(channelProperties) |
| 87 | + .getImmediateConnectTimeout(); |
| 88 | + |
| 89 | + ManagedChannel channel = (ManagedChannel) factory.createChannel(CHANNEL_NAME); |
| 90 | + |
| 91 | + ConnectivityState state = channel.getState(false); |
| 92 | + assertEquals(ConnectivityState.IDLE, state); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void checkThrowsIllegalStateOnInterrupt() { |
| 97 | + InProcessChannelFactory factory = createFactory(); |
| 98 | + Mockito.doReturn(Duration.ofMillis(100)) |
| 99 | + .when(channelProperties) |
| 100 | + .getImmediateConnectTimeout(); |
| 101 | + |
| 102 | + Thread.currentThread().interrupt(); |
| 103 | + IllegalStateException exception = assertThrows(IllegalStateException.class, |
| 104 | + () -> factory.createChannel(CHANNEL_NAME)); |
| 105 | + assertEquals("Can't connect to channel " + CHANNEL_NAME, exception.getMessage()); |
| 106 | + } |
| 107 | + |
| 108 | + private InProcessChannelFactory createFactory() { |
| 109 | + return new InProcessChannelFactory(channelsProperties, registry); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments