Search in sources :

Example 6 with EmbeddedCassandraService

use of org.apache.cassandra.service.EmbeddedCassandraService in project eiger by wlloyd.

the class CliTest method testCli.

@Test
public void testCli() throws IOException, TException, ConfigurationException, ClassNotFoundException, TimedOutException, NotFoundException, SchemaDisagreementException, NoSuchFieldException, InvalidRequestException, UnavailableException, InstantiationException, IllegalAccessException {
    new EmbeddedCassandraService().start();
    // new error/output streams for CliSessionState
    ByteArrayOutputStream errStream = new ByteArrayOutputStream();
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    // checking if we can connect to the running cassandra node on localhost
    CliMain.connect("127.0.0.1", 9170);
    // setting new output stream
    CliMain.sessionState.setOut(new PrintStream(outStream));
    CliMain.sessionState.setErr(new PrintStream(errStream));
    // re-creating keyspace for tests
    try {
        // dropping in case it exists e.g. could be left from previous run
        CliMain.processStatement("drop keyspace TestKeySpace;");
    } catch (Exception e) {
    // TODO check before drop so we don't have this fragile ignored exception block
    }
    CliMain.processStatement("create keyspace TestKeySpace;");
    for (String statement : statements) {
        errStream.reset();
        // System.out.println("Executing statement: " + statement);
        CliMain.processStatement(statement);
        String result = outStream.toString();
        // System.out.println("Result:\n" + result);
        assertEquals(errStream.toString() + " processing " + statement, "", errStream.toString());
        if (statement.startsWith("drop ") || statement.startsWith("create ") || statement.startsWith("update ")) {
            assert Pattern.compile("(.{8})-(.{4})-(.{4})-(.{4})-(.{12}).*", Pattern.DOTALL).matcher(result).matches() : String.format("\"%s\" failed: %s", statement, result);
        } else if (statement.startsWith("set ")) {
            assertTrue(result.contains("Value inserted."));
            assertTrue(result.contains("Elapsed time:"));
        } else if (statement.startsWith("incr ")) {
            assertTrue(result.contains("Value incremented."));
        } else if (statement.startsWith("decr ")) {
            assertTrue(result.contains("Value decremented."));
        } else if (statement.startsWith("get ")) {
            if (statement.contains("where")) {
                assertTrue(result.startsWith("-------------------" + System.getProperty("line.separator") + "RowKey:"));
            } else if (statement.contains("Counter")) {
                assertTrue(result.startsWith("=> (counter=") || result.startsWith("Value was not found"));
            } else {
                assertTrue(result.startsWith("=> (column=") || result.startsWith("Value was not found"));
            }
            assertTrue(result.contains("Elapsed time:"));
        } else if (statement.startsWith("truncate ")) {
            assertTrue(result.contains(" truncated."));
        } else if (statement.startsWith("assume ")) {
            assertTrue(result.contains("successfully."));
        }
        // reset stream so we have only output from next statement all the time
        outStream.reset();
        // no errors to the end user.
        errStream.reset();
    }
}
Also used : PrintStream(java.io.PrintStream) EmbeddedCassandraService(org.apache.cassandra.service.EmbeddedCassandraService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TTransportException(org.apache.thrift.transport.TTransportException) TException(org.apache.thrift.TException) IOException(java.io.IOException) ConfigurationException(org.apache.cassandra.config.ConfigurationException) Test(org.junit.Test)

Example 7 with EmbeddedCassandraService

use of org.apache.cassandra.service.EmbeddedCassandraService in project eiger by wlloyd.

the class EmbeddedCops2Test method setup.

/**
 * Set embedded cassandra up and spawn it in a new thread.
 *
 * @throws TTransportException
 * @throws IOException
 * @throws InterruptedException
 */
@BeforeClass
public static void setup() throws TTransportException, IOException, InterruptedException, ConfigurationException {
    EmbeddedCassandraService cassandra = new EmbeddedCassandraService();
    ShortNodeId.updateShortNodeIds(Collections.singletonMap(InetAddress.getByName("127.0.0.1"), new String[] { "DC1:RAC1" }));
    cassandra.start();
    // setup the normal test
    HashMap<String, Integer> localServerIPAndPorts = new HashMap<String, Integer>();
    localServerIPAndPorts.put("localhost", DatabaseDescriptor.getRpcPort());
    Cops2Test.setLocalServerIPAndPorts(localServerIPAndPorts);
    Cops2Test.setConsistencyLevel(ConsistencyLevel.ONE);
}
Also used : HashMap(java.util.HashMap) EmbeddedCassandraService(org.apache.cassandra.service.EmbeddedCassandraService) BeforeClass(org.junit.BeforeClass)

Example 8 with EmbeddedCassandraService

use of org.apache.cassandra.service.EmbeddedCassandraService in project cassandra by apache.

the class PreparedStatementsTest method setup.

@BeforeClass
public static void setup() throws Exception {
    Schema.instance.clear();
    EmbeddedCassandraService cassandra = new EmbeddedCassandraService();
    cassandra.start();
    // Currently the native server start method return before the server is fully binded to the socket, so we need
    // to wait slightly before trying to connect to it. We should fix this but in the meantime using a sleep.
    Thread.sleep(500);
    cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(DatabaseDescriptor.getNativeTransportPort()).build();
    session = cluster.connect();
    session.execute(dropKsStatement);
    session.execute(createKsStatement);
}
Also used : EmbeddedCassandraService(org.apache.cassandra.service.EmbeddedCassandraService) BeforeClass(org.junit.BeforeClass)

Example 9 with EmbeddedCassandraService

use of org.apache.cassandra.service.EmbeddedCassandraService in project cassandra by apache.

the class CorruptionTest method setup.

@BeforeClass()
public static void setup() throws ConfigurationException, IOException {
    Schema.instance.clear();
    cassandra = new EmbeddedCassandraService();
    cassandra.start();
    cluster = Cluster.builder().addContactPoint("127.0.0.1").withRetryPolicy(new LoggingRetryPolicy(Policies.defaultRetryPolicy())).withPort(DatabaseDescriptor.getNativeTransportPort()).build();
    session = cluster.connect();
    session.execute("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE + " WITH replication " + "= {'class':'SimpleStrategy', 'replication_factor':1};");
    session.execute("USE " + KEYSPACE);
    session.execute("CREATE TABLE IF NOT EXISTS " + TABLE + " (" + "key blob," + "value blob," + "PRIMARY KEY (key));");
    // Prepared statements
    getStatement = session.prepare("SELECT value FROM " + TABLE + " WHERE key = ?;");
    getStatement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
    putStatement = session.prepare("INSERT INTO " + TABLE + " (key, value) VALUES (?, ?);");
    putStatement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
    StringBuilder s = new StringBuilder();
    char a = 'a';
    char z = 'z';
    for (int i = 0; i < 500 * 1024; i++) {
        char x = (char) ((i % ((z - a) + 1)) + a);
        if (x == 'a') {
            x = '\n';
        }
        s.append(x);
    }
    VALUE = s.toString();
}
Also used : EmbeddedCassandraService(org.apache.cassandra.service.EmbeddedCassandraService) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy) BeforeClass(org.junit.BeforeClass)

Example 10 with EmbeddedCassandraService

use of org.apache.cassandra.service.EmbeddedCassandraService in project cassandra by apache.

the class BatchTests method setup.

@BeforeClass()
public static void setup() throws ConfigurationException, IOException {
    cassandra = new EmbeddedCassandraService();
    cassandra.start();
    cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(DatabaseDescriptor.getNativeTransportPort()).build();
    session = cluster.connect();
    session.execute("drop keyspace if exists junit;");
    session.execute("create keyspace junit WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
    session.execute("CREATE TABLE junit.noncounter (\n" + "  id int PRIMARY KEY,\n" + "  val text\n" + ");");
    session.execute("CREATE TABLE junit.counter (\n" + "  id int PRIMARY KEY,\n" + "  val counter,\n" + ");");
    session.execute("CREATE TABLE junit.clustering (\n" + "  id int,\n" + "  clustering1 int,\n" + "  clustering2 int,\n" + "  clustering3 int,\n" + "  val text, \n" + " PRIMARY KEY(id, clustering1, clustering2, clustering3)" + ");");
    noncounter = session.prepare("insert into junit.noncounter(id, val)values(?,?)");
    counter = session.prepare("update junit.counter set val = val + ? where id = ?");
    clustering = session.prepare("insert into junit.clustering(id, clustering1, clustering2, clustering3, val) values(?,?,?,?,?)");
}
Also used : EmbeddedCassandraService(org.apache.cassandra.service.EmbeddedCassandraService) BeforeClass(org.junit.BeforeClass)

Aggregations

EmbeddedCassandraService (org.apache.cassandra.service.EmbeddedCassandraService)15 BeforeClass (org.junit.BeforeClass)13 Cluster (com.datastax.driver.core.Cluster)2 ParameterizedClass (org.apache.cassandra.config.ParameterizedClass)2 LoggingRetryPolicy (com.datastax.driver.core.policies.LoggingRetryPolicy)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 ConfigurationException (org.apache.cassandra.config.ConfigurationException)1 TException (org.apache.thrift.TException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 Test (org.junit.Test)1