|
| 1 | +/* |
| 2 | + * Copyright 2024 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.binder.internal; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import io.grpc.internal.ServerListener; |
| 26 | +import io.grpc.internal.ServerTransport; |
| 27 | +import io.grpc.internal.ServerTransportListener; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.junit.MockitoJUnit; |
| 34 | +import org.mockito.junit.MockitoRule; |
| 35 | +import org.robolectric.RobolectricTestRunner; |
| 36 | + |
| 37 | +@RunWith(RobolectricTestRunner.class) |
| 38 | +public final class ActiveTransportTrackerTest { |
| 39 | + @Rule public final MockitoRule mocks = MockitoJUnit.rule(); |
| 40 | + |
| 41 | + private ActiveTransportTracker tracker; |
| 42 | + |
| 43 | + @Mock Runnable mockShutdownListener; |
| 44 | + @Mock ServerListener mockServerListener; |
| 45 | + @Mock ServerTransportListener mockServerTransportListener; |
| 46 | + @Mock ServerTransport mockServerTransport; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + when(mockServerListener.transportCreated(any())).thenReturn(mockServerTransportListener); |
| 51 | + tracker = new ActiveTransportTracker(mockServerListener, mockShutdownListener); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testServerShutdown_onlyNotifiesAfterAllTransportAreTerminated() { |
| 56 | + ServerTransportListener wrapperListener1 = registerNewTransport(); |
| 57 | + ServerTransportListener wrapperListener2 = registerNewTransport(); |
| 58 | + |
| 59 | + tracker.serverShutdown(); |
| 60 | + // 2 active transports, notification scheduled |
| 61 | + verifyNoInteractions(mockShutdownListener); |
| 62 | + |
| 63 | + wrapperListener1.transportTerminated(); |
| 64 | + // 1 active transport remaining, notification still pending |
| 65 | + verifyNoInteractions(mockShutdownListener); |
| 66 | + |
| 67 | + wrapperListener2.transportTerminated(); |
| 68 | + // No more active transports, shutdown notified |
| 69 | + verify(mockShutdownListener).run(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testServerShutdown_noActiveTransport_notifiesTerminationImmediately() { |
| 74 | + verifyNoInteractions(mockShutdownListener); |
| 75 | + |
| 76 | + tracker.serverShutdown(); |
| 77 | + |
| 78 | + verify(mockShutdownListener).run(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testLastTransportTerminated_serverNotShutdownYet_doesNotNotify() { |
| 83 | + ServerTransportListener wrapperListener = registerNewTransport(); |
| 84 | + verifyNoInteractions(mockShutdownListener); |
| 85 | + |
| 86 | + wrapperListener.transportTerminated(); |
| 87 | + |
| 88 | + verifyNoInteractions(mockShutdownListener); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testTransportCreation_afterServerShutdown_throws() { |
| 93 | + tracker.serverShutdown(); |
| 94 | + |
| 95 | + assertThrows(IllegalStateException.class, this::registerNewTransport); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testServerListenerCallbacks_invokesDelegates() { |
| 100 | + ServerTransportListener listener = tracker.transportCreated(mockServerTransport); |
| 101 | + verify(mockServerListener).transportCreated(mockServerTransport); |
| 102 | + |
| 103 | + listener.transportTerminated(); |
| 104 | + verify(mockServerTransportListener).transportTerminated(); |
| 105 | + |
| 106 | + tracker.serverShutdown(); |
| 107 | + verify(mockServerListener).serverShutdown(); |
| 108 | + } |
| 109 | + |
| 110 | + private ServerTransportListener registerNewTransport() { |
| 111 | + return tracker.transportCreated(mockServerTransport); |
| 112 | + } |
| 113 | +} |
0 commit comments