Search in sources :

Example 11 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class ManagedChannelImplIdlenessTest method oobTransportDoesNotAffectIdleness.

@Test
public void oobTransportDoesNotAffectIdleness() {
    // Start a call, which goes to delayed transport
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    // Verify that we have exited the idle mode
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
    Helper helper = helperCaptor.getValue();
    // Fail the RPC
    SubchannelPicker failingPicker = mock(SubchannelPicker.class);
    when(failingPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withError(Status.UNAVAILABLE));
    helper.updatePicker(failingPicker);
    executor.runDueTasks();
    verify(mockCallListener).onClose(same(Status.UNAVAILABLE), any(Metadata.class));
    // ... so that the channel resets its in-use state
    assertFalse(channel.inUseStateAggregator.isInUse());
    // Now make an RPC on an OOB channel
    ManagedChannel oob = helper.createOobChannel(addressGroupList.get(0), "oobauthority");
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
    ClientCall<String, Integer> oobCall = oob.newCall(method, CallOptions.DEFAULT);
    oobCall.start(mockCallListener2, new Metadata());
    verify(mockTransportFactory).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
    MockClientTransportInfo oobTransportInfo = newTransports.poll();
    assertEquals(0, newTransports.size());
    // The OOB transport reports in-use state
    oobTransportInfo.listener.transportInUse(true);
    // But it won't stop the channel from going idle
    verify(mockLoadBalancer, never()).shutdown();
    timer.forwardTime(IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    verify(mockLoadBalancer).shutdown();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) Metadata(io.grpc.Metadata) ManagedChannel(io.grpc.ManagedChannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Matchers.anyString(org.mockito.Matchers.anyString) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 12 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class ManagedChannelImplIdlenessTest method realTransportsHoldsOffIdleness.

@Test
public void realTransportsHoldsOffIdleness() throws Exception {
    final EquivalentAddressGroup addressGroup = addressGroupList.get(1);
    // Start a call, which goes to delayed transport
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    // Verify that we have exited the idle mode
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
    Helper helper = helperCaptor.getValue();
    assertTrue(channel.inUseStateAggregator.isInUse());
    // Assume LoadBalancer has received an address, then create a subchannel.
    Subchannel subchannel = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    subchannel.requestConnection();
    MockClientTransportInfo t0 = newTransports.poll();
    t0.listener.transportReady();
    SubchannelPicker mockPicker = mock(SubchannelPicker.class);
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    helper.updatePicker(mockPicker);
    // Delayed transport creates real streams in the app executor
    executor.runDueTasks();
    // Delayed transport exits in-use, while real transport has not entered in-use yet.
    assertFalse(channel.inUseStateAggregator.isInUse());
    // Now it's in-use
    t0.listener.transportInUse(true);
    assertTrue(channel.inUseStateAggregator.isInUse());
    // As long as the transport is in-use, the channel won't go idle.
    timer.forwardTime(IDLE_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
    assertTrue(channel.inUseStateAggregator.isInUse());
    t0.listener.transportInUse(false);
    assertFalse(channel.inUseStateAggregator.isInUse());
    // And allow the channel to go idle.
    timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
    verify(mockLoadBalancer, never()).shutdown();
    timer.forwardTime(1, TimeUnit.SECONDS);
    verify(mockLoadBalancer).shutdown();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 13 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class InternalSubchannelTest method shutdownNow.

@Test
public void shutdownNow() throws Exception {
    SocketAddress addr = mock(SocketAddress.class);
    createInternalSubchannel(addr);
    internalSubchannel.obtainActiveTransport();
    MockClientTransportInfo t1 = transports.poll();
    t1.listener.transportReady();
    assertExactCallbackInvokes("onStateChange:CONNECTING", "onStateChange:READY");
    t1.listener.transportShutdown(Status.UNAVAILABLE);
    assertExactCallbackInvokes("onStateChange:IDLE");
    internalSubchannel.obtainActiveTransport();
    assertExactCallbackInvokes("onStateChange:CONNECTING");
    MockClientTransportInfo t2 = transports.poll();
    Status status = Status.UNAVAILABLE.withDescription("Requested");
    internalSubchannel.shutdownNow(status);
    verify(t1.transport).shutdownNow(same(status));
    verify(t2.transport).shutdownNow(same(status));
    assertExactCallbackInvokes("onStateChange:SHUTDOWN");
}
Also used : Status(io.grpc.Status) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 14 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class InternalSubchannelTest method shutdownBeforeTransportCreated.

@Test
public void shutdownBeforeTransportCreated() throws Exception {
    SocketAddress addr = mock(SocketAddress.class);
    createInternalSubchannel(addr);
    // First transport is created immediately
    internalSubchannel.obtainActiveTransport();
    assertExactCallbackInvokes("onStateChange:CONNECTING");
    verify(mockTransportFactory).newClientTransport(addr, AUTHORITY, USER_AGENT);
    // Fail this one
    MockClientTransportInfo transportInfo = transports.poll();
    transportInfo.listener.transportShutdown(Status.UNAVAILABLE);
    transportInfo.listener.transportTerminated();
    // Entering TRANSIENT_FAILURE, waiting for back-off
    assertExactCallbackInvokes("onStateChange:" + UNAVAILABLE_STATE);
    // Save the reconnectTask before shutting down
    FakeClock.ScheduledTask reconnectTask = null;
    for (FakeClock.ScheduledTask task : fakeClock.getPendingTasks()) {
        if (task.command.toString().contains("EndOfCurrentBackoff")) {
            assertNull("There shouldn't be more than one reconnectTask", reconnectTask);
            assertFalse(task.isDone());
            reconnectTask = task;
        }
    }
    assertNotNull("There should be at least one reconnectTask", reconnectTask);
    // Shut down InternalSubchannel before the transport is created.
    internalSubchannel.shutdown();
    assertTrue(reconnectTask.isCancelled());
    // InternalSubchannel terminated promptly.
    assertExactCallbackInvokes("onStateChange:SHUTDOWN", "onTerminated");
    // Simulate a race between reconnectTask cancellation and execution -- the task runs anyway.
    // This should not lead to the creation of a new transport.
    reconnectTask.command.run();
    // Futher call to obtainActiveTransport() is no-op.
    assertNull(internalSubchannel.obtainActiveTransport());
    assertEquals(ConnectivityState.SHUTDOWN, internalSubchannel.getState());
    assertNoCallbackInvoke();
    // No more transports will be created.
    fakeClock.forwardMillis(10000);
    assertEquals(ConnectivityState.SHUTDOWN, internalSubchannel.getState());
    verifyNoMoreInteractions(mockTransportFactory);
    assertEquals(0, transports.size());
    assertNoCallbackInvoke();
}
Also used : MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 15 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class InternalSubchannelTest method inUseState.

@Test
public void inUseState() {
    SocketAddress addr = mock(SocketAddress.class);
    createInternalSubchannel(addr);
    internalSubchannel.obtainActiveTransport();
    MockClientTransportInfo t0 = transports.poll();
    t0.listener.transportReady();
    assertExactCallbackInvokes("onStateChange:CONNECTING", "onStateChange:READY");
    t0.listener.transportInUse(true);
    assertExactCallbackInvokes("onInUse");
    t0.listener.transportInUse(false);
    assertExactCallbackInvokes("onNotInUse");
    t0.listener.transportInUse(true);
    assertExactCallbackInvokes("onInUse");
    t0.listener.transportShutdown(Status.UNAVAILABLE);
    assertExactCallbackInvokes("onStateChange:IDLE");
    assertNull(internalSubchannel.obtainActiveTransport());
    MockClientTransportInfo t1 = transports.poll();
    t1.listener.transportReady();
    assertExactCallbackInvokes("onStateChange:CONNECTING", "onStateChange:READY");
    t1.listener.transportInUse(true);
    // InternalSubchannel is already in-use, thus doesn't call the callback
    assertNoCallbackInvoke();
    t1.listener.transportInUse(false);
    // t0 is still in-use
    assertNoCallbackInvoke();
    t0.listener.transportInUse(false);
    assertExactCallbackInvokes("onNotInUse");
}
Also used : MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Aggregations

MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)18 Test (org.junit.Test)17 SocketAddress (java.net.SocketAddress)13 Metadata (io.grpc.Metadata)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 Status (io.grpc.Status)7 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)6 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 CallOptions (io.grpc.CallOptions)3 Helper (io.grpc.LoadBalancer.Helper)3 ManagedChannel (io.grpc.ManagedChannel)3 MethodDescriptor (io.grpc.MethodDescriptor)3 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)3 Attributes (io.grpc.Attributes)2 ResolvedServerInfo (io.grpc.ResolvedServerInfo)2 InOrder (org.mockito.InOrder)2 MetadataApplier (io.grpc.CallCredentials.MetadataApplier)1 Context (io.grpc.Context)1 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1