use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.
the class ControlConnectionTestBase method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
adminEventLoopGroup = new DefaultEventLoopGroup(1);
when(context.getNettyOptions()).thenReturn(nettyOptions);
when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventLoopGroup);
eventBus = spy(new EventBus("test"));
when(context.getEventBus()).thenReturn(eventBus);
when(context.getChannelFactory()).thenReturn(channelFactory);
channelFactoryFuture = new Exchanger<>();
when(channelFactory.connect(any(Node.class), any(DriverChannelOptions.class))).thenAnswer(invocation -> {
CompletableFuture<DriverChannel> channelFuture = new CompletableFuture<>();
channelFactoryFuture.exchange(channelFuture, 100, TimeUnit.MILLISECONDS);
return channelFuture;
});
when(context.getConfig()).thenReturn(config);
when(config.getDefaultProfile()).thenReturn(defaultProfile);
when(defaultProfile.getBoolean(DefaultDriverOption.RECONNECT_ON_INIT)).thenReturn(false);
when(context.getReconnectionPolicy()).thenReturn(reconnectionPolicy);
// Child classes only cover "runtime" reconnections when the driver is already initialized
when(reconnectionPolicy.newControlConnectionSchedule(false)).thenReturn(reconnectionSchedule);
// By default, set a large reconnection delay. Tests that care about reconnection will override
// it.
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofDays(1));
when(context.getLoadBalancingPolicyWrapper()).thenReturn(loadBalancingPolicyWrapper);
when(context.getMetricsFactory()).thenReturn(metricsFactory);
node1 = TestNodeFactory.newNode(1, context);
node2 = TestNodeFactory.newNode(2, context);
mockQueryPlan(node1, node2);
when(metadataManager.refreshNodes()).thenReturn(CompletableFuture.completedFuture(null));
when(metadataManager.refreshSchema(anyString(), anyBoolean(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
when(context.getMetadataManager()).thenReturn(metadataManager);
when(context.getConfig()).thenReturn(config);
when(config.getDefaultProfile()).thenReturn(defaultProfile);
when(defaultProfile.getBoolean(DefaultDriverOption.CONNECTION_WARN_INIT_ERROR)).thenReturn(false);
controlConnection = new ControlConnection(context);
}
use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.
the class DefaultDriverConfigLoaderTest method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(context.getSessionName()).thenReturn("test");
when(context.getNettyOptions()).thenReturn(nettyOptions);
when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventExecutorGroup);
adminExecutor = new ScheduledTaskCapturingEventLoop(adminEventExecutorGroup);
when(adminEventExecutorGroup.next()).thenReturn(adminExecutor);
eventBus = spy(new EventBus("test"));
when(context.getEventBus()).thenReturn(eventBus);
// The already loaded config in the context.
// In real life, it's the object managed by the loader, but in this test it's simpler to mock
// it.
when(context.getConfig()).thenReturn(config);
when(config.getDefaultProfile()).thenReturn(defaultProfile);
when(defaultProfile.getDuration(DefaultDriverOption.CONFIG_RELOAD_INTERVAL)).thenReturn(Duration.ofSeconds(12));
configSource = new AtomicReference<>("int1 = 42");
}
use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.
the class ChannelFactoryTestBase method setup.
@Before
public void setup() throws InterruptedException {
MockitoAnnotations.initMocks(this);
serverGroup = new DefaultEventLoopGroup(1);
clientGroup = new DefaultEventLoopGroup(1);
when(context.getConfig()).thenReturn(driverConfig);
when(driverConfig.getDefaultProfile()).thenReturn(defaultProfile);
when(defaultProfile.isDefined(DefaultDriverOption.AUTH_PROVIDER_CLASS)).thenReturn(false);
when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT)).thenReturn(Duration.ofMillis(TIMEOUT_MILLIS));
when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_SET_KEYSPACE_TIMEOUT)).thenReturn(Duration.ofMillis(TIMEOUT_MILLIS));
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS)).thenReturn(1);
when(defaultProfile.getDuration(DefaultDriverOption.HEARTBEAT_INTERVAL)).thenReturn(Duration.ofSeconds(30));
when(defaultProfile.getDuration(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT)).thenReturn(Duration.ofSeconds(5));
when(context.getProtocolVersionRegistry()).thenReturn(protocolVersionRegistry);
when(context.getNettyOptions()).thenReturn(nettyOptions);
when(nettyOptions.ioEventLoopGroup()).thenReturn(clientGroup);
when(nettyOptions.channelClass()).thenAnswer((Answer<Object>) i -> LocalChannel.class);
when(nettyOptions.allocator()).thenReturn(ByteBufAllocator.DEFAULT);
when(context.getFrameCodec()).thenReturn(FrameCodec.defaultClient(new ByteBufPrimitiveCodec(ByteBufAllocator.DEFAULT), Compressor.none()));
when(context.getSslHandlerFactory()).thenReturn(Optional.empty());
when(context.getEventBus()).thenReturn(eventBus);
when(context.getWriteCoalescer()).thenReturn(new PassThroughWriteCoalescer(null));
when(context.getCompressor()).thenReturn(compressor);
// Start local server
ServerBootstrap serverBootstrap = new ServerBootstrap().group(serverGroup).channel(LocalServerChannel.class).localAddress(SERVER_ADDRESS.resolve()).childHandler(new ServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind().sync();
serverAcceptChannel = (LocalServerChannel) channelFuture.sync().channel();
}
use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.
the class NodeMetadataIT method should_expose_node_metadata.
@Test
public void should_expose_node_metadata() {
try (CqlSession session = SessionUtils.newSession(ccmRule)) {
Node node = getUniqueNode(session);
// Run a few basic checks given what we know about our test environment:
assertThat(node.getEndPoint()).isNotNull();
InetSocketAddress connectAddress = (InetSocketAddress) node.getEndPoint().resolve();
node.getBroadcastAddress().ifPresent(broadcastAddress -> assertThat(broadcastAddress.getAddress()).isEqualTo(connectAddress.getAddress()));
assertThat(node.getListenAddress().get().getAddress()).isEqualTo(connectAddress.getAddress());
assertThat(node.getDatacenter()).isEqualTo("dc1");
assertThat(node.getRack()).isEqualTo("r1");
if (!CcmBridge.DSE_ENABLEMENT) {
// CcmBridge does not report accurate C* versions for DSE, only approximated values
assertThat(node.getCassandraVersion()).isEqualTo(ccmRule.getCassandraVersion());
}
assertThat(node.getState()).isSameAs(NodeState.UP);
assertThat(node.getDistance()).isSameAs(NodeDistance.LOCAL);
assertThat(node.getHostId()).isNotNull();
assertThat(node.getSchemaVersion()).isNotNull();
long upTime1 = node.getUpSinceMillis();
assertThat(upTime1).isGreaterThan(-1);
// Note: open connections and reconnection status are covered in NodeStateIT
// Force the node down and back up to check that upSinceMillis gets updated
EventBus eventBus = ((InternalDriverContext) session.getContext()).getEventBus();
eventBus.fire(TopologyEvent.forceDown(node.getBroadcastRpcAddress().get()));
await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> node.getState() == NodeState.FORCED_DOWN);
assertThat(node.getUpSinceMillis()).isEqualTo(-1);
eventBus.fire(TopologyEvent.forceUp(node.getBroadcastRpcAddress().get()));
await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> node.getState() == NodeState.UP);
assertThat(node.getUpSinceMillis()).isGreaterThan(upTime1);
}
}
use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.
the class MetricsITBase method should_evict_down_node_metrics_when_timeout_fires.
@Test
public void should_evict_down_node_metrics_when_timeout_fires() throws Exception {
// given
Duration expireAfter = Duration.ofSeconds(1);
DriverConfigLoader loader = allMetricsEnabled().withDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER, expireAfter).build();
AbstractMetricUpdater.MIN_EXPIRE_AFTER = expireAfter;
try (CqlSession session = CqlSession.builder().addContactEndPoints(simulacron().getContactPoints()).withConfigLoader(loader).withMetricRegistry(newMetricRegistry()).build()) {
queryAllNodes(session);
DefaultNode node1 = findNode(session, 0);
DefaultNode node2 = findNode(session, 1);
DefaultNode node3 = findNode(session, 2);
EventBus eventBus = ((InternalDriverContext) session.getContext()).getEventBus();
// trigger node1 UP -> DOWN
eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, node1));
Thread.sleep(expireAfter.toMillis());
// then node-level metrics should be evicted from node1, but
// node2 and node3 metrics should not have been evicted
await().untilAsserted(() -> assertNodeMetricsEvicted(session, node1));
assertNodeMetricsNotEvicted(session, node2);
assertNodeMetricsNotEvicted(session, node3);
} finally {
AbstractMetricUpdater.MIN_EXPIRE_AFTER = Duration.ofMinutes(5);
}
}
Aggregations