DATACASS-70 - Optimizing Unit Tests for Mac thread limit

Make use of static session and template for the entire test suite.
Use same keyspace to prevent too many file writer threads.
Clean keyspace after each test.
This commit is contained in:
David Webb
2014-01-16 00:39:32 -05:00
parent fb3b99ddba
commit a7b886c35e
2 changed files with 47 additions and 11 deletions

View File

@@ -7,6 +7,9 @@ import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.KeyspaceMetadata;
@@ -14,22 +17,30 @@ import com.datastax.driver.core.Session;
public abstract class AbstractEmbeddedCassandraIntegrationTest {
private static Logger log = LoggerFactory.getLogger(AbstractEmbeddedCassandraIntegrationTest.class);
protected final static String CASSANDRA_CONFIG = "spring-cassandra.yaml";
protected final static String CASSANDRA_HOST = "localhost";
protected final static int CASSANDRA_NATIVE_PORT = 9042;
@BeforeClass
public static void startCassandra() throws ConfigurationException, TTransportException, IOException,
InterruptedException {
log.info("Starting Cassandra Embedded Server");
EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG);
}
public AbstractEmbeddedCassandraIntegrationTest() {
try {
startCassandra();
} catch (Exception e) {
throw new RuntimeException(e);
if (session == null) {
connect();
}
}
public AbstractEmbeddedCassandraIntegrationTest(String keyspace) {
this.keyspace = keyspace;
if (session == null) {
connect();
}
connect();
}
/**
@@ -48,10 +59,11 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest {
* If not <code>null</code>, get a {@link Session} for the from the {@link #cluster}.
*/
protected String keyspace = "ks" + UUID.randomUUID().toString().replace("-", "");
/**
* The {@link Session} for the {@link #keyspace} from the {@link #cluster}.
*/
protected Session session;
protected static Session session;
protected String keyspace() {
return keyspace;
@@ -91,8 +103,11 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest {
@After
public void after() {
log.info("After: clear -> " + clear + ", connected -> " + connected());
if (clear && connected()) {
log.info("Cleaning Cassandra");
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
}
}

View File

@@ -67,7 +67,7 @@ import com.datastax.driver.core.exceptions.DriverException;
*/
public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegrationTest {
private CassandraOperations cassandraTemplate;
private static CassandraOperations cassandraTemplate;
private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);
@@ -88,9 +88,26 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
"cassandraOperationsTest-cql-dataload.cql", this.keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
CASSANDRA_NATIVE_PORT);
public CassandraOperationsTest() {
super("sdctest");
clear = true;
}
@Before
public void setupTemplate() {
cassandraTemplate = new CassandraTemplate(session);
log.info("Running setupTemplate()");
if (cassandraTemplate == null) {
log.info("null Template ... Initialzing DB test CQL");
// CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet(
// "cassandraOperationsTest-cql-dataload.cql", keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
// CASSANDRA_NATIVE_PORT);
cassandraTemplate = new CassandraTemplate(session);
}
}
@Test
@@ -140,9 +157,11 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
}
@Test
@SuppressWarnings( "unchecked" )
@SuppressWarnings("unchecked")
public void ingestionTestListOfList() {
log.info("Keyspace => " + keyspace);
String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
List<List<?>> values = new LinkedList<List<?>>();
@@ -166,6 +185,8 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
@Test
public void ingestionTestObjectArray() {
log.info("Keyspace => " + keyspace);
String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
Object[][] values = new Object[3][];
@@ -508,7 +529,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
// Insert our 3 test books.
ingestionTestObjectArray();
@SuppressWarnings( "unused" )
@SuppressWarnings("unused")
Book book = cassandraTemplate.queryForObject("select * from book where isbn in ('1234','2345','3456')",
new RowMapper<Book>() {
@Override
@@ -563,7 +584,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
@Test(expected = ClassCastException.class)
public void queryForObjectTestCqlStringRequiredTypeInvalid() {
@SuppressWarnings( "unused" )
@SuppressWarnings("unused")
Float title = cassandraTemplate.queryForObject("select title from book where isbn in ('" + ISBN_NINES + "')",
Float.class);