use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.
the class PerRequestKeyspaceIT method should_execute_simple_statement_with_keyspace.
@Test
@CassandraRequirement(min = "4.0")
public void should_execute_simple_statement_with_keyspace() {
CqlSession session = sessionRule.session();
session.execute(SimpleStatement.newInstance("INSERT INTO foo (k, cc, v) VALUES (?, ?, ?)", nameRule.getMethodName(), 1, 1).setKeyspace(sessionRule.keyspace()));
Row row = session.execute(SimpleStatement.newInstance("SELECT v FROM foo WHERE k = ? AND cc = 1", nameRule.getMethodName()).setKeyspace(sessionRule.keyspace())).one();
assertThat(row.getInt(0)).isEqualTo(1);
}
use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.
the class ExecutionInfoWarningsIT method should_expose_warnings_on_execution_info.
@Test
@CassandraRequirement(min = "2.2")
public void should_expose_warnings_on_execution_info() {
// the default batch size warn threshold is 5 * 1024 bytes, but after CASSANDRA-10876 there must
// be multiple mutations in a batch to trigger this warning so the batch includes 2 different
// inserts.
final String query = String.format("BEGIN UNLOGGED BATCH\n" + "INSERT INTO test (k, v) VALUES (1, '%s')\n" + "INSERT INTO test (k, v) VALUES (2, '%s')\n" + "APPLY BATCH", Strings.repeat("1", 2 * 1024), Strings.repeat("1", 3 * 1024));
Statement<?> st = SimpleStatement.builder(query).build();
ResultSet result = sessionRule.session().execute(st);
ExecutionInfo executionInfo = result.getExecutionInfo();
assertThat(executionInfo).isNotNull();
List<String> warnings = executionInfo.getWarnings();
assertThat(warnings).isNotEmpty();
// verify the log was generated
verify(appender, timeout(500).atLeast(1)).doAppend(loggingEventCaptor.capture());
List<String> logMessages = loggingEventCaptor.getAllValues().stream().map(ILoggingEvent::getFormattedMessage).collect(Collectors.toList());
assertThat(logMessages).anySatisfy(logMessage -> assertThat(logMessage).startsWith("Query '").contains(query.substring(0, RequestLogger.DEFAULT_REQUEST_LOGGER_MAX_QUERY_LENGTH)).contains("' generated server side warning(s): ").contains("Batch").contains("for").contains(String.format("%s.test", sessionRule.keyspace().asCql(true))).contains("is of size").containsPattern("exceeding specified .*threshold"));
}
use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.
the class ExecutionInfoWarningsIT method should_execute_query_and_log_server_side_warnings.
@Test
@CassandraRequirement(min = "3.0")
public void should_execute_query_and_log_server_side_warnings() {
final String query = "SELECT count(*) FROM test;";
Statement<?> st = SimpleStatement.builder(query).build();
ResultSet result = sessionRule.session().execute(st);
ExecutionInfo executionInfo = result.getExecutionInfo();
assertThat(executionInfo).isNotNull();
List<String> warnings = executionInfo.getWarnings();
assertThat(warnings).isNotEmpty();
String warning = warnings.get(0);
assertThat(warning).isEqualTo("Aggregation query used without partition key");
// verify the log was generated
verify(appender, timeout(500).times(1)).doAppend(loggingEventCaptor.capture());
assertThat(loggingEventCaptor.getValue().getMessage()).isNotNull();
String logMessage = loggingEventCaptor.getValue().getFormattedMessage();
assertThat(logMessage).startsWith("Query '[0 values] " + query + "' generated server side warning(s): Aggregation query used without partition key");
}
use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.
the class PreparedStatementIT method should_update_metadata_when_schema_changed_across_pages.
@Test
@CassandraRequirement(min = "4.0")
public void should_update_metadata_when_schema_changed_across_pages() {
// Given
CqlSession session = sessionRule.session();
PreparedStatement ps = session.prepare("SELECT * FROM prepared_statement_test");
ByteBuffer idBefore = ps.getResultMetadataId();
assertThat(ps.getResultSetDefinitions()).hasSize(3);
CompletionStage<AsyncResultSet> future = session.executeAsync(ps.bind());
AsyncResultSet rows = CompletableFutures.getUninterruptibly(future);
assertThat(rows.getColumnDefinitions()).hasSize(3);
assertThat(rows.getColumnDefinitions().contains("d")).isFalse();
// Consume the first page
for (Row row : rows.currentPage()) {
try {
row.getInt("d");
TestCase.fail("expected an error");
} catch (IllegalArgumentException e) {
/*expected*/
}
}
// When
session.execute(SimpleStatement.builder("ALTER TABLE prepared_statement_test ADD d int").setExecutionProfile(sessionRule.slowProfile()).build());
// Then
// this should trigger a background fetch of the second page, and therefore update the
// definitions
rows = CompletableFutures.getUninterruptibly(rows.fetchNextPage());
for (Row row : rows.currentPage()) {
assertThat(row.isNull("d")).isTrue();
}
assertThat(rows.getColumnDefinitions()).hasSize(4);
assertThat(rows.getColumnDefinitions().get("d").getType()).isEqualTo(DataTypes.INT);
// Should have updated the prepared statement too
ByteBuffer idAfter = ps.getResultMetadataId();
assertThat(Bytes.toHexString(idAfter)).isNotEqualTo(Bytes.toHexString(idBefore));
assertThat(ps.getResultSetDefinitions()).hasSize(4);
assertThat(ps.getResultSetDefinitions().get("d").getType()).isEqualTo(DataTypes.INT);
}
use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.
the class PreparedStatementIT method should_update_metadata_when_schema_changed_across_sessions.
@Test
@CassandraRequirement(min = "4.0")
public void should_update_metadata_when_schema_changed_across_sessions() {
// Given
CqlSession session1 = sessionRule.session();
CqlSession session2 = SessionUtils.newSession(ccmRule, sessionRule.keyspace());
PreparedStatement ps1 = session1.prepare("SELECT * FROM prepared_statement_test WHERE a = ?");
PreparedStatement ps2 = session2.prepare("SELECT * FROM prepared_statement_test WHERE a = ?");
ByteBuffer id1a = ps1.getResultMetadataId();
ByteBuffer id2a = ps2.getResultMetadataId();
ResultSet rows1 = session1.execute(ps1.bind(1));
ResultSet rows2 = session2.execute(ps2.bind(1));
assertThat(rows1.getColumnDefinitions()).hasSize(3);
assertThat(rows1.getColumnDefinitions().contains("d")).isFalse();
assertThat(rows2.getColumnDefinitions()).hasSize(3);
assertThat(rows2.getColumnDefinitions().contains("d")).isFalse();
// When
session1.execute("ALTER TABLE prepared_statement_test ADD d int");
rows1 = session1.execute(ps1.bind(1));
rows2 = session2.execute(ps2.bind(1));
ByteBuffer id1b = ps1.getResultMetadataId();
ByteBuffer id2b = ps2.getResultMetadataId();
// Then
assertThat(Bytes.toHexString(id1b)).isNotEqualTo(Bytes.toHexString(id1a));
assertThat(Bytes.toHexString(id2b)).isNotEqualTo(Bytes.toHexString(id2a));
assertThat(ps1.getResultSetDefinitions()).hasSize(4);
assertThat(ps1.getResultSetDefinitions().contains("d")).isTrue();
assertThat(ps2.getResultSetDefinitions()).hasSize(4);
assertThat(ps2.getResultSetDefinitions().contains("d")).isTrue();
assertThat(rows1.getColumnDefinitions()).hasSize(4);
assertThat(rows1.getColumnDefinitions().contains("d")).isTrue();
assertThat(rows2.getColumnDefinitions()).hasSize(4);
assertThat(rows2.getColumnDefinitions().contains("d")).isTrue();
session2.close();
}
Aggregations