Search in sources :

Example 11 with Attributes

use of io.grpc.Attributes in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecoverToGrpclb.

@Test
public void nameResolutionFailsThenRecoverToGrpclb() {
    Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
    deliverNameResolutionError(error);
    verify(helper).updatePicker(pickerCaptor.capture());
    ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
    assertSame(error, errorPicker.result.getStatus());
    // Recover with a subsequent success
    List<ResolvedServerInfoGroup> resolvedServers = createResolvedServerInfoGroupList(true);
    EquivalentAddressGroup eag = resolvedServers.get(0).toEquivalentAddressGroup();
    Attributes resolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
    deliverResolvedAddresses(resolvedServers, resolutionAttrs);
    assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
    assertNull(balancer.getDelegate());
    verify(helper).createOobChannel(eq(eag), eq(lbAuthority(0)));
    verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    verifyNoMoreInteractions(pickFirstBalancerFactory);
    verifyNoMoreInteractions(pickFirstBalancer);
    verifyNoMoreInteractions(roundRobinBalancerFactory);
    verifyNoMoreInteractions(roundRobinBalancer);
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 12 with Attributes

use of io.grpc.Attributes in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method grpclbBalanerCommErrors.

@Test
public void grpclbBalanerCommErrors() {
    InOrder inOrder = inOrder(helper, mockLbService);
    // Make the first LB address fail to connect
    failingLbAuthorities.add(lbAuthority(0));
    List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, true, true);
    Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
    deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
    // First LB addr fails to connect
    inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
    inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
    assertEquals(2, fakeOobChannels.size());
    assertTrue(fakeOobChannels.poll().isShutdown());
    // Will move on to second LB addr
    inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    lbRequestObservers.poll();
    assertEquals(1, fakeOobChannels.size());
    assertFalse(fakeOobChannels.peek().isShutdown());
    Status error1 = Status.UNAVAILABLE.withDescription("error1");
    // Simulate that the stream on the second LB failed
    lbResponseObserver.onError(error1.asException());
    assertTrue(fakeOobChannels.poll().isShutdown());
    inOrder.verify(helper).updatePicker(pickerCaptor.capture());
    ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
    assertEquals(error1.getCode(), errorPicker.result.getStatus().getCode());
    assertTrue(errorPicker.result.getStatus().getDescription().contains(error1.getDescription()));
    // Move on to the third LB.
    inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(2).toEquivalentAddressGroup()), eq(lbAuthority(2)));
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    lbRequestObservers.poll();
    assertEquals(1, fakeOobChannels.size());
    assertFalse(fakeOobChannels.peek().isShutdown());
    // Simulate that the stream on the third LB closed without error.  It is treated
    // as an error.
    lbResponseObserver.onCompleted();
    assertTrue(fakeOobChannels.poll().isShutdown());
    // Loop back to the first LB addr, which still fails.
    inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
    inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
    assertEquals(2, fakeOobChannels.size());
    assertTrue(fakeOobChannels.poll().isShutdown());
    // Will move on to second LB addr
    inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    assertEquals(1, fakeOobChannels.size());
    assertFalse(fakeOobChannels.peek().isShutdown());
    // Finally it works.
    lbResponseObserver.onNext(buildInitialResponse());
    List<ServerEntry> backends = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "token001"), new ServerEntry("127.0.0.1", 2010, "token002"));
    lbResponseObserver.onNext(buildLbResponse(backends));
    inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(0).addr)), any(Attributes.class));
    inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(1).addr)), any(Attributes.class));
    inOrder.verify(helper).updatePicker(same(GrpclbLoadBalancer.BUFFER_PICKER));
    inOrder.verifyNoMoreInteractions();
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) InOrder(org.mockito.InOrder) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 13 with Attributes

use of io.grpc.Attributes in project grpc-java by grpc.

the class RoundRobinLoadBalancerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < 3; i++) {
        SocketAddress addr = new FakeSocketAddress("server" + i);
        EquivalentAddressGroup eag = new EquivalentAddressGroup(addr);
        servers.put(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(addr)).build(), eag);
        subchannels.put(eag, mock(Subchannel.class));
    }
    when(mockHelper.createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class))).then(new Answer<Subchannel>() {

        @Override
        public Subchannel answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Subchannel subchannel = subchannels.get(args[0]);
            when(subchannel.getAttributes()).thenReturn((Attributes) args[1]);
            return subchannel;
        }
    });
    loadBalancer = (RoundRobinLoadBalancer) RoundRobinLoadBalancerFactory.getInstance().newLoadBalancer(mockHelper);
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Attributes(io.grpc.Attributes) ResolvedServerInfo(io.grpc.ResolvedServerInfo) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Example 14 with Attributes

use of io.grpc.Attributes in project grpc-java by grpc.

the class ClientCallImplTest method getAttributes.

@Test
public void getAttributes() {
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
    Attributes attrs = Attributes.newBuilder().set(Key.<String>of("fake key"), "fake value").build();
    when(stream.getAttributes()).thenReturn(attrs);
    assertNotEquals(attrs, call.getAttributes());
    call.start(callListener, new Metadata());
    assertEquals(attrs, call.getAttributes());
}
Also used : Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 15 with Attributes

use of io.grpc.Attributes in project grpc-java by grpc.

the class CallCredentialsApplyingTest method parameterPropagation_overrideByTransport.

@Test
public void parameterPropagation_overrideByTransport() {
    Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).set(CallCredentials.ATTR_AUTHORITY, "transport-override-authority").set(CallCredentials.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY).build();
    when(mockTransport.getAttributes()).thenReturn(transportAttrs);
    transport.newStream(method, origHeaders, callOptions, statsTraceCtx);
    ArgumentCaptor<Attributes> attrsCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(same(method), attrsCaptor.capture(), same(mockExecutor), any(MetadataApplier.class));
    Attributes attrs = attrsCaptor.getValue();
    assertSame(ATTR_VALUE, attrs.get(ATTR_KEY));
    assertEquals("transport-override-authority", attrs.get(CallCredentials.ATTR_AUTHORITY));
    assertSame(SecurityLevel.INTEGRITY, attrs.get(CallCredentials.ATTR_SECURITY_LEVEL));
}
Also used : MetadataApplier(io.grpc.CallCredentials.MetadataApplier) Attributes(io.grpc.Attributes) Test(org.junit.Test)

Aggregations

Attributes (io.grpc.Attributes)21 Test (org.junit.Test)17 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)9 Status (io.grpc.Status)9 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 ErrorPicker (io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker)5 MetadataApplier (io.grpc.CallCredentials.MetadataApplier)4 Subchannel (io.grpc.LoadBalancer.Subchannel)4 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 ManagedChannel (io.grpc.ManagedChannel)3 SocketAddress (java.net.SocketAddress)3 InOrder (org.mockito.InOrder)3 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 Metadata (io.grpc.Metadata)2 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)2 Executor (java.util.concurrent.Executor)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ByteString (com.google.protobuf.ByteString)1 CallOptions (io.grpc.CallOptions)1