Search in sources :

Example 6 with Attributes

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

the class CallCredentialsApplyingTest method parameterPropagation_overrideByCallOptions.

@Test
public void parameterPropagation_overrideByCallOptions() {
    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);
    Executor anotherExecutor = mock(Executor.class);
    transport.newStream(method, origHeaders, callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor), statsTraceCtx);
    ArgumentCaptor<Attributes> attrsCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(same(method), attrsCaptor.capture(), same(anotherExecutor), any(MetadataApplier.class));
    Attributes attrs = attrsCaptor.getValue();
    assertSame(ATTR_VALUE, attrs.get(ATTR_KEY));
    assertEquals("calloptions-authority", attrs.get(CallCredentials.ATTR_AUTHORITY));
    assertSame(SecurityLevel.INTEGRITY, attrs.get(CallCredentials.ATTR_SECURITY_LEVEL));
}
Also used : MetadataApplier(io.grpc.CallCredentials.MetadataApplier) Executor(java.util.concurrent.Executor) Attributes(io.grpc.Attributes) Test(org.junit.Test)

Example 7 with Attributes

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

the class CallCredentialsApplyingTest method parameterPropagation_base.

@Test
public void parameterPropagation_base() {
    Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).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));
    assertSame(AUTHORITY, attrs.get(CallCredentials.ATTR_AUTHORITY));
    assertSame(SecurityLevel.NONE, attrs.get(CallCredentials.ATTR_SECURITY_LEVEL));
}
Also used : MetadataApplier(io.grpc.CallCredentials.MetadataApplier) Attributes(io.grpc.Attributes) Test(org.junit.Test)

Example 8 with Attributes

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

the class GrpclbLoadBalancerTest method delegatingPickFirstThenNameResolutionFails.

@Test
public void delegatingPickFirstThenNameResolutionFails() {
    List<ResolvedServerInfoGroup> resolvedServers = createResolvedServerInfoGroupList(false);
    Attributes resolutionAttrs = Attributes.newBuilder().set(RESOLUTION_ATTR, "yeah").build();
    deliverResolvedAddresses(resolvedServers, resolutionAttrs);
    verify(pickFirstBalancerFactory).newLoadBalancer(helper);
    verify(pickFirstBalancer).handleResolvedAddresses(eq(resolvedServers), eq(resolutionAttrs));
    // Then let name resolution fail.  The error will be passed directly to the delegate.
    Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
    deliverNameResolutionError(error);
    verify(pickFirstBalancer).handleNameResolutionError(error);
    verify(helper, never()).updatePicker(any(SubchannelPicker.class));
    verifyNoMoreInteractions(roundRobinBalancerFactory);
    verifyNoMoreInteractions(roundRobinBalancer);
}
Also used : Status(io.grpc.Status) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 9 with Attributes

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

the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecoverToDelegate.

@Test
public void nameResolutionFailsThenRecoverToDelegate() {
    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(false);
    Attributes resolutionAttrs = Attributes.newBuilder().set(RESOLUTION_ATTR, "yeah").build();
    deliverResolvedAddresses(resolvedServers, resolutionAttrs);
    verify(pickFirstBalancerFactory).newLoadBalancer(helper);
    verify(pickFirstBalancer).handleResolvedAddresses(eq(resolvedServers), eq(resolutionAttrs));
    verifyNoMoreInteractions(roundRobinBalancerFactory);
    verifyNoMoreInteractions(roundRobinBalancer);
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 10 with Attributes

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

the class GrpclbLoadBalancerTest method grpclbThenNameResolutionFails.

@Test
public void grpclbThenNameResolutionFails() {
    InOrder inOrder = inOrder(helper);
    // Go to GRPCLB first
    List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, true);
    Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
    deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
    assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
    assertNull(balancer.getDelegate());
    verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
    assertEquals(1, fakeOobChannels.size());
    ManagedChannel oobChannel = fakeOobChannels.poll();
    verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
    // Let name resolution fail before round-robin list is ready
    Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
    deliverNameResolutionError(error);
    inOrder.verify(helper).updatePicker(pickerCaptor.capture());
    ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
    assertSame(error, errorPicker.result.getStatus());
    assertFalse(oobChannel.isShutdown());
    // Simulate receiving LB response
    List<ServerEntry> backends = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "TOKEN1"), new ServerEntry("127.0.0.1", 2010, "TOKEN2"));
    verify(helper, never()).runSerialized(any(Runnable.class));
    lbResponseObserver.onNext(buildInitialResponse());
    lbResponseObserver.onNext(buildLbResponse(backends));
    verify(helper, times(2)).runSerialized(any(Runnable.class));
    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));
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) InOrder(org.mockito.InOrder) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) 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