Standalone (in parallel) client for embedded cassandra

203 views
Skip to first unread message

NL

unread,
Jul 24, 2017, 6:02:28 AM7/24/17
to cassandra-unit-users
Hi,

I understand that cassandra-unit is intended for running unit tests. 

However, once the embedded cassandra is running (after EmbeddedCassandraServerHelper.startEmbeddedCassandra returns), why isn't it possible to start another java process that connects to cassandra and runs a query (or use datastax DevCenter to look at the data)?

Please find the attached test project.

If I run the SimpleCUTest - everythting passes. More importantly this:

com.datastax.driver.core.Cluster cluster =
 
new com.datastax.driver.core.Cluster.Builder().addContactPoints("localhost").withPort(9142).build();
Session session = cluster.connect("test");

works and gives me a session I can work with.

However - if I set a breakpoint (let's say first line in the testQueryWithEmbeddedSession method in SimpleCUTest) and then run another java process (executing CassandraClient) which tries to connect in exactly the same way I get:

Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.OperationTimedOutException: [localhost/127.0.0.1:9142] Operation timed out))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:232)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1600)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1518)
at com.datastax.driver.core.Cluster.init(Cluster.java:159)
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:330)
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:305)
at com.datastax.driver.core.Cluster.connect(Cluster.java:247)
at org.cutest.CassandraClient.main(CassandraClient.java:13)

Is it wrong to expect to be able to connect to localhost on port 1942 while the embedded cassandra is running?

Thank you very much.

Here is the full contents of the two classes (so that you do not need to unzip the attachement):

SimpleCUTest.java

public class SimpleCUTest {
 
@BeforeClass
 
public static void beforeTests() throws InterruptedException, TTransportException, ConfigurationException, IOException {
 
EmbeddedCassandraServerHelper.startEmbeddedCassandra(30000);
 
CQLDataLoader dataLoader = new CQLDataLoader( EmbeddedCassandraServerHelper.getSession());
 dataLoader
.load( new FileCQLDataSet( "./src/test/resources/cassandra-structure.cql", true, false, "test"));
 dataLoader
.load( new FileCQLDataSet( "./src/test/resources/cassandra-data.cql", false, false, "test"));
 
}

 
@AfterClass
 
public static void afterTests() throws InterruptedException, TTransportException, ConfigurationException, IOException {
 
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
 
}

 
@Test
 
public void testQueryWithEmbeddedSession(){
 
Session session = EmbeddedCassandraServerHelper.getSession();
 runSelect
(session);
 
}

 
@Test
 
public void testQueryWithNewSession(){
 com
.datastax.driver.core.Cluster cluster =
 
new com.datastax.driver.core.Cluster.Builder().addContactPoints("localhost").withPort(9142).build();
 
try {
 
Session session = cluster.connect("test");
 runSelect
(session);
 
} finally {
 cluster
.close();
 
}
 
}

 
private void runSelect(Session session){
 
ResultSet messages = session.execute("select * from message");
 messages
.forEach(row -> System.out.println("key=" + row.getString("messagekey") + " payload=" + row.getString("payload")));
 
}
}

CassandraClient

public class CassandraClient {
 
public static void main(String[] args){
 com
.datastax.driver.core.Cluster cluster =
 
new com.datastax.driver.core.Cluster.Builder().addContactPoints("localhost").withPort(9142).build();
 
Session session = cluster.connect();
 
ResultSet messages = session.execute("select * from message");
 messages
.forEach( row -> System.out.println( "key=" + row.getString("messagekey") + " payload=" + row.getString("payload")));
 
}
}

Thank you very much.

Rgds,
NL
cutest.zip

Ivan Vyshnevskyi

unread,
Jul 24, 2017, 9:44:41 AM7/24/17
to cassandra-unit-users
Hey NL,

I suspect the issue is with a breakpoint: you're pausing JVM in which Cassandra runs and then attempt connecting to it, the client doesn't hear back from server and times out.

Regards,
Ivan

NL

unread,
Jul 24, 2017, 10:30:05 AM7/24/17
to cassandra-unit-users
Hey Ivan,

You're right! Thanks! 

I've replaced the breakpoint with a long sleep and now I'm able to connect and query the data.

Still - I would have thought that the only thread being blocked is the JUnit thread that is running the test (maybe IntelliJ does something fancy with the rest of the threads). 

Not sure why a Cassandra IO thread would be blocked as well.

Rgds,
NL

Ivan Vyshnevskyi

unread,
Jul 24, 2017, 11:07:51 AM7/24/17
to cassandra-unit-users
Awesome! Glad to help. :)

> maybe IntelliJ does something fancy with the rest of the threads
You're right, IntelliJ by default will suspend whole JVM when hitting a breakpoint.

Best,
Ivan
Reply all
Reply to author
Forward
0 new messages