Skip to content

Commit 5529a6b

Browse files
authored
CPP-688 - Abstract protocol handling (#433)
1 parent 4d80893 commit 5529a6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+524
-161
lines changed

cmake/modules/Gtest.cmake

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ endmacro()
172172
# Configure unit tests to be built.
173173
#
174174
# Arguments:
175-
# project_name - Name of project that has Google Test unit tests.
176-
# extra_files - List of extra files to build into the test executable, if
177-
# any.
178-
# extra_includes - List of extra includes to include into the text
179-
# executable, if any.
175+
# project_name - Name of project that has Google Test unit tests.
176+
# extra_files - List of extra files to build into the test executable, if
177+
# any.
178+
# extra_includes - List of extra includes to include into the text
179+
# executable, if any.
180+
# excluded_test_files - List of test files to exclude from the build.
180181
#------------------------
181-
macro(GtestUnitTests project_name extra_files extra_includes)
182+
macro(GtestUnitTests project_name extra_files extra_includes excluded_test_files)
182183
set(UNIT_TESTS_NAME "${project_name}-unit-tests")
183184
set(UNIT_TESTS_DISPLAY_NAME "Unit Tests (${project_name})")
184185
set(UNIT_TESTS_SOURCE_DIR "${TESTS_SOURCE_DIR}/unit")
@@ -193,6 +194,11 @@ macro(GtestUnitTests project_name extra_files extra_includes)
193194
file(GLOB UNIT_TESTS_INCLUDE_FILES ${UNIT_TESTS_SOURCE_DIR}/*.hpp )
194195
file(GLOB UNIT_TESTS_SOURCE_FILES ${UNIT_TESTS_SOURCE_DIR}/*.cpp)
195196
file(GLOB UNIT_TESTS_TESTS_SOURCE_FILES ${UNIT_TESTS_SOURCE_DIR}/tests/*.cpp)
197+
foreach(excluded_test_file ${excluded_test_files})
198+
list(REMOVE_ITEM
199+
UNIT_TESTS_TESTS_SOURCE_FILES
200+
"${UNIT_TESTS_SOURCE_DIR}/tests/${excluded_test_file}")
201+
endforeach()
196202
source_group("Header Files" FILES ${UNIT_TESTS_INCLUDE_FILES})
197203
source_group("Source Files" FILES ${UNIT_TESTS_SOURCE_FILES})
198204
source_group("Source Files\\tests" FILES ${UNIT_TESTS_TESTS_SOURCE_FILES})

gtests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ endif()
124124
# Unit test executable
125125
#------------------------------
126126
if(CASS_BUILD_UNIT_TESTS)
127-
GtestUnitTests("cassandra" "" "")
127+
GtestUnitTests("cassandra" "" "" "${CASS_EXCLUDED_UNIT_TEST_FILES}")
128128
endif()

gtests/src/unit/tests/test_decoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class TestDecoder : public cass::Decoder {
2424
public:
2525
TestDecoder(const char* input, size_t length,
26-
int protocol_version = CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION)
26+
cass::ProtocolVersion protocol_version = cass::ProtocolVersion::highest_supported())
2727
: cass::Decoder(input, length, protocol_version) { }
2828

2929
inline const char* buffer() const { return cass::Decoder::buffer(); }
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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 <gtest/gtest.h>
18+
19+
#include "unit.hpp"
20+
21+
#include "cassandra.h"
22+
#include "protocol.hpp"
23+
24+
using cass::ProtocolVersion;
25+
26+
class ProtocolVersionUnitTest : public Unit { };
27+
28+
TEST_F(ProtocolVersionUnitTest, LowestSupported) {
29+
EXPECT_EQ(ProtocolVersion(CASS_PROTOCOL_VERSION_V3),
30+
ProtocolVersion::lowest_supported());
31+
}
32+
33+
TEST_F(ProtocolVersionUnitTest, HighestSupported) {
34+
EXPECT_EQ(ProtocolVersion(CASS_PROTOCOL_VERSION_V4),
35+
ProtocolVersion::highest_supported());
36+
}
37+
38+
TEST_F(ProtocolVersionUnitTest, NewestBeta) {
39+
EXPECT_EQ(ProtocolVersion(CASS_PROTOCOL_VERSION_V5),
40+
ProtocolVersion::newest_beta());
41+
}
42+
43+
44+
TEST_F(ProtocolVersionUnitTest, IsValid) {
45+
{ // Invalid
46+
ProtocolVersion invalid;
47+
EXPECT_FALSE(invalid.is_valid());
48+
}
49+
50+
{ // Invalid
51+
ProtocolVersion zero(0);
52+
EXPECT_FALSE(zero.is_valid());
53+
}
54+
55+
{ // Invalid (no longer supported)
56+
ProtocolVersion v1(CASS_PROTOCOL_VERSION_V1);
57+
EXPECT_FALSE(v1.is_valid());
58+
}
59+
60+
{ // Invalid (no longer supported)
61+
ProtocolVersion v2(CASS_PROTOCOL_VERSION_V2);
62+
EXPECT_FALSE(v2.is_valid());
63+
}
64+
65+
{ // Valid
66+
ProtocolVersion v3(CASS_PROTOCOL_VERSION_V3);
67+
EXPECT_TRUE(v3.is_valid());
68+
}
69+
70+
{ // Valid
71+
ProtocolVersion v4(CASS_PROTOCOL_VERSION_V4);
72+
EXPECT_TRUE(v4.is_valid());
73+
}
74+
75+
{ // Invalid (beta version)
76+
ProtocolVersion v5(CASS_PROTOCOL_VERSION_V5);
77+
EXPECT_FALSE(v5.is_valid());
78+
}
79+
80+
{ // Invalid (DSE version)
81+
ProtocolVersion vDSE1(CASS_PROTOCOL_VERSION_DSEV1);
82+
EXPECT_FALSE(vDSE1.is_valid());
83+
}
84+
85+
{ // Invalid (DSE version)
86+
ProtocolVersion vDSE2(CASS_PROTOCOL_VERSION_DSEV2);
87+
EXPECT_FALSE(vDSE2.is_valid());
88+
}
89+
}
90+
91+
TEST_F(ProtocolVersionUnitTest, IsBeta) {
92+
{ // Not valid beta
93+
ProtocolVersion invalid;
94+
EXPECT_FALSE(invalid.is_beta());
95+
}
96+
97+
{ // Valid beta
98+
ProtocolVersion v5(CASS_PROTOCOL_VERSION_V5);
99+
EXPECT_TRUE(v5.is_beta());
100+
}
101+
102+
{ // Not valid beta (DSE version)
103+
ProtocolVersion vDSE2(CASS_PROTOCOL_VERSION_DSEV2);
104+
EXPECT_FALSE(vDSE2.is_beta());
105+
}
106+
}
107+
108+
TEST_F(ProtocolVersionUnitTest, ToString) {
109+
{
110+
ProtocolVersion invalid;
111+
EXPECT_EQ(invalid.to_string(), "<invalid>");
112+
}
113+
114+
{
115+
ProtocolVersion v4(CASS_PROTOCOL_VERSION_V4);
116+
EXPECT_EQ(v4.to_string(), "v4");
117+
}
118+
119+
{
120+
ProtocolVersion v5(CASS_PROTOCOL_VERSION_V5);
121+
EXPECT_EQ(v5.to_string(), "v5");
122+
}
123+
124+
{
125+
ProtocolVersion DSEv1(CASS_PROTOCOL_VERSION_DSEV1);
126+
EXPECT_EQ(DSEv1.to_string(), "DSEv1");
127+
}
128+
}
129+
130+
TEST_F(ProtocolVersionUnitTest, AttemptLowerSupported) {
131+
ProtocolVersion version(CASS_PROTOCOL_VERSION_V4);
132+
EXPECT_EQ(ProtocolVersion(CASS_PROTOCOL_VERSION_V4), version);
133+
134+
EXPECT_TRUE(version.attempt_lower_supported("127.0.0.1"));
135+
EXPECT_EQ(ProtocolVersion(CASS_PROTOCOL_VERSION_V3), version);
136+
137+
EXPECT_FALSE(version.attempt_lower_supported("127.0.0.1"));
138+
}
139+
140+
TEST_F(ProtocolVersionUnitTest, SupportsSetKeyspace) {
141+
{ // Supported
142+
ProtocolVersion v5(CASS_PROTOCOL_VERSION_V5);
143+
EXPECT_TRUE(v5.supports_set_keyspace());
144+
}
145+
146+
{ // Not supported
147+
for (int i = CASS_PROTOCOL_VERSION_V1; i <= CASS_PROTOCOL_VERSION_V4; ++i) {
148+
ProtocolVersion version(i);
149+
EXPECT_FALSE(version.supports_set_keyspace());
150+
}
151+
}
152+
}
153+
154+
TEST_F(ProtocolVersionUnitTest, SupportsResultMetadataId) {
155+
for (int i = CASS_PROTOCOL_VERSION_V1; i <= CASS_PROTOCOL_VERSION_V5; ++i) {
156+
ProtocolVersion version(i);
157+
EXPECT_FALSE(version.supports_result_metadata_id());
158+
}
159+
}

gtests/src/unit/tests/test_session_base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class TestSessionBase : public cass::SessionBase {
4141

4242
protected:
4343
virtual void on_connect(const cass::Host::Ptr& connected_host,
44-
int protocol_version,
44+
cass::ProtocolVersion protocol_version,
4545
const cass::HostMap& hosts,
4646
const cass::TokenMap::Ptr& token_map) {
4747
++connected_;
4848
ASSERT_STREQ("127.0.0.1", connected_host->address_string().c_str());
49-
ASSERT_EQ(PROTOCOL_VERSION, protocol_version);
49+
ASSERT_EQ(cass::ProtocolVersion(PROTOCOL_VERSION), protocol_version);
5050
ASSERT_EQ(1, hosts.size());
5151
ASSERT_EQ(state(), SESSION_STATE_CONNECTING);
5252
notify_connected();

include/cassandra.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,11 @@ typedef enum CassProtocolVersion_ {
626626
CASS_PROTOCOL_VERSION_V2 = 0x02, /**< Deprecated */
627627
CASS_PROTOCOL_VERSION_V3 = 0x03,
628628
CASS_PROTOCOL_VERSION_V4 = 0x04,
629-
CASS_PROTOCOL_VERSION_V5 = 0x05
629+
CASS_PROTOCOL_VERSION_V5 = 0x05,
630+
CASS_PROTOCOL_VERSION_DSEV1 = 0x41, /**< Only supported when using the DSE
631+
driver with DataStax Enterprise */
632+
CASS_PROTOCOL_VERSION_DSEV2 = 0x42 /**< Only supported when using the DSE
633+
driver with DataStax Enterprise */
630634
} CassProtocolVersion;
631635

632636
typedef enum CassErrorSource_ {
@@ -1549,10 +1553,11 @@ cass_cluster_set_authenticator_callbacks(CassCluster* cluster,
15491553
void* data);
15501554

15511555
/**
1552-
* Sets the protocol version. This will automatically downgrade to the lowest
1556+
* Sets the protocol version. The driver will automatically downgrade to the lowest
15531557
* supported protocol version.
15541558
*
1555-
* <b>Default:</b> CASS_PROTOCOL_VERSION_V4
1559+
* <b>Default:</b> CASS_PROTOCOL_VERSION_V4 or CASS_PROTOCOL_VERSION_DSEV1 when
1560+
* using the DSE driver with DataStax Enterprise.
15561561
*
15571562
* @public @memberof CassCluster
15581563
*
@@ -1568,7 +1573,8 @@ cass_cluster_set_protocol_version(CassCluster* cluster,
15681573

15691574
/**
15701575
* Use the newest beta protocol version. This currently enables the use of
1571-
* protocol version v5 (CASS_PROTOCOL_VERSION_V5).
1576+
* protocol version v5 (CASS_PROTOCOL_VERSION_V5) or DSEv2 (CASS_PROTOCOL_VERSION_DSEV2)
1577+
* when using the DSE driver with DataStax Enterprise.
15721578
*
15731579
* <b>Default:</b> cass_false
15741580
*

src/auth_requests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace cass {
2020

21-
int AuthResponseRequest::encode(int version, RequestCallback* callback, BufferVec* bufs) const {
21+
int AuthResponseRequest::encode(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const {
2222
// <token> [bytes]
2323
size_t length = sizeof(int32_t) + token_.size();
2424

src/auth_requests.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AuthResponseRequest : public Request {
3535
const Authenticator::Ptr& auth() const { return auth_; }
3636

3737
private:
38-
int encode(int version, RequestCallback* callback, BufferVec* bufs) const;
38+
int encode(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const;
3939

4040
private:
4141
String token_;

src/batch_request.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace cass {
126126
// <flags> is a [byte] (or [int] for protocol v5)
127127
// <serial_consistency> is a [short]
128128
// <timestamp> is a [long]
129-
int BatchRequest::encode(int version, RequestCallback* callback, BufferVec* bufs) const {
129+
int BatchRequest::encode(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const {
130130
int length = 0;
131131
uint32_t flags = 0;
132132

@@ -179,7 +179,7 @@ int BatchRequest::encode(int version, RequestCallback* callback, BufferVec* bufs
179179
flags |= CASS_QUERY_FLAG_DEFAULT_TIMESTAMP;
180180
}
181181

182-
if (supports_set_keyspace(version) && !keyspace().empty()) {
182+
if (version.supports_set_keyspace() && !keyspace().empty()) {
183183
buf_size += sizeof(uint16_t) + keyspace().size();
184184
flags |= CASS_QUERY_FLAG_WITH_KEYSPACE;
185185
}
@@ -201,7 +201,7 @@ int BatchRequest::encode(int version, RequestCallback* callback, BufferVec* bufs
201201
pos = buf.encode_int64(pos, callback->timestamp());
202202
}
203203

204-
if (supports_set_keyspace(version) && !keyspace().empty()) {
204+
if (version.supports_set_keyspace() && !keyspace().empty()) {
205205
pos = buf.encode_string(pos, keyspace().data(), keyspace().size());
206206
}
207207

src/batch_request.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class BatchRequest : public RoutableRequest {
5050
virtual bool get_routing_key(String* routing_key) const;
5151

5252
private:
53-
int encode(int version, RequestCallback* callback, BufferVec* bufs) const;
53+
int encode(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const;
5454

5555
private:
5656
uint8_t type_;

0 commit comments

Comments
 (0)