Skip to content

Commit 77efd8d

Browse files
authored
CPP-843 - Port logging tests to Google test framework (#326)
1 parent 10d60ef commit 77efd8d

File tree

5 files changed

+204
-159
lines changed

5 files changed

+204
-159
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 "integration.hpp"
18+
19+
class LoggingTests : public Integration {
20+
public:
21+
LoggingTests() { is_ccm_requested_ = false; }
22+
23+
static void log(const CassLogMessage* log, void* data) {
24+
bool* is_triggered = static_cast<bool*>(data);
25+
*is_triggered = true;
26+
}
27+
};
28+
29+
/**
30+
* Ensure the driver is calling the client logging callback
31+
*/
32+
CASSANDRA_INTEGRATION_TEST_F(LoggingTests, Callback) {
33+
CHECK_FAILURE;
34+
35+
bool is_triggered = false;
36+
cass_log_set_callback(LoggingTests::log, &is_triggered);
37+
default_cluster().connect("", false);
38+
EXPECT_TRUE(is_triggered);
39+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 "unit.hpp"
18+
19+
#include "session.hpp"
20+
21+
using datastax::String;
22+
using datastax::internal::get_time_monotonic_ns;
23+
using datastax::internal::core::Address;
24+
using datastax::internal::core::Config;
25+
using datastax::internal::core::Future;
26+
using datastax::internal::core::Session;
27+
using datastax::internal::core::StringMultimap;
28+
using mockssandra::Options;
29+
using test::Utils;
30+
31+
class LoggingUnitTest : public Unit {
32+
public:
33+
void TearDown() {
34+
EXPECT_TRUE(session_.close()->wait_for(WAIT_FOR_TIME));
35+
36+
Unit::TearDown();
37+
}
38+
39+
Future::Ptr connect_async(const Config& config = Config()) {
40+
Config temp(config);
41+
temp.contact_points().push_back(Address("127.0.0.1", 9042));
42+
return session_.connect(temp);
43+
}
44+
45+
bool wait_for_logger(int expected_count) {
46+
// Wait for about 60 seconds
47+
for (int i = 0; i < 600 && logging_criteria_count() < expected_count; ++i) {
48+
Utils::msleep(100);
49+
}
50+
return logging_criteria_count() >= expected_count;
51+
}
52+
53+
private:
54+
Session session_;
55+
};
56+
57+
/**
58+
* On a new control connection, logger message severity should be high (e.g.
59+
* CASS_LOG_ERROR).
60+
*
61+
* @jira_ticket CPP-337
62+
* @since 2.4.0
63+
*/
64+
TEST_F(LoggingUnitTest, ControlConnectionSeverityHigh) {
65+
add_logging_critera("Unable to establish a control connection to host 127.0.0.1", CASS_LOG_ERROR);
66+
Future::Ptr connect_future = connect_async();
67+
EXPECT_TRUE(connect_future->wait_for(WAIT_FOR_TIME));
68+
EXPECT_EQ(1u, logging_criteria_count());
69+
}
70+
71+
/**
72+
* On a established control connection, logger message severity should be reduced
73+
* (e.g. CASS_LOG_WARN).
74+
*
75+
* @jira_ticket CPP-337
76+
* @since 2.4.0
77+
*/
78+
TEST_F(LoggingUnitTest, ControlConnectionSeverityReduced) {
79+
mockssandra::SimpleCluster cluster(simple());
80+
ASSERT_EQ(cluster.start_all(), 0);
81+
82+
add_logging_critera("Lost control connection to host 127.0.0.1", CASS_LOG_WARN);
83+
Future::Ptr connect_future = connect_async();
84+
EXPECT_TRUE(connect_future->wait_for(WAIT_FOR_TIME));
85+
EXPECT_EQ(0u, logging_criteria_count());
86+
cluster.stop_all();
87+
EXPECT_TRUE(wait_for_logger(1));
88+
}
89+
90+
/**
91+
* On a established connection the first connection pool logger message severity should be high
92+
* while subsequent messages should have reduced severity.
93+
*
94+
* @jira_ticket CPP-337
95+
* @since 2.4.0
96+
*/
97+
TEST_F(LoggingUnitTest, ConnectionPoolSeverityReduced) {
98+
mockssandra::SimpleCluster cluster(simple(), 2);
99+
ASSERT_EQ(cluster.start(1), 0);
100+
101+
add_logging_critera(
102+
"Connection pool was unable to connect to host 127.0.0.2 because of the following error",
103+
CASS_LOG_ERROR);
104+
Config config;
105+
config.set_connection_heartbeat_interval_secs(1);
106+
config.set_connection_idle_timeout_secs(1);
107+
config.set_request_timeout(1000);
108+
config.set_constant_reconnect(100);
109+
Future::Ptr connect_future = connect_async(config);
110+
EXPECT_TRUE(connect_future->wait_for(WAIT_FOR_TIME));
111+
EXPECT_TRUE(wait_for_logger(1));
112+
113+
reset_logging_criteria();
114+
add_logging_critera(
115+
"Connection pool was unable to reconnect to host 127.0.0.2 because of the following error",
116+
CASS_LOG_WARN);
117+
EXPECT_TRUE(wait_for_logger(1));
118+
}

cpp-driver/gtests/src/unit/unit.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
#include "unit.hpp"
1717

18+
#include "scoped_lock.hpp"
19+
1820
using namespace datastax;
1921
using namespace datastax::internal;
2022
using namespace datastax::internal::core;
@@ -99,11 +101,13 @@ Unit::Unit()
99101
, logging_criteria_count_(0) {
100102
Logger::set_log_level(CASS_LOG_TRACE);
101103
Logger::set_callback(on_log, this);
104+
uv_mutex_init(&mutex_);
102105
}
103106

104107
Unit::~Unit() {
105108
Logger::set_log_level(CASS_LOG_DISABLED);
106109
Logger::set_callback(NULL, NULL);
110+
uv_mutex_destroy(&mutex_);
107111
}
108112

109113
void Unit::set_output_log_level(CassLogLevel output_log_level) {
@@ -132,9 +136,22 @@ ConnectionSettings Unit::use_ssl(mockssandra::Cluster* cluster, const String& cn
132136
return settings;
133137
}
134138

135-
void Unit::add_logging_critera(const String& criteria) { logging_criteria_.push_back(criteria); }
139+
void Unit::add_logging_critera(const String& criteria,
140+
CassLogLevel severity /*= CASS_LOG_LAST_ENTRY*/) {
141+
ScopedMutex l(&mutex_);
142+
logging_criteria_[severity].push_back(criteria);
143+
}
136144

137-
int Unit::logging_criteria_count() { return logging_criteria_count_.load(); }
145+
int Unit::logging_criteria_count() {
146+
ScopedMutex l(&mutex_);
147+
return logging_criteria_count_;
148+
}
149+
150+
void Unit::reset_logging_criteria() {
151+
ScopedMutex l(&mutex_);
152+
logging_criteria_.clear();
153+
logging_criteria_count_ = 0;
154+
}
138155

139156
void Unit::on_log(const CassLogMessage* message, void* data) {
140157
Unit* instance = static_cast<Unit*>(data);
@@ -148,11 +165,18 @@ void Unit::on_log(const CassLogMessage* message, void* data) {
148165
}
149166

150167
// Determine if the log message matches any of the criteria
151-
for (Vector<String>::const_iterator it = instance->logging_criteria_.begin(),
152-
end = instance->logging_criteria_.end();
153-
it != end; ++it) {
154-
if (strstr(message->message, it->c_str()) != NULL) {
155-
instance->logging_criteria_count_.fetch_add(1);
168+
ScopedMutex l(&instance->mutex_);
169+
for (Map<CassLogLevel, Vector<String> >::iterator level_it = instance->logging_criteria_.begin(),
170+
level_end = instance->logging_criteria_.end();
171+
level_it != level_end; ++level_it) {
172+
CassLogLevel level = level_it->first;
173+
Vector<String> criteria = level_it->second;
174+
for (Vector<String>::const_iterator it = criteria.begin(), end = criteria.end(); it != end;
175+
++it) {
176+
if ((level == CASS_LOG_LAST_ENTRY || level == message->severity) &&
177+
strstr(message->message, it->c_str()) != NULL) {
178+
++instance->logging_criteria_count_;
179+
}
156180
}
157181
}
158182
}

cpp-driver/gtests/src/unit/unit.hpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,24 @@ class Unit : public testing::Test {
209209
const datastax::String& cn = "");
210210

211211
/**
212-
* Add criteria to the search criteria for incoming log messages
212+
* Add criteria to the search criteria for incoming log messages.
213213
*
214-
* @param criteria Criteria to add
214+
* @param criteria Criteria to add.
215+
* @param severity Severity level to match (Optional: defaults to any severity).
215216
*/
216-
void add_logging_critera(const datastax::String& criteria);
217+
void add_logging_critera(const String& criteria, CassLogLevel severity = CASS_LOG_LAST_ENTRY);
217218
/**
218-
* Get the number of log messages that matched the search criteria
219+
* Get the number of log messages that matched the search criteria.
219220
*
220-
* @return Number of matched log messages
221+
* @return Number of matched log messages.
221222
*/
222223
int logging_criteria_count();
223224

225+
/**
226+
* Reset the logging criteria; clears all criteria and resets count.
227+
*/
228+
void reset_logging_criteria();
229+
224230
private:
225231
/**
226232
* Log the message from the driver (callback).
@@ -232,8 +238,11 @@ class Unit : public testing::Test {
232238

233239
private:
234240
CassLogLevel output_log_level_;
235-
datastax::internal::Vector<datastax::String> logging_criteria_;
236-
datastax::internal::Atomic<int> logging_criteria_count_;
241+
datastax::internal::Map<CassLogLevel, datastax::internal::Vector<datastax::String> >
242+
logging_criteria_;
243+
int logging_criteria_count_;
244+
uv_mutex_t mutex_;
245+
uint64_t start_time_;
237246
};
238247

239248
#endif // UNIT_TEST_HPP

0 commit comments

Comments
 (0)