Skip to content

Commit

Permalink
CPP-561: Migrating boost test for control connection tests
Browse files Browse the repository at this point in the history
* Updated consistency tests to use forced decommissioning
  • Loading branch information
Michael Fero authored and mikefero committed Apr 3, 2018
1 parent 840b533 commit a5b064c
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 36 deletions.
37 changes: 35 additions & 2 deletions gtests/src/integration/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,27 @@ class CassException : public Exception {
* @param message Exception message
* @param error_code Error code
*/
CassException(const std::string &message, const CassError error_code)
CassException(const std::string& message, const CassError error_code)
: Exception(message)
, error_code_(error_code) {}

/**
* Exception class that contains an error code
*
* @param message Exception message
* @param error_code Error code
* @param error_code Error message
*/
CassException(const std::string& message, const CassError error_code,
const std::string& error_message)
: Exception(message)
, error_code_(error_code)
, error_message_(error_message) { }

/**
* Destructor
*/
virtual ~CassException() throw() {}
virtual ~CassException() throw() { }

/**
* Get the error code associated with the exception
Expand All @@ -70,11 +83,31 @@ class CassException : public Exception {
*/
CassError error_code() const { return error_code_; }

/**
* Get the human readable description of the error code
*
* @return Error description
*/
const std::string error_description() {
return std::string(cass_error_desc(error_code()));
}

/**
* Get the error message associated with the exception
*
* @return Error message
*/
const std::string error_message() { return error_message_; }

private:
/**
* Error code associated with the exception
*/
CassError error_code_;
/**
* Error message associated with the exception
*/
std::string error_message_;
};

} // namespace test
Expand Down
39 changes: 31 additions & 8 deletions gtests/src/integration/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Integration::Integration()
, session_()
, keyspace_name_("")
, table_name_("")
, system_schema_keyspaces_("system.schema_keyspaces")
, uuid_generator_()
, server_version_(Options::server_version())
, number_dc1_nodes_(1)
Expand All @@ -54,6 +55,11 @@ Integration::Integration()
, protocol_version_(CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION)
, create_keyspace_query_("")
, start_time_(0ull) {
// Determine if the schema keyspaces table should be updated
if (server_version_ >= "3.0.0") {
system_schema_keyspaces_ = "system_schema.keyspaces";
}

// Get the name of the test and the case/suite it belongs to
const testing::TestInfo* test_information = testing::UnitTest::GetInstance()->current_test_info();
test_name_ = test_information->name();
Expand Down Expand Up @@ -167,13 +173,25 @@ void Integration::SetUp() {
}

void Integration::TearDown() {
// Restart all stopped nodes
if (!is_test_chaotic_) { // No need to restart as cluster will be destroyed
for (std::vector<unsigned int>::iterator iterator = stopped_nodes_.begin();
iterator != stopped_nodes_.end(); ++iterator) {
TEST_LOG("Restarting Node Stopped in " << test_name_ << ": " << *iterator);
ccm_->start_node(*iterator);
}
}
stopped_nodes_.clear();

// Drop keyspace for integration test (may or may have not been created)
std::stringstream use_keyspace_query;
use_keyspace_query << "DROP KEYSPACE " << keyspace_name_;
try {
session_.execute(use_keyspace_query.str(), CASS_CONSISTENCY_ANY, false,
false);
} catch (...) {}
if (!is_test_chaotic_) { // No need to drop keyspace as cluster will be destroyed
std::stringstream use_keyspace_query;
use_keyspace_query << "DROP KEYSPACE " << keyspace_name_;
try {
session_.execute(use_keyspace_query.str(), CASS_CONSISTENCY_ANY, false,
false);
} catch (...) { }
}

// Determine if the CCM cluster should be destroyed
if (is_test_chaotic_) {
Expand Down Expand Up @@ -332,16 +350,21 @@ void Integration::enable_cluster_tracing(bool enable /*= true*/) {
}
}

bool Integration::decommission_node(unsigned int node) {
bool Integration::decommission_node(unsigned int node,
bool is_force /*= false */) {
// Decommission the requested node
bool status = ccm_->decommission_node(node);
bool status = ccm_->decommission_node(node, is_force);
if (status) {
// Indicate the test is chaotic
is_test_chaotic_ = true;
}
return status;
}

bool Integration::force_decommission_node(unsigned int node) {
return decommission_node(node, true);
}

bool Integration::stop_node(unsigned int node) {
// Stop the requested node
bool status = ccm_->stop_node(node);
Expand Down
20 changes: 19 additions & 1 deletion gtests/src/integration/integration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class Integration : public testing::Test {
* Generated table name for the integration test
*/
std::string table_name_;
/**
* Keyspaces schema table
*/
std::string system_schema_keyspaces_;
/**
* UUID generator
*/
Expand Down Expand Up @@ -366,10 +370,24 @@ class Integration : public testing::Test {
* completed
*
* @param node Node that should be decommissioned
* @oaram is_force True if decommission should be forced; false otherwise
* (default: false)
* @return True if node was decommissioned; false otherwise (the node is
* invalid or was already decommissioned)
*/
virtual bool decommission_node(unsigned int node, bool is_force = false);

/**
* Decommission a node by force and ensure the cluster is set to be removed
* after the test is completed
*
* NOTE: Alias for decommission_node(node, true)
*
* @param node Node that should be decommissioned forcefully
* @return True if node was decommissioned; false otherwise (the node is
* invalid or was already decommissioned)
*/
virtual bool decommission_node(unsigned int node);
virtual bool force_decommission_node(unsigned int node);

/**
* Stop a node that should be restarted after test is completed
Expand Down
36 changes: 36 additions & 0 deletions gtests/src/integration/objects/cluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ class Cluster : public Object<CassCluster, cass_cluster_free> {
return *this;
}

/**
* Sets the number of I/O threads. This is the number of threads that will
* handle query requests
*
* @param num_threads Number of thread that will handle query requests
* @return Cluster object
*/
Cluster& with_num_threads_io(unsigned int num_threads) {
EXPECT_EQ(CASS_OK, cass_cluster_set_num_threads_io(get(), num_threads));
return *this;
}

/**
* Enable data center aware load balance policy for statement/batch execution
*
* @param local_dc The primary data center to try first
* @param used_hosts_per_remote_dc The number of hosts used in each remote
* data center if no hosts are available in
* the local data center
* @param allow_remote_dcs_for_local_cl True if remote hosts are to be used as
* local data centers when no local data
* center is available and consistency
* levels are LOCAL_ONE or LOCAL_QUORUM;
* otherwise false
* @return Cluster object
*/
Cluster& with_load_balance_dc_aware(const std::string& local_dc,
size_t used_hosts_per_remote_dc,
bool allow_remote_dcs_for_local_cl) {
EXPECT_EQ(CASS_OK,
cass_cluster_set_load_balance_dc_aware(get(), local_dc.c_str(),
used_hosts_per_remote_dc,
(allow_remote_dcs_for_local_cl == true ? cass_true : cass_false)));
return *this;
}

/**
* Enable round robin load balance policy for statement/batch execution
*
Expand Down
15 changes: 9 additions & 6 deletions gtests/src/integration/objects/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,32 @@ friend class Cluster;
public:
class Exception : public test::CassException {
public:
Exception(const std::string& message, const CassError error_code)
: test::CassException(message, error_code) {}
Exception(const std::string& message, const CassError error_code,
const std::string& error_message)
: test::CassException(message, error_code, error_message) { }
};

/**
* Create the default session object
*/
Session()
: Object<CassSession, cass_session_free>(cass_session_new()) {}
: Object<CassSession, cass_session_free>(cass_session_new()) { }

/**
* Create the session object from the native driver object
*
* @param session Native driver object
*/
Session(CassSession* session)
: Object<CassSession, cass_session_free>(session) {}
: Object<CassSession, cass_session_free>(session) { }

/**
* Create the session object from a shared reference
*
* @param session Shared reference
*/
Session(Ptr session)
: Object<CassSession, cass_session_free>(session) {}
: Object<CassSession, cass_session_free>(session) { }

/**
* Close the active session
Expand Down Expand Up @@ -240,7 +241,9 @@ friend class Cluster;
session.connect_future_.wait(false);
if (assert_ok && session.connect_error_code() != CASS_OK) {
throw Exception("Unable to Establish Session Connection: "
+ session.connect_error_description(), session.connect_error_code());
+ session.connect_error_description(),
session.connect_error_code(),
session.connect_error_message());
}
return session;
}
Expand Down
8 changes: 1 addition & 7 deletions gtests/src/integration/tests/test_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ class AuthenticationTests : public Integration {
CASSANDRA_INTEGRATION_TEST_F(AuthenticationTests, ProtocolVersions) {
CHECK_FAILURE;

// Determine the schema keyspaces table (based on server version used)
std::string schema_keysapces_table = "system.schema_keyspaces";
if (server_version_ >= "3.0.0") {
schema_keysapces_table = "system_schema.keyspaces";
}

// Iterate over all known/supported protocol versions
for (int i = starting_protocol_version_;
i <= CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION; ++i) {
Expand All @@ -107,7 +101,7 @@ CASSANDRA_INTEGRATION_TEST_F(AuthenticationTests, ProtocolVersions) {
<< session.connect_error_description();

// Execute a query against the schema keyspaces table
Result result = session.execute("SELECT * FROM " + schema_keysapces_table);
Result result = session.execute("SELECT * FROM " + system_schema_keyspaces_);
ASSERT_EQ(CASS_OK, result.error_code());
ASSERT_GT(result.row_count(), 0u);
}
Expand Down
6 changes: 3 additions & 3 deletions gtests/src/integration/tests/test_consistency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ CASSANDRA_INTEGRATION_TEST_F(ConsistencyThreeNodeClusterTests,
session_.execute(select_);

// Decommission node two
decommission_node(2);
force_decommission_node(2);

// Perform a check using consistency `QUORUM` (N=2, RF=3)
insert_.set_consistency(CASS_CONSISTENCY_QUORUM);
Expand Down Expand Up @@ -298,8 +298,8 @@ CASSANDRA_INTEGRATION_TEST_F(ConsistencyThreeNodeClusterTests,
session_.execute(select_);

// Decommission node two and three
decommission_node(2);
decommission_node(3);
force_decommission_node(2);
force_decommission_node(3);

// Perform a check using consistency `ONE` (N=1, RF=3)
insert_.set_consistency(CASS_CONSISTENCY_ONE);
Expand Down
Loading

0 comments on commit a5b064c

Please sign in to comment.