Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parameter takes the path of application.conf for CqlSession #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,25 @@ public static void cleanDataEmbeddedCassandra(String keyspace, String... exclude
}

public static CqlSession getSession() {
initSession();
initSession(null);
return session;
}

private static synchronized void initSession() {
public static CqlSession getSession(Path confPath) {
log.debug("Use CqlSession config: {}", confPath);
DriverConfigLoader configLoader = DriverConfigLoader.fromFile(confPath.toFile());
initSession(configLoader);
return session;
}

private static synchronized void initSession(DriverConfigLoader configLoader) {
if (session == null) {
DriverConfigLoader configLoader = DriverConfigLoader.programmaticBuilder()
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(0))
.withInt(DefaultDriverOption.METADATA_SCHEMA_MAX_EVENTS, 1)
.build();
if (configLoader == null) {
configLoader = DriverConfigLoader.programmaticBuilder()
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(0))
.withInt(DefaultDriverOption.METADATA_SCHEMA_MAX_EVENTS, 1)
.build();
}
session = CqlSession.builder()
.addContactPoint(new InetSocketAddress(EmbeddedCassandraServerHelper.getHost(), EmbeddedCassandraServerHelper.getNativeTransportPort()))
.withConfigLoader(configLoader)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.cassandraunit.utils;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverConfig;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Ignore;
import org.junit.Test;

/**
* UnitTest for EmbeddedCassandra with DriverConfigLoader. Because Cassandra basically can only be started once per JVM, this test is
* disabled, and should be manually enabled for single tests only. (CassandraDaemon#deactivate is a bad joke. There may be some
* workaround with surefire-fork or classloaders or whatever, but one shouldnt invest too much in a workaround for a broken
* external functionality)
*
* @author Tetsuya Morimoto
*/
@Ignore("Cassandra can only be started once. If you want to run this test, then enable it and run only this test")
public class EmbeddedCassandraServerHelperWithConfigLoaderTest {

private static final String CONF_PATH = "src/test/resources/driver-application.conf";

@Test
public void shouldStartupWithApplicationConfig() throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
Path absolutePath = Paths.get(CONF_PATH).toAbsolutePath();
try (CqlSession session = EmbeddedCassandraServerHelper.getSession(absolutePath)) {
assertThat(session.getMetadata().getNodes().size(), is(1));
KeyspaceMetadata system = session.getMetadata().getKeyspace("system").get();
assertThat(system.getTables().size(), not(0));

DriverConfig config = session.getContext().getConfig();
DriverExecutionProfile profile = config.getDefaultProfile();
assertEquals(1234, profile.getInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS));
}
}

@Test
public void shouldClean() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
assertTrue(true);
}
}
6 changes: 6 additions & 0 deletions cassandra-unit/src/test/resources/driver-application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
datastax-java-driver {
// Don't set basic.contact-points and basic.load-balancing-policy.local-datacenter
// Because these parameters should be set in EmbeddedCassandraServerHelper

advanced.connection.max-requests-per-connection = 1234
}