Search in sources :

Example 36 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolKeyspaceTest method should_switch_keyspace_on_existing_channels.

@Test
public void should_switch_keyspace_on_existing_channels() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    CqlIdentifier newKeyspace = CqlIdentifier.fromCql("new_keyspace");
    CompletionStage<Void> setKeyspaceFuture = pool.setKeyspace(newKeyspace);
    verify(channel1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(channel2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    assertThatStage(setKeyspaceFuture).isSuccess();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 37 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolKeyspaceTest method should_switch_keyspace_on_pending_channels.

@Test
public void should_switch_keyspace_on_pending_channels() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    CompletableFuture<DriverChannel> channel1Future = new CompletableFuture<>();
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, "mock channel init failure").failure(node, "mock channel init failure").pending(node, channel1Future).pending(node, channel2Future).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    // Check that reconnection has kicked in, but do not complete it yet
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCalls(node, 2);
    // Switch keyspace, it succeeds immediately since there is no active channel
    CqlIdentifier newKeyspace = CqlIdentifier.fromCql("new_keyspace");
    CompletionStage<Void> setKeyspaceFuture = pool.setKeyspace(newKeyspace);
    assertThatStage(setKeyspaceFuture).isSuccess();
    // Now let the two channels succeed to complete the reconnection
    channel1Future.complete(channel1);
    channel2Future.complete(channel2);
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    verify(channel1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(channel2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 38 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionEventsTest method should_process_schema_change_events.

@Test
public void should_process_schema_change_events() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    ArgumentCaptor<DriverChannelOptions> optionsCaptor = ArgumentCaptor.forClass(DriverChannelOptions.class);
    when(channelFactory.connect(eq(node1), optionsCaptor.capture())).thenReturn(CompletableFuture.completedFuture(channel1));
    controlConnection.init(false, false, false);
    await().until(() -> optionsCaptor.getValue() != null);
    EventCallback callback = optionsCaptor.getValue().eventCallback;
    SchemaChangeEvent event = new SchemaChangeEvent(ProtocolConstants.SchemaChangeType.CREATED, ProtocolConstants.SchemaChangeTarget.FUNCTION, "ks", "fn", ImmutableList.of("text", "text"));
    // When
    callback.onEvent(event);
    // Then
    verify(metadataManager).refreshSchema("ks", false, false);
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DriverChannelOptions(com.datastax.oss.driver.internal.core.channel.DriverChannelOptions) EventCallback(com.datastax.oss.driver.internal.core.channel.EventCallback) SchemaChangeEvent(com.datastax.oss.protocol.internal.response.event.SchemaChangeEvent) Test(org.junit.Test)

Example 39 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionEventsTest method should_process_status_change_events.

@Test
public void should_process_status_change_events() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    ArgumentCaptor<DriverChannelOptions> optionsCaptor = ArgumentCaptor.forClass(DriverChannelOptions.class);
    when(channelFactory.connect(eq(node1), optionsCaptor.capture())).thenReturn(CompletableFuture.completedFuture(channel1));
    controlConnection.init(true, false, false);
    await().until(() -> optionsCaptor.getValue() != null);
    EventCallback callback = optionsCaptor.getValue().eventCallback;
    StatusChangeEvent event = new StatusChangeEvent(ProtocolConstants.StatusChangeType.UP, ADDRESS1);
    // When
    callback.onEvent(event);
    // Then
    verify(eventBus).fire(TopologyEvent.suggestUp(ADDRESS1));
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DriverChannelOptions(com.datastax.oss.driver.internal.core.channel.DriverChannelOptions) StatusChangeEvent(com.datastax.oss.protocol.internal.response.event.StatusChangeEvent) EventCallback(com.datastax.oss.driver.internal.core.channel.EventCallback) Test(org.junit.Test)

Example 40 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ControlConnectionEventsTest method should_register_for_schema_events_only_if_topology_not_requested.

@Test
public void should_register_for_schema_events_only_if_topology_not_requested() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    ArgumentCaptor<DriverChannelOptions> optionsCaptor = ArgumentCaptor.forClass(DriverChannelOptions.class);
    when(channelFactory.connect(eq(node1), optionsCaptor.capture())).thenReturn(CompletableFuture.completedFuture(channel1));
    // When
    controlConnection.init(false, false, false);
    // Then
    await().untilAsserted(() -> {
        DriverChannelOptions channelOptions = optionsCaptor.getValue();
        assertThat(channelOptions.eventTypes).containsExactly(ProtocolConstants.EventType.SCHEMA_CHANGE);
        assertThat(channelOptions.eventCallback).isEqualTo(controlConnection);
    });
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DriverChannelOptions(com.datastax.oss.driver.internal.core.channel.DriverChannelOptions) Test(org.junit.Test)

Aggregations

DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)55 Test (org.junit.Test)35 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)30 CompletableFuture (java.util.concurrent.CompletableFuture)17 InOrder (org.mockito.InOrder)13 Node (com.datastax.oss.driver.api.core.metadata.Node)9 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)6 DriverChannelOptions (com.datastax.oss.driver.internal.core.channel.DriverChannelOptions)6 ChannelPromise (io.netty.channel.ChannelPromise)5 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 InetSocketAddress (java.net.InetSocketAddress)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)3 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)3 EventCallback (com.datastax.oss.driver.internal.core.channel.EventCallback)3 Before (org.junit.Before)3 Version (com.datastax.oss.driver.api.core.Version)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2