Reformat codebase with 120 char limit (#583)

This commit is contained in:
Alex Kremer
2023-04-06 11:24:36 +01:00
committed by GitHub
parent e60fd3e58e
commit d816ef54ab
174 changed files with 5591 additions and 10450 deletions

View File

@@ -38,83 +38,60 @@ TEST_F(BackendCassandraAsyncExecutorTest, CompletionCalledOnSuccess)
auto statement = FakeStatement{};
auto handle = MockHandle{};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([this](auto const&, auto&& cb) {
ctx.post([cb = std::move(cb)]() { cb({}); });
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
AsyncExecutor<FakeStatement, MockHandle>::run(
ctx, handle, statement, [&called, &work](auto&&) {
called = true;
work.reset();
});
AsyncExecutor<FakeStatement, MockHandle>::run(ctx, handle, statement, [&called, &work](auto&&) {
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraAsyncExecutorTest,
ExecutedMultipleTimesByRetryPolicyOnMainThread)
TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnMainThread)
{
auto callCount = std::atomic_int{0};
auto statement = FakeStatement{};
auto handle = MockHandle{};
// emulate successfull execution after some attempts
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([&callCount](auto const&, auto&& cb) {
++callCount;
if (callCount >= 3)
cb({});
else
cb({CassandraError{
"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}});
cb({CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}});
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(3);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
AsyncExecutor<FakeStatement, MockHandle>::run(
ctx, handle, statement, [&called, &work](auto&&) {
called = true;
work.reset();
});
AsyncExecutor<FakeStatement, MockHandle>::run(ctx, handle, statement, [&called, &work](auto&&) {
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(callCount >= 3);
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraAsyncExecutorTest,
ExecutedMultipleTimesByRetryPolicyOnOtherThread)
TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnOtherThread)
{
auto callCount = std::atomic_int{0};
auto statement = FakeStatement{};
@@ -125,37 +102,27 @@ TEST_F(
auto thread = std::thread{[&threadedCtx] { threadedCtx.run(); }};
// emulate successfull execution after some attempts
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([&callCount](auto const&, auto&& cb) {
++callCount;
if (callCount >= 3)
cb({});
else
cb({CassandraError{
"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}});
cb({CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}});
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(3);
auto called = std::atomic_bool{false};
auto work2 = std::optional<boost::asio::io_context::work>{ctx};
AsyncExecutor<FakeStatement, MockHandle>::run(
threadedCtx, handle, statement, [&called, &work, &work2](auto&&) {
called = true;
work.reset();
work2.reset();
});
AsyncExecutor<FakeStatement, MockHandle>::run(threadedCtx, handle, statement, [&called, &work, &work2](auto&&) {
called = true;
work.reset();
work2.reset();
});
ctx.run();
ASSERT_TRUE(callCount >= 3);
@@ -164,30 +131,19 @@ TEST_F(
thread.join();
}
TEST_F(
BackendCassandraAsyncExecutorTest,
CompletionCalledOnFailureAfterRetryCountExceeded)
TEST_F(BackendCassandraAsyncExecutorTest, CompletionCalledOnFailureAfterRetryCountExceeded)
{
auto statement = FakeStatement{};
auto handle = MockHandle{};
// FakeRetryPolicy returns false for shouldRetry in which case we should
// still call onComplete giving it whatever error we have raised internally.
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
cb({CassandraError{
"not a timeout", CASS_ERROR_LIB_INTERNAL_ERROR}});
cb({CassandraError{"not a timeout", CASS_ERROR_LIB_INTERNAL_ERROR}});
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};

File diff suppressed because it is too large Load Diff

View File

@@ -40,8 +40,7 @@ protected:
{
Handle handle{contactPoints};
EXPECT_TRUE(handle.connect());
std::string query = "CREATE KEYSPACE IF NOT EXISTS " +
std::string{keyspace} +
std::string query = "CREATE KEYSPACE IF NOT EXISTS " + std::string{keyspace} +
" WITH replication = {'class': "
"'SimpleStrategy', 'replication_factor': '1'} AND "
"durable_writes = "
@@ -85,8 +84,7 @@ protected:
int64_t idx = 1000;
for (auto const& entry : entries)
statements.push_back(
insert.bind(entry, static_cast<int64_t>(idx++)));
statements.push_back(insert.bind(entry, static_cast<int64_t>(idx++)));
EXPECT_EQ(statements.size(), entries.size());
EXPECT_TRUE(handle.execute(statements));
@@ -107,9 +105,7 @@ TEST_F(BackendCassandraBaseTest, ConnectionFailFormat)
auto f = handle.asyncConnect();
auto res = f.await();
ASSERT_FALSE(res);
EXPECT_EQ(
res.error(),
"No hosts available: Unable to connect to any contact points");
EXPECT_EQ(res.error(), "No hosts available: Unable to connect to any contact points");
EXPECT_EQ(res.error().code(), CASS_ERROR_LIB_NO_HOSTS_AVAILABLE);
}
@@ -124,8 +120,7 @@ TEST_F(BackendCassandraBaseTest, ConnectionFailTimeout)
auto res = f.await();
ASSERT_FALSE(res);
// scylla and cassandra produce different text
EXPECT_TRUE(res.error().message().starts_with(
"No hosts available: Underlying connection error:"));
EXPECT_TRUE(res.error().message().starts_with("No hosts available: Underlying connection error:"));
EXPECT_EQ(res.error().code(), CASS_ERROR_LIB_NO_HOSTS_AVAILABLE);
}
@@ -134,9 +129,7 @@ TEST_F(BackendCassandraBaseTest, FutureCallback)
Handle handle{"127.0.0.1"};
ASSERT_TRUE(handle.connect());
auto const statement =
handle.prepare("SELECT keyspace_name FROM system_schema.keyspaces")
.bind();
auto const statement = handle.prepare("SELECT keyspace_name FROM system_schema.keyspaces").bind();
bool complete = false;
auto f = handle.asyncExecute(statement, [&complete](auto const res) {
@@ -157,24 +150,21 @@ TEST_F(BackendCassandraBaseTest, FutureCallbackSurviveMove)
Handle handle{"127.0.0.1"};
ASSERT_TRUE(handle.connect());
auto const statement =
handle.prepare("SELECT keyspace_name FROM system_schema.keyspaces")
.bind();
auto const statement = handle.prepare("SELECT keyspace_name FROM system_schema.keyspaces").bind();
bool complete = false;
std::vector<FutureWithCallback> futures;
std::binary_semaphore sem{0};
futures.push_back(
handle.asyncExecute(statement, [&complete, &sem](auto const res) {
complete = true;
EXPECT_TRUE(res.value().hasRows());
futures.push_back(handle.asyncExecute(statement, [&complete, &sem](auto const res) {
complete = true;
EXPECT_TRUE(res.value().hasRows());
for (auto [ks] : extract<std::string>(res.value()))
std::cout << "keyspace: " << ks << '\n';
for (auto [ks] : extract<std::string>(res.value()))
std::cout << "keyspace: " << ks << '\n';
sem.release();
}));
sem.release();
}));
sem.acquire();
for (auto const& f : futures)
@@ -252,8 +242,7 @@ TEST_F(BackendCassandraBaseTest, CreateTableWithStrings)
int64_t idx = 1000;
for (auto const& entry : entries)
futures.push_back(handle.asyncExecute(
insert, entry, static_cast<int64_t>(idx++)));
futures.push_back(handle.asyncExecute(insert, entry, static_cast<int64_t>(idx++)));
ASSERT_EQ(futures.size(), entries.size());
for (auto const& f : futures)
@@ -324,8 +313,7 @@ TEST_F(BackendCassandraBaseTest, BatchInsert)
int64_t idx = 1000;
for (auto const& entry : entries)
statements.push_back(
insert.bind(entry, static_cast<int64_t>(idx++)));
statements.push_back(insert.bind(entry, static_cast<int64_t>(idx++)));
ASSERT_EQ(statements.size(), entries.size());
auto rc = handle.execute(statements);
@@ -385,8 +373,7 @@ TEST_F(BackendCassandraBaseTest, AlterTableMoveToNewTable)
// now migrate data; tmp column will just get the sequence number + 1 stored
std::vector<Statement> migrationStatements;
auto const migrationInsert = handle.prepare(
"INSERT INTO strings_v2 (hash, sequence, tmp) VALUES (?, ?, ?)");
auto const migrationInsert = handle.prepare("INSERT INTO strings_v2 (hash, sequence, tmp) VALUES (?, ?, ?)");
auto const res = handle.execute("SELECT hash, sequence FROM strings");
ASSERT_TRUE(res);
@@ -396,8 +383,8 @@ TEST_F(BackendCassandraBaseTest, AlterTableMoveToNewTable)
{
static_assert(std::is_same_v<decltype(hash), std::string>);
static_assert(std::is_same_v<decltype(seq), int64_t>);
migrationStatements.push_back(migrationInsert.bind(
hash, static_cast<int64_t>(seq), static_cast<int64_t>(seq + 1u)));
migrationStatements.push_back(
migrationInsert.bind(hash, static_cast<int64_t>(seq), static_cast<int64_t>(seq + 1u)));
}
EXPECT_TRUE(handle.execute(migrationStatements));

View File

@@ -37,113 +37,82 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineSuccessful)
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statement, auto&& cb) {
cb({}); // pretend we got data
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
strat.read(yield, statement);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
strat.read(yield, statement);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadOneInCoroutineThrowsOnTimeoutFailure)
TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnTimeoutFailure)
{
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
cb({}); // notify that item is ready
return FakeFutureWithCallback{FakeResultOrError{
CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}}};
return FakeFutureWithCallback{
FakeResultOrError{CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}}};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
EXPECT_THROW(strat.read(yield, statement), DatabaseTimeout);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
EXPECT_THROW(strat.read(yield, statement), DatabaseTimeout);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadOneInCoroutineThrowsOnInvalidQueryFailure)
TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnInvalidQueryFailure)
{
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
cb({}); // notify that item is ready
return FakeFutureWithCallback{FakeResultOrError{
CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}}};
return FakeFutureWithCallback{
FakeResultOrError{CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}}};
});
EXPECT_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
EXPECT_THROW(strat.read(yield, statement), std::runtime_error);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statement = FakeStatement{};
EXPECT_THROW(strat.read(yield, statement), std::runtime_error);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
@@ -155,123 +124,96 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineSuccessful)
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_EQ(statements.size(), 3);
cb({}); // pretend we got data
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
strat.read(yield, statements);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
strat.read(yield, statements);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadBatchInCoroutineThrowsOnTimeoutFailure)
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnTimeoutFailure)
{
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_EQ(statements.size(), 3);
cb({}); // notify that item is ready
return FakeFutureWithCallback{FakeResultOrError{
CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}}};
return FakeFutureWithCallback{
FakeResultOrError{CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}}};
});
EXPECT_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.read(yield, statements), DatabaseTimeout);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.read(yield, statements), DatabaseTimeout);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadBatchInCoroutineThrowsOnInvalidQueryFailure)
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnInvalidQueryFailure)
{
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_EQ(statements.size(), 3);
cb({}); // notify that item is ready
return FakeFutureWithCallback{FakeResultOrError{
CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}}};
return FakeFutureWithCallback{
FakeResultOrError{CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}}};
});
EXPECT_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.read(yield, statements), std::runtime_error);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.read(yield, statements), std::runtime_error);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadBatchInCoroutineMarksBusyIfRequestsOutstandingExceeded)
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineMarksBusyIfRequestsOutstandingExceeded)
{
auto handle = MockHandle{};
auto settings = Settings{};
@@ -279,10 +221,7 @@ TEST_F(
auto strat = DefaultExecutionStrategy{settings, handle};
ON_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([&strat](auto const& statements, auto&& cb) {
EXPECT_EQ(statements.size(), 3);
EXPECT_TRUE(strat.isTooBusy()); // 2 was the limit, we sent 3
@@ -291,26 +230,21 @@ TEST_F(
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
EXPECT_FALSE(strat.isTooBusy()); // 2 was the limit, 0 atm
auto statements = std::vector<FakeStatement>(3);
strat.read(yield, statements);
EXPECT_FALSE(
strat.isTooBusy()); // after read completes it's 0 again
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
EXPECT_FALSE(strat.isTooBusy()); // 2 was the limit, 0 atm
auto statements = std::vector<FakeStatement>(3);
strat.read(yield, statements);
EXPECT_FALSE(strat.isTooBusy()); // after read completes it's 0 again
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
@@ -321,11 +255,7 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
cb({}); // pretend we got data
return FakeFutureWithCallback{};
@@ -340,37 +270,29 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
auto res = strat.readEach(yield, statements);
EXPECT_EQ(res.size(), statements.size());
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
auto res = strat.readEach(yield, statements);
EXPECT_EQ(res.size(), statements.size());
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
}
TEST_F(
BackendCassandraExecutionStrategyTest,
ReadEachInCoroutineThrowsOnFailure)
TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineThrowsOnFailure)
{
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
auto callCount = std::atomic_int{0};
ON_CALL(
handle,
asyncExecute(
An<FakeStatement const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([&callCount](auto const&, auto&& cb) {
if (callCount == 1) // error happens on one of the entries
cb({CassandraError{
"invalid data", CASS_ERROR_LIB_INVALID_DATA}});
cb({CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}});
else
cb({}); // pretend we got data
++callCount;
@@ -386,14 +308,13 @@ TEST_F(
auto called = std::atomic_bool{false};
auto work = std::optional<boost::asio::io_context::work>{ctx};
boost::asio::spawn(
ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.readEach(yield, statements), DatabaseTimeout);
boost::asio::spawn(ctx, [&work, &called, &strat](boost::asio::yield_context yield) {
auto statements = std::vector<FakeStatement>(3);
EXPECT_THROW(strat.readEach(yield, statements), DatabaseTimeout);
called = true;
work.reset();
});
called = true;
work.reset();
});
ctx.run();
ASSERT_TRUE(called);
@@ -404,12 +325,9 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteSyncFirstTrySuccessful)
auto handle = MockHandle{};
auto strat = DefaultExecutionStrategy{Settings{}, handle};
ON_CALL(handle, execute(An<FakeStatement const&>()))
.WillByDefault([](auto const&) { return FakeResultOrError{}; });
EXPECT_CALL(
handle,
execute(An<FakeStatement const&>()))
.Times(1); // first one will succeed
ON_CALL(handle, execute(An<FakeStatement const&>())).WillByDefault([](auto const&) { return FakeResultOrError{}; });
EXPECT_CALL(handle,
execute(An<FakeStatement const&>())).Times(1); // first one will succeed
EXPECT_TRUE(strat.writeSync({}));
}
@@ -420,17 +338,13 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteSyncRetrySuccessful)
auto strat = DefaultExecutionStrategy{Settings{}, handle};
auto callCount = 0;
ON_CALL(handle, execute(An<FakeStatement const&>()))
.WillByDefault([&callCount](auto const&) {
if (callCount++ == 1)
return FakeResultOrError{};
return FakeResultOrError{
CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}};
});
EXPECT_CALL(
handle,
execute(An<FakeStatement const&>()))
.Times(2); // first one will fail, second will succeed
ON_CALL(handle, execute(An<FakeStatement const&>())).WillByDefault([&callCount](auto const&) {
if (callCount++ == 1)
return FakeResultOrError{};
return FakeResultOrError{CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}};
});
EXPECT_CALL(handle,
execute(An<FakeStatement const&>())).Times(2); // first one will fail, second will succeed
EXPECT_TRUE(strat.writeSync({}));
}
@@ -446,10 +360,7 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
auto thread = std::thread{[this]() { ctx.run(); }};
ON_CALL(
handle,
asyncExecute(
An<std::vector<FakeStatement> const&>(),
An<std::function<void(FakeResultOrError)>&&>()))
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([this, &callCount](auto const&, auto&& cb) {
// run on thread to emulate concurrency model of real asyncExecute
boost::asio::post(ctx, [&callCount, cb = std::move(cb)] {
@@ -469,7 +380,7 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
for (auto i = 0u; i < totalRequests; ++i)
strat.write(statements);
strat.sync(); // make sure all above writes are finished
strat.sync(); // make sure all above writes are finished
ASSERT_EQ(callCount, totalRequests); // all requests should finish
work.reset();

View File

@@ -35,12 +35,9 @@ class BackendCassandraRetryPolicyTest : public SyncAsioContextTest
TEST_F(BackendCassandraRetryPolicyTest, ShouldRetryAlwaysTrue)
{
auto retryPolicy = ExponentialBackoffRetryPolicy{ctx};
EXPECT_TRUE(retryPolicy.shouldRetry(
CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}));
EXPECT_TRUE(retryPolicy.shouldRetry(
CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}));
EXPECT_TRUE(retryPolicy.shouldRetry(
CassandraError{"invalid query", CASS_ERROR_SERVER_INVALID_QUERY}));
EXPECT_TRUE(retryPolicy.shouldRetry(CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}));
EXPECT_TRUE(retryPolicy.shouldRetry(CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}));
EXPECT_TRUE(retryPolicy.shouldRetry(CassandraError{"invalid query", CASS_ERROR_SERVER_INVALID_QUERY}));
// this policy actually always returns true
auto const err = CassandraError{"ok", CASS_OK};
@@ -64,9 +61,8 @@ TEST_F(BackendCassandraRetryPolicyTest, CheckComputedBackoffDelayIsCorrect)
EXPECT_EQ(retryPolicy.calculateDelay(8).count(), 256);
EXPECT_EQ(retryPolicy.calculateDelay(9).count(), 512);
EXPECT_EQ(retryPolicy.calculateDelay(10).count(), 1024);
EXPECT_EQ(
retryPolicy.calculateDelay(11).count(),
1024); // 10 is max, same after that
EXPECT_EQ(retryPolicy.calculateDelay(11).count(),
1024); // 10 is max, same after that
}
TEST_F(BackendCassandraRetryPolicyTest, RetryCorrectlyExecuted)

View File

@@ -54,8 +54,7 @@ TEST_F(SettingsProviderTest, Defaults)
EXPECT_EQ(settings.username, std::nullopt);
EXPECT_EQ(settings.password, std::nullopt);
auto const* cp =
std::get_if<Settings::ContactPoints>(&settings.connectionInfo);
auto const* cp = std::get_if<Settings::ContactPoints>(&settings.connectionInfo);
ASSERT_TRUE(cp != nullptr);
EXPECT_EQ(cp->contactPoints, "127.0.0.1");
EXPECT_FALSE(cp->port);
@@ -80,8 +79,7 @@ TEST_F(SettingsProviderTest, SimpleConfig)
auto const settings = provider.getSettings();
EXPECT_EQ(settings.threads, 24);
auto const* cp =
std::get_if<Settings::ContactPoints>(&settings.connectionInfo);
auto const* cp = std::get_if<Settings::ContactPoints>(&settings.connectionInfo);
ASSERT_TRUE(cp != nullptr);
EXPECT_EQ(cp->contactPoints, "123.123.123.123");
EXPECT_EQ(cp->port, 1234);
@@ -97,8 +95,7 @@ TEST_F(SettingsProviderTest, SecureBundleConfig)
SettingsProvider provider{cfg};
auto const settings = provider.getSettings();
auto const* sb =
std::get_if<Settings::SecureConnectionBundle>(&settings.connectionInfo);
auto const* sb = std::get_if<Settings::SecureConnectionBundle>(&settings.connectionInfo);
ASSERT_TRUE(sb != nullptr);
EXPECT_EQ(sb->bundle, "bundleData");
}

View File

@@ -99,8 +99,7 @@ struct MockHandle
MOCK_METHOD(
FutureWithCallbackType,
asyncExecute,
(std::vector<StatementType> const&,
std::function<void(ResultOrErrorType)>&&),
(std::vector<StatementType> const&, std::function<void(ResultOrErrorType)>&&),
(const));
MOCK_METHOD(ResultOrErrorType, execute, (StatementType const&), (const));