Search in sources :

Example 1 with RequestInfo

use of io.grpc.CallCredentials.RequestInfo in project grpc-java by grpc.

the class CallCredentialsApplyingTest method parameterPropagation_transportSetSecurityLevel.

@Test
public void parameterPropagation_transportSetSecurityLevel() {
    Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY).build();
    when(mockTransport.getAttributes()).thenReturn(transportAttrs);
    transport.newStream(method, origHeaders, callOptions, tracers);
    ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(mockExecutor), any(io.grpc.CallCredentials.MetadataApplier.class));
    RequestInfo info = infoCaptor.getValue();
    assertSame(method, info.getMethodDescriptor());
    assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
    assertSame(AUTHORITY, info.getAuthority());
    assertSame(SecurityLevel.INTEGRITY, info.getSecurityLevel());
}
Also used : Attributes(io.grpc.Attributes) RequestInfo(io.grpc.CallCredentials.RequestInfo) Test(org.junit.Test)

Example 2 with RequestInfo

use of io.grpc.CallCredentials.RequestInfo 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, tracers);
    ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(mockExecutor), any(CallCredentials.MetadataApplier.class));
    RequestInfo info = infoCaptor.getValue();
    assertSame(transportAttrs, info.getTransportAttrs());
    assertSame(method, info.getMethodDescriptor());
    assertSame(AUTHORITY, info.getAuthority());
    assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
Also used : Attributes(io.grpc.Attributes) RequestInfo(io.grpc.CallCredentials.RequestInfo) Test(org.junit.Test)

Example 3 with RequestInfo

use of io.grpc.CallCredentials.RequestInfo 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(GrpcAttributes.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), tracers);
    ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(anotherExecutor), any(CallCredentials.MetadataApplier.class));
    RequestInfo info = infoCaptor.getValue();
    assertSame(transportAttrs, info.getTransportAttrs());
    assertSame(method, info.getMethodDescriptor());
    assertEquals("calloptions-authority", info.getAuthority());
    assertSame(SecurityLevel.INTEGRITY, info.getSecurityLevel());
}
Also used : Executor(java.util.concurrent.Executor) Attributes(io.grpc.Attributes) RequestInfo(io.grpc.CallCredentials.RequestInfo) Test(org.junit.Test)

Example 4 with RequestInfo

use of io.grpc.CallCredentials.RequestInfo in project grpc-java by grpc.

the class ManagedChannelImplTest method informationPropagatedToNewStreamAndCallCredentials.

/**
 * Test that information such as the Call's context, MethodDescriptor, authority, executor are
 * propagated to newStream() and applyRequestMetadata().
 */
@Test
public void informationPropagatedToNewStreamAndCallCredentials() {
    createChannel();
    CallOptions callOptions = CallOptions.DEFAULT.withCallCredentials(creds);
    final Context.Key<String> testKey = Context.key("testing");
    Context ctx = Context.current().withValue(testKey, "testValue");
    final LinkedList<Context> credsApplyContexts = new LinkedList<>();
    final LinkedList<Context> newStreamContexts = new LinkedList<>();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock in) throws Throwable {
            credsApplyContexts.add(Context.current());
            return null;
        }
    }).when(creds).applyRequestMetadata(any(RequestInfo.class), any(Executor.class), any(CallCredentials.MetadataApplier.class));
    // First call will be on delayed transport.  Only newCall() is run within the expected context,
    // so that we can verify that the context is explicitly attached before calling newStream() and
    // applyRequestMetadata(), which happens after we detach the context from the thread.
    Context origCtx = ctx.attach();
    assertEquals("testValue", testKey.get());
    ClientCall<String, Integer> call = channel.newCall(method, callOptions);
    ctx.detach(origCtx);
    assertNull(testKey.get());
    call.start(mockCallListener, new Metadata());
    // Simulate name resolution results
    EquivalentAddressGroup addressGroup = new EquivalentAddressGroup(socketAddress);
    Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    requestConnectionSafely(helper, subchannel);
    verify(mockTransportFactory).newClientTransport(same(socketAddress), eq(clientTransportOptions), any(ChannelLogger.class));
    MockClientTransportInfo transportInfo = transports.poll();
    final ConnectionClientTransport transport = transportInfo.transport;
    when(transport.getAttributes()).thenReturn(Attributes.EMPTY);
    doAnswer(new Answer<ClientStream>() {

        @Override
        public ClientStream answer(InvocationOnMock in) throws Throwable {
            newStreamContexts.add(Context.current());
            return mock(ClientStream.class);
        }
    }).when(transport).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    verify(creds, never()).applyRequestMetadata(any(RequestInfo.class), any(Executor.class), any(CallCredentials.MetadataApplier.class));
    // applyRequestMetadata() is called after the transport becomes ready.
    transportInfo.listener.transportReady();
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    updateBalancingStateSafely(helper, READY, mockPicker);
    executor.runDueTasks();
    ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
    ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
    verify(creds).applyRequestMetadata(infoCaptor.capture(), same(executor.getScheduledExecutorService()), applierCaptor.capture());
    assertEquals("testValue", testKey.get(credsApplyContexts.poll()));
    assertEquals(AUTHORITY, infoCaptor.getValue().getAuthority());
    assertEquals(SecurityLevel.NONE, infoCaptor.getValue().getSecurityLevel());
    verify(transport, never()).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    // newStream() is called after apply() is called
    applierCaptor.getValue().apply(new Metadata());
    verify(transport).newStream(same(method), any(Metadata.class), same(callOptions), ArgumentMatchers.<ClientStreamTracer[]>any());
    assertEquals("testValue", testKey.get(newStreamContexts.poll()));
    // The context should not live beyond the scope of newStream() and applyRequestMetadata()
    assertNull(testKey.get());
    // Second call will not be on delayed transport
    origCtx = ctx.attach();
    call = channel.newCall(method, callOptions);
    ctx.detach(origCtx);
    call.start(mockCallListener, new Metadata());
    verify(creds, times(2)).applyRequestMetadata(infoCaptor.capture(), same(executor.getScheduledExecutorService()), applierCaptor.capture());
    assertEquals("testValue", testKey.get(credsApplyContexts.poll()));
    assertEquals(AUTHORITY, infoCaptor.getValue().getAuthority());
    assertEquals(SecurityLevel.NONE, infoCaptor.getValue().getSecurityLevel());
    // This is from the first call
    verify(transport).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    // Still, newStream() is called after apply() is called
    applierCaptor.getValue().apply(new Metadata());
    verify(transport, times(2)).newStream(same(method), any(Metadata.class), same(callOptions), ArgumentMatchers.<ClientStreamTracer[]>any());
    assertEquals("testValue", testKey.get(newStreamContexts.poll()));
    assertNull(testKey.get());
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) RequestInfo(io.grpc.CallCredentials.RequestInfo) Executor(java.util.concurrent.Executor) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) ChannelLogger(io.grpc.ChannelLogger) Context(io.grpc.Context) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) MethodDescriptor(io.grpc.MethodDescriptor) LinkedList(java.util.LinkedList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) Test(org.junit.Test)

Example 5 with RequestInfo

use of io.grpc.CallCredentials.RequestInfo in project grpc-java by grpc.

the class CallCredentialsApplyingTest method parameterPropagation_callOptionsSetAuthority.

@Test
public void parameterPropagation_callOptionsSetAuthority() {
    Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).build();
    when(mockTransport.getAttributes()).thenReturn(transportAttrs);
    Executor anotherExecutor = mock(Executor.class);
    transport.newStream(method, origHeaders, callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor), tracers);
    ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(anotherExecutor), any(io.grpc.CallCredentials.MetadataApplier.class));
    RequestInfo info = infoCaptor.getValue();
    assertSame(method, info.getMethodDescriptor());
    assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
    assertEquals("calloptions-authority", info.getAuthority());
    assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
Also used : Executor(java.util.concurrent.Executor) Attributes(io.grpc.Attributes) RequestInfo(io.grpc.CallCredentials.RequestInfo) Test(org.junit.Test)

Aggregations

RequestInfo (io.grpc.CallCredentials.RequestInfo)5 Test (org.junit.Test)5 Attributes (io.grpc.Attributes)4 Executor (java.util.concurrent.Executor)3 CallOptions (io.grpc.CallOptions)1 ChannelLogger (io.grpc.ChannelLogger)1 ClientStreamTracer (io.grpc.ClientStreamTracer)1 Context (io.grpc.Context)1 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)1 Subchannel (io.grpc.LoadBalancer.Subchannel)1 Metadata (io.grpc.Metadata)1 MethodDescriptor (io.grpc.MethodDescriptor)1 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)1 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)1 LinkedList (java.util.LinkedList)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1