use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.
the class CachedSubchannelPoolTest method subchannelReused.
@Test
public void subchannelReused() {
Subchannel subchannel1 = pool.takeOrCreateSubchannel(EAG1, ATTRS1);
assertThat(subchannel1).isNotNull();
InOrder inOrder = Mockito.inOrder(helper);
inOrder.verify(helper).createSubchannel(createSubchannelArgsCaptor.capture());
CreateSubchannelArgs createSubchannelArgs = createSubchannelArgsCaptor.getValue();
assertThat(createSubchannelArgs.getAddresses()).containsExactly(EAG1);
assertThat(createSubchannelArgs.getAttributes()).isEqualTo(ATTRS1);
Subchannel subchannel2 = pool.takeOrCreateSubchannel(EAG2, ATTRS2);
assertThat(subchannel2).isNotNull();
assertThat(subchannel2).isNotSameInstanceAs(subchannel1);
inOrder.verify(helper).createSubchannel(createSubchannelArgsCaptor.capture());
createSubchannelArgs = createSubchannelArgsCaptor.getValue();
assertThat(createSubchannelArgs.getAddresses()).containsExactly(EAG2);
assertThat(createSubchannelArgs.getAttributes()).isEqualTo(ATTRS2);
pool.returnSubchannel(subchannel1, READY_STATE);
// subchannel1 is 1ms away from expiration.
clock.forwardTime(SHUTDOWN_TIMEOUT_MS - 1, MILLISECONDS);
// This will cancel the shutdown timer for subchannel1
Subchannel subchannel1a = pool.takeOrCreateSubchannel(EAG1, ATTRS1);
assertThat(subchannel1a).isSameInstanceAs(subchannel1);
pool.returnSubchannel(subchannel2, READY_STATE);
// subchannel2 expires SHUTDOWN_TIMEOUT_MS after being returned
clock.forwardTime(SHUTDOWN_TIMEOUT_MS - 1, MILLISECONDS);
verify(subchannel2, never()).shutdown();
clock.forwardTime(1, MILLISECONDS);
verify(subchannel2).shutdown();
// pool will create a new channel for EAG2 when requested
Subchannel subchannel2a = pool.takeOrCreateSubchannel(EAG2, ATTRS2);
assertThat(subchannel2a).isNotSameInstanceAs(subchannel2);
inOrder.verify(helper).createSubchannel(createSubchannelArgsCaptor.capture());
createSubchannelArgs = createSubchannelArgsCaptor.getValue();
assertThat(createSubchannelArgs.getAddresses()).containsExactly(EAG2);
assertThat(createSubchannelArgs.getAttributes()).isEqualTo(ATTRS2);
// subchannel1 expires SHUTDOWN_TIMEOUT_MS after being returned
pool.returnSubchannel(subchannel1a, READY_STATE);
clock.forwardTime(SHUTDOWN_TIMEOUT_MS - 1, MILLISECONDS);
verify(subchannel1a, never()).shutdown();
clock.forwardTime(1, MILLISECONDS);
verify(subchannel1a).shutdown();
assertThat(clock.numPendingTasks()).isEqualTo(0);
}
use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.
the class CachedSubchannelPoolTest method clear.
@Test
public void clear() {
Subchannel subchannel1 = pool.takeOrCreateSubchannel(EAG1, ATTRS1);
Subchannel subchannel2 = pool.takeOrCreateSubchannel(EAG2, ATTRS2);
Subchannel subchannel3 = pool.takeOrCreateSubchannel(EAG2, ATTRS2);
pool.returnSubchannel(subchannel1, READY_STATE);
pool.returnSubchannel(subchannel2, READY_STATE);
verify(subchannel1, never()).shutdown();
verify(subchannel2, never()).shutdown();
pool.clear();
verify(subchannel1).shutdown();
verify(subchannel2).shutdown();
verify(subchannel3, never()).shutdown();
assertThat(clock.numPendingTasks()).isEqualTo(0);
}
use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.
the class CachedSubchannelPoolTest method subchannelExpireAfterReturned.
@Test
public void subchannelExpireAfterReturned() {
Subchannel subchannel1 = pool.takeOrCreateSubchannel(EAG1, ATTRS1);
assertThat(subchannel1).isNotNull();
InOrder inOrder = Mockito.inOrder(helper);
inOrder.verify(helper).createSubchannel(createSubchannelArgsCaptor.capture());
CreateSubchannelArgs createSubchannelArgs = createSubchannelArgsCaptor.getValue();
assertThat(createSubchannelArgs.getAddresses()).containsExactly(EAG1);
assertThat(createSubchannelArgs.getAttributes()).isEqualTo(ATTRS1);
Subchannel subchannel2 = pool.takeOrCreateSubchannel(EAG2, ATTRS2);
assertThat(subchannel2).isNotNull();
assertThat(subchannel2).isNotSameInstanceAs(subchannel1);
inOrder.verify(helper).createSubchannel(createSubchannelArgsCaptor.capture());
createSubchannelArgs = createSubchannelArgsCaptor.getValue();
assertThat(createSubchannelArgs.getAddresses()).containsExactly(EAG2);
assertThat(createSubchannelArgs.getAttributes()).isEqualTo(ATTRS2);
pool.returnSubchannel(subchannel1, READY_STATE);
// subchannel1 is 1ms away from expiration.
clock.forwardTime(SHUTDOWN_TIMEOUT_MS - 1, MILLISECONDS);
verify(subchannel1, never()).shutdown();
pool.returnSubchannel(subchannel2, READY_STATE);
// subchannel1 expires. subchannel2 is (SHUTDOWN_TIMEOUT_MS - 1) away from expiration.
clock.forwardTime(1, MILLISECONDS);
verify(subchannel1).shutdown();
// subchanne2 expires.
clock.forwardTime(SHUTDOWN_TIMEOUT_MS - 1, MILLISECONDS);
verify(subchannel2).shutdown();
assertThat(clock.numPendingTasks()).isEqualTo(0);
}
use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.
the class CachedSubchannelPoolTest method returnDuplicateAddressSubchannel.
@Test
public void returnDuplicateAddressSubchannel() {
Subchannel subchannel1 = pool.takeOrCreateSubchannel(EAG1, ATTRS1);
Subchannel subchannel2 = pool.takeOrCreateSubchannel(EAG1, ATTRS2);
Subchannel subchannel3 = pool.takeOrCreateSubchannel(EAG2, ATTRS1);
assertThat(subchannel1).isNotSameInstanceAs(subchannel2);
assertThat(clock.getPendingTasks(SHUTDOWN_TASK_FILTER)).isEmpty();
pool.returnSubchannel(subchannel2, READY_STATE);
assertThat(clock.getPendingTasks(SHUTDOWN_TASK_FILTER)).hasSize(1);
// If the subchannel being returned has an address that is the same as a subchannel in the pool,
// the returned subchannel will be shut down.
verify(subchannel1, never()).shutdown();
pool.returnSubchannel(subchannel1, READY_STATE);
assertThat(clock.getPendingTasks(SHUTDOWN_TASK_FILTER)).hasSize(1);
verify(subchannel1).shutdown();
pool.returnSubchannel(subchannel3, READY_STATE);
assertThat(clock.getPendingTasks(SHUTDOWN_TASK_FILTER)).hasSize(2);
// Returning the same subchannel twice has no effect.
pool.returnSubchannel(subchannel3, READY_STATE);
assertThat(clock.getPendingTasks(SHUTDOWN_TASK_FILTER)).hasSize(2);
verify(subchannel2, never()).shutdown();
verify(subchannel3, never()).shutdown();
}
use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.
the class HealthCheckingLoadBalancerFactoryTest method serviceConfigChangesServiceNameWhenRpcActive.
@Test
public void serviceConfigChangesServiceNameWhenRpcActive() {
Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
ResolvedAddresses result1 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result1);
verify(origLb).handleResolvedAddresses(result1);
verifyNoMoreInteractions(origLb);
Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
SubchannelStateListener mockListener = mockStateListeners[0];
assertThat(unwrap(subchannel)).isSameInstanceAs(subchannels[0]);
InOrder inOrder = inOrder(origLb, mockListener);
deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
inOrder.verify(mockListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(CONNECTING)));
HealthImpl healthImpl = healthImpls[0];
assertThat(healthImpl.calls).hasSize(1);
ServerSideCall serverCall = healthImpl.calls.poll();
assertThat(serverCall.cancelled).isFalse();
assertThat(serverCall.request).isEqualTo(makeRequest("TeeService"));
// Health check responded
serverCall.responseObserver.onNext(makeResponse(ServingStatus.SERVING));
inOrder.verify(mockListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(READY)));
// Service config returns with the same health check name.
hcLbEventDelivery.handleResolvedAddresses(result1);
// It's delivered to origLb, but nothing else happens
inOrder.verify(origLb).handleResolvedAddresses(result1);
verifyNoMoreInteractions(origLb, mockListener);
// Service config returns a different health check name.
resolutionAttrs = attrsWithHealthCheckService("FooService");
ResolvedAddresses result2 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result2);
inOrder.verify(origLb).handleResolvedAddresses(result2);
// Current health check RPC cancelled.
assertThat(serverCall.cancelled).isTrue();
// A second RPC is started immediately
assertThat(healthImpl.calls).hasSize(1);
serverCall = healthImpl.calls.poll();
// with the new service name
assertThat(serverCall.request).isEqualTo(makeRequest("FooService"));
// State stays in READY, instead of switching to CONNECTING.
verifyNoMoreInteractions(origLb, mockListener);
}
Aggregations