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