1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "testing.hpp"
18 
19 #include "address.hpp"
20 #include "cluster_config.hpp"
21 #include "external.hpp"
22 #include "future.hpp"
23 #include "get_time.hpp"
24 #include "logger.hpp"
25 #include "metadata.hpp"
26 #include "murmur3.hpp"
27 #include "request_handler.hpp"
28 #include "result_response.hpp"
29 #include "session.hpp"
30 #include "statement.hpp"
31 
32 namespace datastax { namespace internal { namespace testing {
33 
34 using namespace core;
35 
get_host_from_future(CassFuture * future)36 String get_host_from_future(CassFuture* future) {
37   if (future->type() != Future::FUTURE_TYPE_RESPONSE) {
38     return "";
39   }
40   ResponseFuture* response_future = static_cast<ResponseFuture*>(future->from());
41   return response_future->address().hostname_or_address();
42 }
43 
get_attempted_hosts_from_future(CassFuture * future)44 StringVec get_attempted_hosts_from_future(CassFuture* future) {
45   if (future->type() != Future::FUTURE_TYPE_RESPONSE) {
46     return StringVec();
47   }
48   StringVec attempted_hosts;
49   ResponseFuture* response_future = static_cast<ResponseFuture*>(future->from());
50 
51   AddressVec attempted_addresses = response_future->attempted_addresses();
52   for (AddressVec::iterator it = attempted_addresses.begin(); it != attempted_addresses.end();
53        ++it) {
54     attempted_hosts.push_back(it->to_string());
55   }
56   std::sort(attempted_hosts.begin(), attempted_hosts.end());
57   return attempted_hosts;
58 }
59 
get_connect_timeout_from_cluster(CassCluster * cluster)60 unsigned get_connect_timeout_from_cluster(CassCluster* cluster) {
61   return cluster->config().connect_timeout_ms();
62 }
63 
get_port_from_cluster(CassCluster * cluster)64 int get_port_from_cluster(CassCluster* cluster) { return cluster->config().port(); }
65 
get_contact_points_from_cluster(CassCluster * cluster)66 String get_contact_points_from_cluster(CassCluster* cluster) {
67   String str;
68 
69   const AddressVec& contact_points = cluster->config().contact_points();
70 
71   for (AddressVec::const_iterator it = contact_points.begin(), end = contact_points.end();
72        it != end; ++it) {
73     if (str.size() > 0) {
74       str.push_back(',');
75     }
76     str.append(it->hostname_or_address());
77   }
78 
79   return str;
80 }
81 
create_murmur3_hash_from_string(const String & value)82 int64_t create_murmur3_hash_from_string(const String& value) {
83   return MurmurHash3_x64_128(value.data(), value.size(), 0);
84 }
85 
get_time_since_epoch_in_ms()86 uint64_t get_time_since_epoch_in_ms() { return internal::get_time_since_epoch_ms(); }
87 
get_host_latency_average(CassSession * session,String ip_address,int port)88 uint64_t get_host_latency_average(CassSession* session, String ip_address, int port) {
89   Address address(ip_address, port);
90   if (address.is_valid()) {
91     Host::Ptr host(session->cluster()->find_host(address));
92     return host ? host->get_current_average().average : 0;
93   }
94   return 0;
95 }
96 
get_consistency(const CassStatement * statement)97 CassConsistency get_consistency(const CassStatement* statement) {
98   return statement->from()->consistency();
99 }
100 
get_serial_consistency(const CassStatement * statement)101 CassConsistency get_serial_consistency(const CassStatement* statement) {
102   return statement->from()->serial_consistency();
103 }
104 
get_request_timeout_ms(const CassStatement * statement)105 uint64_t get_request_timeout_ms(const CassStatement* statement) {
106   return statement->from()->request_timeout_ms();
107 }
108 
get_retry_policy(const CassStatement * statement)109 const CassRetryPolicy* get_retry_policy(const CassStatement* statement) {
110   return CassRetryPolicy::to(statement->from()->retry_policy().get());
111 }
112 
get_server_name(CassFuture * future)113 String get_server_name(CassFuture* future) {
114   if (future->type() != Future::FUTURE_TYPE_RESPONSE) {
115     return "";
116   }
117   return static_cast<ResponseFuture*>(future->from())->address().server_name();
118 }
119 
set_record_attempted_hosts(CassStatement * statement,bool enable)120 void set_record_attempted_hosts(CassStatement* statement, bool enable) {
121   static_cast<Statement*>(statement->from())->set_record_attempted_addresses(enable);
122 }
123 
124 }}} // namespace datastax::internal::testing
125