The document discusses testing Cassandra applications and introduces Stubbed Cassandra, a tool for testing Cassandra drivers. Stubbed Cassandra allows priming a fake Cassandra server to return expected responses or errors. It can be used to test failure scenarios, edge cases, and retry policies. The tool has Java and REST APIs and makes it possible to quickly test Cassandra applications in a deterministic and non-brittle way.
6. Tunable consistency
● Each write/read is done at a consistency
● ANY, ONE, TWO, THREE, QUORUM,
LOCAL_QUORUM, EACH_QUORUM, ALL
● Any more than ANY and ONE require a
cluster of more than one node to make
sense
@chbatey
9. Unit & Integration testing
● Requirements:
@chbatey
○ Quick to run
○ Deterministic
○ Not brittle!!
● Integration tests against an embedded Cassandra? E.g.
cassandra-unit
● Integration tests against an external Cassandra running on
developer / CI machines
● Mocking the driver
S
13. Some code that needs testing
@chbatey
public List<Person> retrieveNames() {
ResultSet result;
try {
Statement statement = new SimpleStatement("select * from person");
statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
result = session.execute(statement);
} catch (ReadTimeoutException e) {
throw new UnableToRetrievePeopleException();
}
List<Person> people = new ArrayList<>();
for (Row row : result) {
people.add(new Person(row.getString("first_name"), row.getString("last_name")));
}
return people;
}
14. The Java API
● Wraps Stubbed Cassandra
● With a method call can:
○ Create and start an instance
○ Stop an instance
○ Prime to behave in a certain way
○ Verify interactions
@chbatey
16. @chbatey
Priming Client
public void primeQuery(PrimingRequest primeRequest)
public void primePreparedStatement(PrimingRequest primeRequest)
public List<PrimingRequest> retrievePreparedPrimes()
public List<PrimingRequest> retrieveQueryPrimes()
● Priming Request:
○ Query
○ Consistency(s)
○ Rows
○ Column Types
○ Result e.g. Success, Readtimeout, Writetimeout, Unavailable
○ Variable types (for prepared statements)
17. @chbatey
Activity client
public List<Query> retrieveQueries()
public List<Connection> retrieveConnections()
public List<PreparedStatementExecution> retrievePreparedStatementExecutions()
public void clearConnections()
public void clearQueries()
public void clearPreparedStatementExecutions()
public void clearAllRecordedActivity()
● Query has text and consistency
● Prepared statement has text, consistency and the
bound variables
18. Verifying query consistency
public void testQueryIssuedWithCorrectConsistency() {
Query expectedQuery = Query.builder()
.withQuery("select * from person")
.withConsistency("QUORUM").build();
@chbatey
underTest.retrieveNames();
List<Query> queries = activityClient.retrieveQueries();
assertTrue("Expected query with consistency QUORUM, found following queries: "
+ queries,
queries.contains(expectedQuery));
}
28. What else can I do?
● Start as many instances as you like in the
same JVM
● Prime all primitive types
● Prime sets and lists of type text
● Prime prepared statements
● Verify number of connections your
application makes
@chbatey
29. Future features
● Regular expressions for the query
● Loading of schema for priming prepared
statements
● Pretending to be multiple nodes
@chbatey
30. How does this work?
● Server implemented in Scala
● Binary port for your Cassandra application to
connect to
● Admin port for the REST API
● Thin Java wrapper to make it easy to be
used in JUnit tests
@chbatey
31. How do I get it?
● It is on maven central
<dependency>
<groupId>org.scassandra</groupId>
<artifactId>java-client</artifactId>
<version>0.2.1</version>
<scope>test</scope>
</dependency>
● Or go to www.scassandra.org
○ Executable jar + REST API
@chbatey
32. @chbatey
I want to help
● Server:
○ https://github.com/scassandra/scassandra-server
● Client:
○ https://github.com/chbatey/scassandra-java-client
● Tests:
○ https://github.com/scassandra/scassandra-it-java-driver-
1
33. Summary
● Testing is good - I want more tools to help
me
● Existing tools for Cassandra are great at
happy path scenarios
● Stubbed Cassandra allows testing of edge
cases
@chbatey
34. The end - Questions?
www.scassandra.org
http://christopher-batey.blogspot.co.uk/
@chbatey
@chbatey