use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapperTest method should_init_policies_with_all_nodes.
@Test
public void should_init_policies_with_all_nodes() {
// Given
node1.state = NodeState.UP;
node2.state = NodeState.UNKNOWN;
node3.state = NodeState.DOWN;
// When
wrapper.init();
// Then
for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
verify(policy).init(initNodesCaptor.capture(), any(DistanceReporter.class));
Map<UUID, Node> initNodes = initNodesCaptor.getValue();
assertThat(initNodes.values()).containsOnly(node1, node2, node3);
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapperTest method should_accumulate_events_during_init_and_replay.
@Test
public void should_accumulate_events_during_init_and_replay() throws InterruptedException {
// Given
// Hack to obtain concurrency: the main thread releases another thread and blocks; then the
// other thread fires an event on the bus and unblocks the main thread.
CountDownLatch eventLatch = new CountDownLatch(1);
CountDownLatch initLatch = new CountDownLatch(1);
// When
Runnable runnable = () -> {
try {
eventLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
eventBus.fire(NodeStateEvent.changed(NodeState.UNKNOWN, NodeState.DOWN, node1));
initLatch.countDown();
};
Thread thread = new Thread(runnable);
thread.start();
wrapper.init();
// Then
// unblock the thread that will fire the event, and waits until it finishes
eventLatch.countDown();
boolean ok = initLatch.await(500, TimeUnit.MILLISECONDS);
assertThat(ok).isTrue();
for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
verify(policy).onDown(node1);
}
thread.join(500);
assertThat(thread.isAlive()).isFalse();
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapperTest method should_propagate_node_states_to_policies_after_init.
@Test
public void should_propagate_node_states_to_policies_after_init() {
// Given
wrapper.init();
// When
eventBus.fire(NodeStateEvent.changed(NodeState.UNKNOWN, NodeState.UP, node1));
// Then
for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
verify(policy).onUp(node1);
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class PerProfileLoadBalancingPolicyIT method setup.
@BeforeClass
public static void setup() {
// sanity checks
DriverContext context = SESSION_RULE.session().getContext();
DriverConfig config = context.getConfig();
assertThat(config.getProfiles()).containsKeys("profile1", "profile2");
assertThat(context.getLoadBalancingPolicies()).hasSize(3).containsKeys(DriverExecutionProfile.DEFAULT_NAME, "profile1", "profile2");
LoadBalancingPolicy defaultPolicy = context.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
LoadBalancingPolicy policy1 = context.getLoadBalancingPolicy("profile1");
LoadBalancingPolicy policy2 = context.getLoadBalancingPolicy("profile2");
assertThat(defaultPolicy).isSameAs(policy2).isNotSameAs(policy1);
for (Node node : SESSION_RULE.session().getMetadata().getNodes().values()) {
// if node is in dc2 it should be ignored, otherwise (dc1, dc3) it should be local.
NodeDistance expectedDistance = Objects.equals(node.getDatacenter(), "dc2") ? NodeDistance.IGNORED : NodeDistance.LOCAL;
assertThat(node.getDistance()).isEqualTo(expectedDistance);
}
}
Aggregations