/**
  * Prepare all queries on a given session.
  *
  * @param session The session to prepare the queries on
  */
 void prepare_all_queries(test_utils::CassSessionPtr session) {
   for (std::vector<std::string>::const_iterator it = prepared_queries_.begin();
        it != prepared_queries_.end();
        ++it) {
     test_utils::CassFuturePtr future(cass_session_prepare(session.get(), it->c_str()));
     BOOST_REQUIRE_EQUAL(cass_future_error_code(future.get()), CASS_OK);
   }
 }
Example #2
0
 /**
  * Execute a select statement against the system tables for a specified amount
  * of time.
  *
  * NOTE: Results and errors are ignored
  *
  * @param duration Duration in seconds to execute queries
  * @param session Session instance
  */
 void execute_system_query(int duration, test_utils::CassSessionPtr session) {
   boost::posix_time::ptime start = boost::posix_time::second_clock::universal_time();
   while ((boost::posix_time::second_clock::universal_time() - start).total_seconds() < duration) {
     test_utils::CassStatementPtr statement(cass_statement_new("SELECT * FROM system.local", 0));
     cass_statement_set_consistency(statement.get(), CASS_CONSISTENCY_ONE);
     test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get()));
     cass_future_wait_timed(future.get(), test_utils::ONE_SECOND_IN_MICROS);
   }
 }
std::string get_replica(test_utils::CassSessionPtr session,
                        const std::string& keyspace,
                        const std::string& value) {
  // The query doesn't matter
  test_utils::CassStatementPtr statement(
        cass_statement_new("SELECT * FROM system.local", 1));
  cass_statement_bind_string_n(statement.get(), 0, value.data(), value.size());
  cass_statement_add_key_index(statement.get(), 0);
  cass_statement_set_keyspace(statement.get(), keyspace.c_str());
  test_utils::CassFuturePtr future(
        cass_session_execute(session.get(), statement.get()));
  return cass::get_host_from_future(future.get());
}
Example #4
0
  std::string get_executing_host(test_utils::CassSessionPtr session) {
    std::stringstream query;
    query << "SELECT * FROM " << (version >= "3.0.0" ? "system_schema.keyspaces" : "system.schema_keyspaces");
    test_utils::CassStatementPtr statement(cass_statement_new(query.str().c_str(), 0));
    test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get()));
    if (cass_future_error_code(future.get()) ==  CASS_OK) {
      return cass::get_host_from_future(future.get());
    } else {
      CassString message;
      cass_future_error_message(future.get(), &message.data, &message.length);
      std::cerr << "Failed to query host: " << std::string(message.data, message.length) << std::endl;
    }

    return "";
  }
  /**
   * Wait for a session to reconnect to a node.
   *
   * @param session The session to use for waiting
   * @param node The node to wait for
   */
  void wait_for_node(test_utils::CassSessionPtr session, int node) {
    std::stringstream ip_address;
    ip_address << ccm->get_ip_prefix() << node;

    for (int i = 0; i < 30; ++i) {
      test_utils::CassStatementPtr statement(cass_statement_new("SELECT * FROM system.peers", 0));
      test_utils::CassFuturePtr future(cass_session_execute(session.get(),
                                                            statement.get()));
      std::string host(cass::get_host_from_future(future.get()).c_str());
      if (cass_future_error_code(future.get()) == CASS_OK && host == ip_address.str()) {
        return;
      }
      boost::this_thread::sleep_for(boost::chrono::seconds(1));
    }
    BOOST_REQUIRE_MESSAGE(false,
                          "Failed to wait for node " <<
                          ip_address.str() <<
                          " to become availble");
  }
  void check_for_live_hosts(test_utils::CassSessionPtr session,
                            const std::set<std::string>& should_be_present) {
    std::set<std::string> hosts;

    for (size_t i = 0; i < should_be_present.size() + 1; ++i) {
      const char* query = "SELECT * FROM system.schema_keyspaces";
      test_utils::CassStatementPtr statement(cass_statement_new(query, 0));
      test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get()));
      if (cass_future_error_code(future.get()) ==  CASS_OK) {
        hosts.insert(cass::get_host_from_future(future.get()));
      } else {
        CassString message;
        cass_future_error_message(future.get(), &message.data, &message.length);
        BOOST_MESSAGE("Failed to query host: " << std::string(message.data, message.length));
      }
    }

    BOOST_CHECK(hosts.size() == should_be_present.size());
    for (std::set<std::string>::const_iterator it = should_be_present.begin();
         it != should_be_present.end(); ++it) {
      BOOST_CHECK(hosts.count(*it) > 0);
    }
  }
  void check_for_live_hosts(test_utils::CassSessionPtr session,
                            const std::set<std::string>& should_be_present) {
    std::set<std::string> hosts;

    std::stringstream query;
    query << "SELECT * FROM " << (version >= "3.0.0" ? "system_schema.keyspaces" : "system.schema_keyspaces");
    for (size_t i = 0; i < should_be_present.size() + 2; ++i) {
      test_utils::CassStatementPtr statement(cass_statement_new(query.str().c_str(), 0));
      test_utils::CassFuturePtr future(cass_session_execute(session.get(), statement.get()));
      if (cass_future_error_code(future.get()) ==  CASS_OK) {
        hosts.insert(cass::get_host_from_future(future.get()));
      } else {
        CassString message;
        cass_future_error_message(future.get(), &message.data, &message.length);
        std::cerr << "Failed to query host: " << std::string(message.data, message.length) << std::endl;
      }
    }

    BOOST_CHECK(hosts.size() == should_be_present.size());
    for (std::set<std::string>::const_iterator it = should_be_present.begin();
         it != should_be_present.end(); ++it) {
      BOOST_CHECK(hosts.count(*it) > 0);
    }
  }
Example #8
0
 /**
  * Get the number of connections established by the driver.
  *
  * @param session Session instance
  * @param Total number of connections
  */
 cass_uint64_t get_total_connections(test_utils::CassSessionPtr session) {
   CassMetrics metrics;
   cass_session_get_metrics(session.get(), &metrics);
   return metrics.stats.total_connections;
 }