@Override public void run() { cluster.init(); } }.start();
@Override public Cluster init() { return delegate().init(); }
private UnsupportedProtocolVersionException initWithUnsupportedVersion(Cluster cluster) { Throwable t = null; try { cluster.init(); } catch (Throwable t2) { t = t2; } if (t instanceof UnsupportedProtocolVersionException) { return (UnsupportedProtocolVersionException) t; } else { throw new AssertionError("Expected UnsupportedProtocolVersionException, got " + t); } }
@Override public void run() { try { startLatch.await(); cluster.init(); } catch (InterruptedException e) { fail("unexpected interruption", e); } } });
/** * Validates that when a Cluster is initialized that {@link * SpeculativeExecutionPolicy#init(Cluster)} is called and that when a Cluster is closed {@link * SpeculativeExecutionPolicy#close()} is called. * * @test_category queries:speculative_execution * @expected_result init and close are called on cluster init and close. * @jira_ticket JAVA-796 * @since 2.0.11, 2.1.7, 2.2.1 */ @Test(groups = "short") public void should_init_and_close_policy_on_cluster() { SpeculativeExecutionPolicy mockPolicy = mock(SpeculativeExecutionPolicy.class); Cluster cluster = Cluster.builder() .addContactPoints(scassandras.address(2).getAddress()) .withPort(scassandras.getBinaryPort()) .withSpeculativeExecutionPolicy(mockPolicy) .build(); verify(mockPolicy, times(0)).init(cluster); verify(mockPolicy, times(0)).close(); try { cluster.init(); verify(mockPolicy, times(1)).init(cluster); } finally { cluster.close(); verify(mockPolicy, times(1)).close(); } }
protected void initTestCluster(Object testInstance) throws Exception { if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster()) { Cluster.Builder builder = ccmTestConfig.clusterProvider(testInstance); // add contact points only if the provided builder didn't do so if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints()); builder.withPort(ccm.getBinaryPort()); cluster = register(builder.build()); cluster.init(); } }
@BeforeClass(groups = "short") public void setUp() throws Exception { scassandra = ScassandraCluster.builder().withNodes(2).build(); scassandra.init(); cluster = builder() .addContactPoints(scassandra.address(1).getAddress()) .withPort(scassandra.getBinaryPort()) .build(); cluster.init(); }
@Test(groups = "short") public void should_throw_ufe_when_protocol_version_lesser_than_4() throws Exception { try { Cluster v3cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withProtocolVersion(V3) .build()) .init(); Session v3session = v3cluster.connect(); Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); statement.setOutgoingPayload(payload1); v3session.execute(statement); fail("Should not send custom payloads with protocol V3"); } catch (UnsupportedFeatureException e) { assertThat(e.getMessage()) .isEqualTo( "Unsupported feature with the native protocol V3 (which is currently in use): Custom payloads are only supported since native protocol V4"); } }
@BeforeMethod(groups = "short") public void setup() { QueryOptions queryOptions = new QueryOptions(); queryOptions.setRefreshNodeListIntervalMillis(DEBOUNCE_TIME); queryOptions.setMaxPendingRefreshNodeListRequests(5); queryOptions.setRefreshSchemaIntervalMillis(0); // Create a separate cluster that will receive the schema events on its control connection. cluster2 = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withQueryOptions(queryOptions) .build()); cluster2.init(); // Create a spy of the Cluster's control connection and replace it with the spy. controlConnection = spy(cluster2.manager.controlConnection); cluster2.manager.controlConnection = controlConnection; reset(controlConnection); }
private Cluster connectWithoutVersion() { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .build()); cluster.init(); return cluster; }
protected void resetTestSession() throws Exception { session.close(); Cluster.Builder builder = ccmTestConfig.clusterProvider(this); // add contact points only if the provided builder didn't do so if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints()); builder.withPort(ccm.getBinaryPort()); cluster = register(builder.build()); cluster.init(); session.close(); session = register(cluster.connect()); useKeyspace(session, keyspace); }
/** * Validates that when metrics are enabled but JMX reporting is disabled via {@link * Cluster.Builder#withoutJMXReporting()} that {@link Cluster#getMetrics()} is not null and * 'clusterName-metrics:name=connected-to' MBean is present. * * @test_category metrics */ @Test(groups = "short", expectedExceptions = InstanceNotFoundException.class) public void should_be_no_jmx_mbean_when_jmx_is_disabled() throws Exception { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withoutJMXReporting() .build()); try { cluster.init(); assertThat(cluster.getMetrics()).isNotNull(); assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue(); assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isFalse(); ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); server.getMBeanInfo(clusterMetricsON); } finally { cluster.close(); } } }
private Cluster connectWithVersion(ProtocolVersion version) { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withProtocolVersion(version) .build()); cluster.init(); return cluster; }
@Test(groups = "short") public void should_not_leak_session_when_wrong_keyspace() throws Exception { // Checking for JAVA-806 channelMonitor = new SocketChannelMonitor(); channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints().get(0)) .withPort(ccm().getBinaryPort()) .withNettyOptions(channelMonitor.nettyOptions()) .build()); cluster.init(); assertThat(cluster.manager.sessions.size()).isEqualTo(0); try { // Should be 1 control connection after initialization. assertOpenConnections(1, cluster); cluster.connect("wrong_keyspace"); fail("Should not have connected to a wrong keyspace"); } catch (InvalidQueryException e) { // ok } assertThat(cluster.manager.sessions.size()).isEqualTo(0); cluster.close(); // Ensure no channels remain open. channelMonitor.stop(); channelMonitor.report(); assertThat(channelMonitor.openChannels(ccm().addressOfNode(1), ccm().addressOfNode(2)).size()) .isEqualTo(0); }
@BeforeClass(groups = "short") public void setup() throws InterruptedException { Cluster.Builder builder = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withQueryOptions(nonDebouncingQueryOptions()); cluster1 = builder.build(); cluster2 = builder.build(); schemaDisabledCluster = spy( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withClusterName("schema-disabled") .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false)) .build()); schemaDisabledSession = schemaDisabledCluster.connect(); schemaDisabledControlConnection = spy(schemaDisabledCluster.manager.controlConnection); schemaDisabledCluster.manager.controlConnection = schemaDisabledControlConnection; session1 = cluster1.connect(); cluster2.init(); cluster1.register(listener1 = mock(SchemaChangeListener.class)); cluster2.register(listener2 = mock(SchemaChangeListener.class)); listeners = Lists.newArrayList(listener1, listener2); schemaDisabledCluster.register(schemaDisabledListener = mock(SchemaChangeListener.class)); verify(schemaDisabledListener, times(1)).onRegister(schemaDisabledCluster); execute(CREATE_KEYSPACE, "lowercase"); execute(CREATE_KEYSPACE, "\"CaseSensitive\""); }
/** * Validates that when metrics are disabled using {@link Cluster.Builder#withoutMetrics()} that * {@link Cluster#getMetrics()} returns null and 'clusterName-metrics:name=connected-to' MBean is * not present. * * @test_category metrics */ @Test(groups = "short", expectedExceptions = InstanceNotFoundException.class) public void metrics_should_be_null_when_metrics_disabled() throws Exception { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withoutMetrics() .build()); try { cluster.init(); assertThat(cluster.getMetrics()).isNull(); assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isFalse(); ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); server.getMBeanInfo(clusterMetricsON); } finally { cluster.close(); } }
/** * Ensures that calling {@link Metadata#newTokenRange(Token, Token)} on a Cluster that has schema * metadata disabled will throw a {@link IllegalStateException}. * * @jira_ticket JAVA-858 * @since 2.0.11 */ @Test(groups = "short", expectedExceptions = IllegalStateException.class) public void should_throw_illegal_state_exception_on_newTokenRange_with_metadata_disabled() { Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false)) .build(); try { cluster.init(); Token.Factory factory = Token.getFactory("Murmur3Partitioner"); Token token = factory.fromString(Long.toString(1)); cluster.getMetadata().newTokenRange(token, token); } finally { cluster.close(); } }
/** * Ensures that calling {@link Metadata#newToken(String)} on a Cluster that has schema metadata * disabled will throw a {@link IllegalStateException}. * * @jira_ticket JAVA-858 * @since 2.0.11 */ @Test(groups = "short", expectedExceptions = IllegalStateException.class) public void should_throw_illegal_state_exception_on_newToken_with_metadata_disabled() { Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false)) .build(); try { cluster.init(); cluster.getMetadata().newToken("0x00"); } finally { cluster.close(); } }
/** * Ensures that when a cluster is initialized that {@link LoadBalancingPolicy#init(Cluster, * Collection)} is called with each reachable contact point. * * @test_category load_balancing:notification * @expected_result init() is called for each of two contact points. */ @Test(groups = "short") public void should_init_policy_with_up_contact_points() throws Exception { HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy()); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withLoadBalancingPolicy(policy) .build()); try { cluster.init(); assertThat(policy.history) .containsOnly( entry(INIT, TestUtils.findHost(cluster, 1)), entry(INIT, TestUtils.findHost(cluster, 2))); } finally { cluster.close(); } }