|
| 1 | +package org.hypertrace.core.grpcutils.client; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import io.grpc.Channel; |
| 11 | +import io.grpc.ManagedChannel; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +class GrpcChannelRegistryTest { |
| 16 | + |
| 17 | + GrpcChannelRegistry channelRegistry; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void beforeEach() { |
| 21 | + this.channelRegistry = new GrpcChannelRegistry(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void createsNewChannelsAsRequested() { |
| 26 | + assertNotNull(this.channelRegistry.forAddress("foo", 1000)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void reusesChannelsForDuplicateRequests() { |
| 31 | + Channel firstChannel = this.channelRegistry.forAddress("foo", 1000); |
| 32 | + assertSame(firstChannel, this.channelRegistry.forAddress("foo", 1000)); |
| 33 | + assertNotSame(firstChannel, this.channelRegistry.forAddress("foo", 1001)); |
| 34 | + assertNotSame(firstChannel, this.channelRegistry.forAddress("bar", 1000)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void shutdownAllChannelsOnShutdown() { |
| 39 | + ManagedChannel firstChannel = this.channelRegistry.forAddress("foo", 1000); |
| 40 | + ManagedChannel secondChannel = this.channelRegistry.forAddress("foo", 1002); |
| 41 | + assertFalse(firstChannel.isShutdown()); |
| 42 | + assertFalse(secondChannel.isShutdown()); |
| 43 | + this.channelRegistry.shutdown(); |
| 44 | + assertTrue(firstChannel.isShutdown()); |
| 45 | + assertTrue(secondChannel.isShutdown()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void throwsIfNewChannelRequestedAfterShutdown() { |
| 50 | + this.channelRegistry.shutdown(); |
| 51 | + assertThrows(AssertionError.class, () -> this.channelRegistry.forAddress("foo", 1000)); |
| 52 | + } |
| 53 | +} |
0 commit comments