<commit_before>/*
  Copyright (c) 2014 DataStax

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE cassandra
#endif

#include "cassandra.h"
#include "test_utils.hpp"
#include "cql_ccm_bridge.hpp"

#include <boost/format.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>

#include <algorithm>

struct ClusterInit {
  ClusterInit()
    : inst(3, 0),
      log_counter("", true),
      session(NULL) {
    cass_cluster_set_log_level(inst.cluster, CASS_LOG_DEBUG);
    cass_cluster_set_log_callback(inst.cluster, test_utils::count_message_log_callback, &log_counter);
    new_session();
  }

  ~ClusterInit() {
    close_session();
  }

  void new_session() {
    close_session();
    test_utils::CassFuturePtr connect_future(cass_cluster_connect(inst.cluster));
    test_utils::wait_and_check_error(connect_future.get());
    session = cass_future_get_session(connect_future.get());
  }

  void close_session() {
    if (session != NULL) {
      test_utils::CassFuturePtr close_future(cass_session_close(session));
      cass_future_wait(close_future.get());
      session = NULL;
    }
  }

  test_utils::MultipleNodesTest inst;
  test_utils::LogData log_counter;
  CassSession* session;
};

BOOST_FIXTURE_TEST_SUITE(schema_agreement, ClusterInit)

// only doing a keyspace for now since there is no difference for types or tables
BOOST_AUTO_TEST_CASE(keyspace_add_drop)
{
  log_counter.reset("Found schema agreement in");

  // "USE" in fast succession would normally fail on the next node if the previous query did not wait
  const std::string use_simple = str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE);
  test_utils::execute_query(session, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT)
                                         % test_utils::SIMPLE_KEYSPACE % 2));
  test_utils::execute_query(session, use_simple);
  test_utils::execute_query(session, "USE system");
  test_utils::execute_query(session, str(boost::format("DROP KEYSPACE %s") % test_utils::SIMPLE_KEYSPACE));
  test_utils::CassResultPtr result;
  CassError rc = test_utils::execute_query_with_error(session, use_simple, &result);
  BOOST_CHECK_EQUAL(rc, CASS_ERROR_SERVER_INVALID_QUERY);

  // close session to flush logger
  close_session();

  BOOST_CHECK_EQUAL(log_counter.message_count, 2ul);
}

BOOST_AUTO_TEST_CASE(agreement_node_down) {
  log_counter.reset("ControlConnection: Node 127.0.0." + inst.conf.ip_prefix() + " is down");

  inst.ccm->stop(3);

  size_t t = 0;
  size_t max_tries = 15;
  for (; t < max_tries; ++t) {
    boost::this_thread::sleep_for(boost::chrono::seconds(1));
    if (log_counter.message_count > 0) break;
  }
  BOOST_REQUIRE_MESSAGE(t < max_tries, "Timed out waiting for node down log message");

  log_counter.reset("Found schema agreement in");<commit_msg>Fixing schema agreement (node down) integration test to work with remote C* instances<commit_after>/*
  Copyright (c) 2014 DataStax

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE cassandra
#endif

#include "cassandra.h"
#include "test_utils.hpp"
#include "cql_ccm_bridge.hpp"

#include <boost/format.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>

#include <algorithm>

struct ClusterInit {
  ClusterInit()
    : inst(3, 0),
      log_counter("", true),
      session(NULL) {
    cass_cluster_set_log_level(inst.cluster, CASS_LOG_DEBUG);
    cass_cluster_set_log_callback(inst.cluster, test_utils::count_message_log_callback, &log_counter);
    new_session();
  }

  ~ClusterInit() {
    close_session();
  }

  void new_session() {
    close_session();
    test_utils::CassFuturePtr connect_future(cass_cluster_connect(inst.cluster));
    test_utils::wait_and_check_error(connect_future.get());
    session = cass_future_get_session(connect_future.get());
  }

  void close_session() {
    if (session != NULL) {
      test_utils::CassFuturePtr close_future(cass_session_close(session));
      cass_future_wait(close_future.get());
      session = NULL;
    }
  }

  test_utils::MultipleNodesTest inst;
  test_utils::LogData log_counter;
  CassSession* session;
};

BOOST_FIXTURE_TEST_SUITE(schema_agreement, ClusterInit)

// only doing a keyspace for now since there is no difference for types or tables
BOOST_AUTO_TEST_CASE(keyspace_add_drop)
{
  log_counter.reset("Found schema agreement in");

  // "USE" in fast succession would normally fail on the next node if the previous query did not wait
  const std::string use_simple = str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE);
  test_utils::execute_query(session, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT)
                                         % test_utils::SIMPLE_KEYSPACE % 2));
  test_utils::execute_query(session, use_simple);
  test_utils::execute_query(session, "USE system");
  test_utils::execute_query(session, str(boost::format("DROP KEYSPACE %s") % test_utils::SIMPLE_KEYSPACE));
  test_utils::CassResultPtr result;
  CassError rc = test_utils::execute_query_with_error(session, use_simple, &result);
  BOOST_CHECK_EQUAL(rc, CASS_ERROR_SERVER_INVALID_QUERY);

  // close session to flush logger
  close_session();

  BOOST_CHECK_EQUAL(log_counter.message_count, 2ul);
}

BOOST_AUTO_TEST_CASE(agreement_node_down) {
  log_counter.reset("ControlConnection: Node " + inst.conf.ip_prefix() + "3 is down");

  inst.ccm->stop(3);

  size_t t = 0;
  size_t max_tries = 15;
  for (; t < max_tries; ++t) {
    boost::this_thread::sleep_for(boost::chrono::seconds(1));
    if (log_counter.message_count > 0) break;
  }
  BOOST_REQUIRE_MESSAGE(t < max_tries, "Timed out waiting for node down log message");

  log_counter.reset("Found schema agreement in");
{'fraction_non_alphanumeric': 0.10553859572612298, 'fraction_numerical': 0.006105538595726123, 'mean_word_length': 4.870307167235495, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6884036', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2182, 'n_words': 533}