xds: Per-rpc rewriting of the authority header based on the selected … · grpc/grpc-java@c167ead

@@ -502,6 +502,43 @@ public void uncaughtException(Thread t, Throwable e) {

502502

verify(transportListener).transportTerminated();

503503

}

504504505+

@Test

506+

public void reprocess_authorityOverridePresentInCallOptions_authorityOverrideFromLbIsIgnored() {

507+

DelayedStream delayedStream = (DelayedStream) delayedTransport.newStream(

508+

method, headers, callOptions, tracers);

509+

delayedStream.start(mock(ClientStreamListener.class));

510+

SubchannelPicker picker = mock(SubchannelPicker.class);

511+

PickResult pickResult = PickResult.withSubchannel(

512+

mockSubchannel, null, "authority-override-hostname-from-lb");

513+

when(picker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(pickResult);

514+515+

delayedTransport.reprocess(picker);

516+

fakeExecutor.runDueTasks();

517+518+

verify(mockRealStream, never()).setAuthority("authority-override-hostname-from-lb");

519+

}

520+521+

@Test

522+

public void

523+

reprocess_authorityOverrideNotInCallOptions_authorityOverrideFromLbIsSetIntoStream() {

524+

DelayedStream delayedStream = (DelayedStream) delayedTransport.newStream(

525+

method, headers, callOptions.withAuthority(null), tracers);

526+

delayedStream.start(mock(ClientStreamListener.class));

527+

SubchannelPicker picker = mock(SubchannelPicker.class);

528+

PickResult pickResult = PickResult.withSubchannel(

529+

mockSubchannel, null, "authority-override-hostname-from-lb");

530+

when(picker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(pickResult);

531+

when(mockRealTransport.newStream(

532+

same(method), same(headers), any(CallOptions.class),

533+

ArgumentMatchers.any()))

534+

.thenReturn(mockRealStream);

535+536+

delayedTransport.reprocess(picker);

537+

fakeExecutor.runDueTasks();

538+539+

verify(mockRealStream).setAuthority("authority-override-hostname-from-lb");

540+

}

541+505542

@Test

506543

public void reprocess_NoPendingStream() {

507544

SubchannelPicker picker = mock(SubchannelPicker.class);

@@ -525,6 +562,55 @@ public void reprocess_NoPendingStream() {

525562

assertSame(mockRealStream, stream);

526563

}

527564565+

@Test

566+

public void newStream_assignsTransport_authorityFromCallOptionsSupersedesAuthorityFromLB() {

567+

SubchannelPicker picker = mock(SubchannelPicker.class);

568+

AbstractSubchannel subchannel = mock(AbstractSubchannel.class);

569+

when(subchannel.getInternalSubchannel()).thenReturn(mockInternalSubchannel);

570+

PickResult pickResult = PickResult.withSubchannel(

571+

subchannel, null, "authority-override-hostname-from-lb");

572+

when(picker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(pickResult);

573+

ArgumentCaptor<CallOptions> callOptionsArgumentCaptor =

574+

ArgumentCaptor.forClass(CallOptions.class);

575+

when(mockRealTransport.newStream(

576+

any(MethodDescriptor.class), any(Metadata.class), callOptionsArgumentCaptor.capture(),

577+

ArgumentMatchers.<ClientStreamTracer[]>any()))

578+

.thenReturn(mockRealStream);

579+

delayedTransport.reprocess(picker);

580+

verifyNoMoreInteractions(picker);

581+

verifyNoMoreInteractions(transportListener);

582+583+

CallOptions callOptions =

584+

CallOptions.DEFAULT.withAuthority("authority-override-hosstname-from-calloptions");

585+

delayedTransport.newStream(method, headers, callOptions, tracers);

586+

assertThat(callOptionsArgumentCaptor.getValue().getAuthority()).isEqualTo(

587+

"authority-override-hosstname-from-calloptions");

588+

}

589+590+

@Test

591+

public void newStream_assignsTransport_authorityFromLB() {

592+

SubchannelPicker picker = mock(SubchannelPicker.class);

593+

AbstractSubchannel subchannel = mock(AbstractSubchannel.class);

594+

when(subchannel.getInternalSubchannel()).thenReturn(mockInternalSubchannel);

595+

PickResult pickResult = PickResult.withSubchannel(

596+

subchannel, null, "authority-override-hostname-from-lb");

597+

when(picker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(pickResult);

598+

ArgumentCaptor<CallOptions> callOptionsArgumentCaptor =

599+

ArgumentCaptor.forClass(CallOptions.class);

600+

when(mockRealTransport.newStream(

601+

any(MethodDescriptor.class), any(Metadata.class), callOptionsArgumentCaptor.capture(),

602+

ArgumentMatchers.<ClientStreamTracer[]>any()))

603+

.thenReturn(mockRealStream);

604+

delayedTransport.reprocess(picker);

605+

verifyNoMoreInteractions(picker);

606+

verifyNoMoreInteractions(transportListener);

607+608+

CallOptions callOptions = CallOptions.DEFAULT;

609+

delayedTransport.newStream(method, headers, callOptions, tracers);

610+

assertThat(callOptionsArgumentCaptor.getValue().getAuthority()).isEqualTo(

611+

"authority-override-hostname-from-lb");

612+

}

613+528614

@Test

529615

public void reprocess_newStreamRacesWithReprocess() throws Exception {

530616

final CyclicBarrier barrier = new CyclicBarrier(2);