Skip to content

Commit 5ed14a4

Browse files
committed
IT: reimplement get_attempted_hosts_from_future
`get_attempted_hosts_from_future` was originally implemented in C++ using the internal C++ Driver API. This commit reimplements it using the new exposed Rust API, namely `testing_future_get_attempted_hosts`. To finish up, the commit also reimplements `set_record_attempted_hosts`, which is tightly coupled with the previous function, using `testing_statement_set_recording_history_listener` (introduced a couple of commits ago). This completes the re-implementation of this testing functionality in Rust. I'd like to enable some tests that depend on this functionality, but none of them has all the requirements met yet. Once I rework the DCAware LBP in a follow-up, I'll be able to enable its tests thanks to this commit.
1 parent 0471a6a commit 5ed14a4

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

src/testing.cpp

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@
1616

1717
#include "testing.hpp"
1818

19-
#include "address.hpp"
20-
#include "external.hpp"
2119
#include "get_time.hpp"
22-
#include "logger.hpp"
2320
#include "murmur3.hpp"
24-
#include <iostream>
21+
#include <cstring>
22+
#include <sstream>
2523

2624
extern "C" {
2725
#include "testing_rust_impls.h"
2826
}
2927

3028
namespace datastax { namespace internal { namespace testing {
3129

32-
using namespace core;
33-
3430
String get_host_from_future(CassFuture* future) {
3531
char* host;
3632
size_t host_length;
@@ -51,7 +47,44 @@ String get_host_from_future(CassFuture* future) {
5147
}
5248

5349
StringVec get_attempted_hosts_from_future(CassFuture* future) {
54-
throw std::runtime_error("Unimplemented 'get_attempted_hosts_from_future'!");
50+
// Original implementation commented out for reference.
51+
/*
52+
if (future->type() != Future::FUTURE_TYPE_RESPONSE) {
53+
return StringVec();
54+
}
55+
56+
StringVec attempted_hosts;
57+
ResponseFuture* response_future = static_cast<ResponseFuture*>(future->from());
58+
59+
AddressVec attempted_addresses = response_future->attempted_addresses();
60+
for (const auto& address : attempted_addresses) {
61+
attempted_hosts.push_back(address.to_string());
62+
}
63+
std::sort(attempted_hosts.begin(), attempted_hosts.end());
64+
return attempted_hosts;
65+
*/
66+
67+
StringVec attempted_hosts;
68+
69+
char* const concatenated_attempted_addresses = testing_future_get_attempted_hosts(future);
70+
71+
std::string concatenated_addresses = concatenated_attempted_addresses;
72+
std::stringstream stream{concatenated_addresses};
73+
while (true) {
74+
String address;
75+
if (std::getline(stream, address, '\n')) {
76+
if (!address.empty()) {
77+
attempted_hosts.push_back(address);
78+
}
79+
} else {
80+
break; // No more addresses to read.
81+
}
82+
}
83+
std::sort(attempted_hosts.begin(), attempted_hosts.end());
84+
85+
testing_free_cstring(concatenated_attempted_addresses);
86+
87+
return attempted_hosts;
5588
}
5689

5790
unsigned get_connect_timeout_from_cluster(CassCluster* cluster) {
@@ -110,7 +143,7 @@ String get_server_name(CassFuture* future) {
110143
}
111144

112145
void set_record_attempted_hosts(CassStatement* statement, bool enable) {
113-
throw std::runtime_error("Unimplemented 'set_record_attempted_hosts'!");
146+
testing_statement_set_recording_history_listener(statement, (cass_bool_t) enable);
114147
}
115148

116149
void set_sleeping_history_listener_on_statement(CassStatement* statement, uint64_t sleep_time_ms) {

src/testing_rust_impls.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ CASS_EXPORT void testing_batch_set_sleeping_history_listener(CassBatch *batch,
4040
cass_uint64_t sleep_time_ms);
4141
}
4242

43+
// Sets a recording history listener on the statement.
44+
// This can be used to collect addresses of hosts attempted during statement execution.
45+
// Those can be later retrieved using `testing_future_get_attempted_hosts`.
46+
CASS_EXPORT void testing_statement_set_recording_history_listener(CassStatement *statement,
47+
cass_bool_t enable);
48+
49+
// Retrieves a concatenated string of attempted hosts from the future.
50+
// Hosts are delimited with '\n' character.
51+
// Hosts are recorded only if `testing_statement_set_recording_history_listener`
52+
// was called on the statement previously.
53+
// The returned pointer is allocated and must be freed with `testing_free_cstring`.
54+
CASS_EXPORT char* testing_future_get_attempted_hosts(CassFuture *future);
55+
4356
/**
4457
* Creates a new ignoring retry policy.
4558
*

0 commit comments

Comments
 (0)