mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -31,6 +31,14 @@ using namespace testing;
|
||||
|
||||
class BackendCassandraAsyncExecutorTest : public SyncAsioContextTest
|
||||
{
|
||||
protected:
|
||||
struct CallbackMock
|
||||
{
|
||||
MOCK_METHOD(void, onComplete, (FakeResultOrError));
|
||||
MOCK_METHOD(void, onRetry, ());
|
||||
};
|
||||
CallbackMock callbackMock_;
|
||||
std::function<void()> onRetry_ = [this]() { callbackMock_.onRetry(); };
|
||||
};
|
||||
|
||||
TEST_F(BackendCassandraAsyncExecutorTest, CompletionCalledOnSuccess)
|
||||
@@ -45,16 +53,20 @@ TEST_F(BackendCassandraAsyncExecutorTest, CompletionCalledOnSuccess)
|
||||
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(AtLeast(1));
|
||||
|
||||
auto called = std::atomic_bool{false};
|
||||
auto work = std::optional<boost::asio::io_context::work>{ctx};
|
||||
EXPECT_CALL(callbackMock_, onComplete);
|
||||
|
||||
AsyncExecutor<FakeStatement, MockHandle>::run(ctx, handle, FakeStatement{}, [&called, &work](auto&&) {
|
||||
called = true;
|
||||
work.reset();
|
||||
});
|
||||
AsyncExecutor<FakeStatement, MockHandle>::run(
|
||||
ctx,
|
||||
handle,
|
||||
FakeStatement{},
|
||||
[&work, this](auto resultOrError) {
|
||||
callbackMock_.onComplete(std::move(resultOrError));
|
||||
work.reset();
|
||||
},
|
||||
std::move(onRetry_));
|
||||
|
||||
ctx.run();
|
||||
ASSERT_TRUE(called);
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnMainThread)
|
||||
@@ -80,17 +92,22 @@ TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnMa
|
||||
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};
|
||||
EXPECT_CALL(callbackMock_, onComplete);
|
||||
EXPECT_CALL(callbackMock_, onRetry).Times(2);
|
||||
|
||||
AsyncExecutor<FakeStatement, MockHandle>::run(ctx, handle, FakeStatement{}, [&called, &work](auto&&) {
|
||||
called = true;
|
||||
work.reset();
|
||||
});
|
||||
AsyncExecutor<FakeStatement, MockHandle>::run(
|
||||
ctx,
|
||||
handle,
|
||||
FakeStatement{},
|
||||
[this, &work](auto resultOrError) {
|
||||
callbackMock_.onComplete(std::move(resultOrError));
|
||||
work.reset();
|
||||
},
|
||||
std::move(onRetry_));
|
||||
|
||||
ctx.run();
|
||||
ASSERT_TRUE(callCount >= 3);
|
||||
ASSERT_TRUE(called);
|
||||
ASSERT_EQ(callCount, 3);
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnOtherThread)
|
||||
@@ -120,19 +137,23 @@ TEST_F(BackendCassandraAsyncExecutorTest, ExecutedMultipleTimesByRetryPolicyOnOt
|
||||
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};
|
||||
EXPECT_CALL(callbackMock_, onComplete);
|
||||
EXPECT_CALL(callbackMock_, onRetry).Times(2);
|
||||
|
||||
AsyncExecutor<FakeStatement, MockHandle>::run(
|
||||
threadedCtx, handle, FakeStatement{}, [&called, &work, &work2](auto&&) {
|
||||
called = true;
|
||||
threadedCtx,
|
||||
handle,
|
||||
FakeStatement{},
|
||||
[this, &work, &work2](auto resultOrError) {
|
||||
callbackMock_.onComplete(std::move(resultOrError));
|
||||
work.reset();
|
||||
work2.reset();
|
||||
});
|
||||
},
|
||||
std::move(onRetry_));
|
||||
|
||||
ctx.run();
|
||||
ASSERT_TRUE(callCount >= 3);
|
||||
ASSERT_TRUE(called);
|
||||
EXPECT_EQ(callCount, 3);
|
||||
threadedCtx.stop();
|
||||
thread.join();
|
||||
}
|
||||
@@ -151,19 +172,22 @@ TEST_F(BackendCassandraAsyncExecutorTest, CompletionCalledOnFailureAfterRetryCou
|
||||
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};
|
||||
EXPECT_CALL(callbackMock_, onComplete);
|
||||
|
||||
AsyncExecutor<FakeStatement, MockHandle, FakeRetryPolicy>::run(
|
||||
ctx, handle, FakeStatement{}, [&called, &work](auto&& res) {
|
||||
ctx,
|
||||
handle,
|
||||
FakeStatement{},
|
||||
[this, &work](auto res) {
|
||||
EXPECT_FALSE(res);
|
||||
EXPECT_EQ(res.error().code(), CASS_ERROR_LIB_INTERNAL_ERROR);
|
||||
EXPECT_EQ(res.error().message(), "not a timeout");
|
||||
|
||||
called = true;
|
||||
callbackMock_.onComplete(std::move(res));
|
||||
work.reset();
|
||||
});
|
||||
},
|
||||
std::move(onRetry_));
|
||||
|
||||
ctx.run();
|
||||
ASSERT_TRUE(called);
|
||||
}
|
||||
|
||||
@@ -30,20 +30,89 @@ using namespace testing;
|
||||
|
||||
class BackendCassandraExecutionStrategyTest : public SyncAsioContextTest
|
||||
{
|
||||
protected:
|
||||
class MockBackendCounters
|
||||
{
|
||||
public:
|
||||
using PtrType = std::shared_ptr<StrictMock<MockBackendCounters>>;
|
||||
static PtrType
|
||||
make()
|
||||
{
|
||||
return std::make_shared<StrictMock<MockBackendCounters>>();
|
||||
}
|
||||
|
||||
MOCK_METHOD(void, registerTooBusy, (), ());
|
||||
MOCK_METHOD(void, registerWriteSync, (), ());
|
||||
MOCK_METHOD(void, registerWriteSyncRetry, (), ());
|
||||
MOCK_METHOD(void, registerWriteStarted, (), ());
|
||||
MOCK_METHOD(void, registerWriteFinished, (), ());
|
||||
MOCK_METHOD(void, registerWriteRetry, (), ());
|
||||
|
||||
void
|
||||
registerReadStarted(std::uint64_t count = 1)
|
||||
{
|
||||
registerReadStartedImpl(count);
|
||||
}
|
||||
MOCK_METHOD(void, registerReadStartedImpl, (std::uint64_t), ());
|
||||
|
||||
void
|
||||
registerReadFinished(std::uint64_t count = 1)
|
||||
{
|
||||
registerReadFinishedImpl(count);
|
||||
}
|
||||
MOCK_METHOD(void, registerReadFinishedImpl, (std::uint64_t), ());
|
||||
|
||||
void
|
||||
registerReadRetry(std::uint64_t count = 1)
|
||||
{
|
||||
registerReadRetryImpl(count);
|
||||
}
|
||||
MOCK_METHOD(void, registerReadRetryImpl, (std::uint64_t), ());
|
||||
|
||||
void
|
||||
registerReadError(std::uint64_t count = 1)
|
||||
{
|
||||
registerReadErrorImpl(count);
|
||||
}
|
||||
MOCK_METHOD(void, registerReadErrorImpl, (std::uint64_t), ());
|
||||
MOCK_METHOD(boost::json::object, report, (), ());
|
||||
};
|
||||
|
||||
MockHandle handle{};
|
||||
MockBackendCounters::PtrType counters = MockBackendCounters::make();
|
||||
static constexpr auto NUM_STATEMENTS = 3u;
|
||||
|
||||
DefaultExecutionStrategy<MockHandle, MockBackendCounters>
|
||||
makeStrategy(Settings s = {})
|
||||
{
|
||||
return DefaultExecutionStrategy<MockHandle, MockBackendCounters>(s, handle, counters);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, IsTooBusy)
|
||||
{
|
||||
{
|
||||
auto strat = makeStrategy(Settings{.maxReadRequestsOutstanding = 0});
|
||||
EXPECT_CALL(*counters, registerTooBusy());
|
||||
EXPECT_TRUE(strat.isTooBusy());
|
||||
}
|
||||
auto strat = makeStrategy(Settings{.maxReadRequestsOutstanding = 1});
|
||||
EXPECT_FALSE(strat.isTooBusy());
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineSuccessful)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<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(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(1));
|
||||
EXPECT_CALL(*counters, registerReadFinishedImpl(1));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statement = FakeStatement{};
|
||||
@@ -53,17 +122,18 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineSuccessful)
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnTimeoutFailure)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const&, auto&& cb) {
|
||||
auto res = FakeResultOrError{CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}};
|
||||
cb(res); // notify that item is ready
|
||||
return FakeFutureWithCallback{res};
|
||||
});
|
||||
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(1));
|
||||
EXPECT_CALL(*counters, registerReadErrorImpl(1));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statement = FakeStatement{};
|
||||
@@ -73,17 +143,18 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnTimeoutF
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnInvalidQueryFailure)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const&, auto&& cb) {
|
||||
auto res = FakeResultOrError{CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}};
|
||||
cb(res); // notify that item is ready
|
||||
return FakeFutureWithCallback{res};
|
||||
});
|
||||
EXPECT_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(1));
|
||||
EXPECT_CALL(*counters, registerReadErrorImpl(1));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statement = FakeStatement{};
|
||||
@@ -93,95 +164,94 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnInvalidQ
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineSuccessful)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const& statements, auto&& cb) {
|
||||
EXPECT_EQ(statements.size(), 3);
|
||||
EXPECT_EQ(statements.size(), NUM_STATEMENTS);
|
||||
cb({}); // pretend we got data
|
||||
return FakeFutureWithCallback{};
|
||||
});
|
||||
EXPECT_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadFinishedImpl(NUM_STATEMENTS));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
strat.read(yield, statements);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnTimeoutFailure)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const& statements, auto&& cb) {
|
||||
EXPECT_EQ(statements.size(), 3);
|
||||
EXPECT_EQ(statements.size(), NUM_STATEMENTS);
|
||||
auto res = FakeResultOrError{CassandraError{"timeout", CASS_ERROR_LIB_REQUEST_TIMED_OUT}};
|
||||
cb(res); // notify that item is ready
|
||||
return FakeFutureWithCallback{res};
|
||||
});
|
||||
EXPECT_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadErrorImpl(NUM_STATEMENTS));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
EXPECT_THROW(strat.read(yield, statements), DatabaseTimeout);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnInvalidQueryFailure)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const& statements, auto&& cb) {
|
||||
EXPECT_EQ(statements.size(), 3);
|
||||
EXPECT_EQ(statements.size(), NUM_STATEMENTS);
|
||||
auto res = FakeResultOrError{CassandraError{"invalid", CASS_ERROR_SERVER_INVALID_QUERY}};
|
||||
cb(res); // notify that item is ready
|
||||
return FakeFutureWithCallback{res};
|
||||
});
|
||||
EXPECT_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadErrorImpl(NUM_STATEMENTS));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
EXPECT_THROW(strat.read(yield, statements), std::runtime_error);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineMarksBusyIfRequestsOutstandingExceeded)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto settings = Settings{};
|
||||
settings.maxReadRequestsOutstanding = 2;
|
||||
auto strat = DefaultExecutionStrategy{settings, handle};
|
||||
auto strat = makeStrategy(Settings{.maxReadRequestsOutstanding = 2});
|
||||
|
||||
ON_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([&strat](auto const& statements, auto&& cb) {
|
||||
EXPECT_EQ(statements.size(), 3);
|
||||
ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([this, &strat](auto const& statements, auto&& cb) {
|
||||
EXPECT_EQ(statements.size(), NUM_STATEMENTS);
|
||||
EXPECT_CALL(*counters, registerTooBusy());
|
||||
EXPECT_TRUE(strat.isTooBusy()); // 2 was the limit, we sent 3
|
||||
|
||||
cb({}); // notify that item is ready
|
||||
return FakeFutureWithCallback{};
|
||||
});
|
||||
EXPECT_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(1);
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadFinishedImpl(NUM_STATEMENTS));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
EXPECT_FALSE(strat.isTooBusy()); // 2 was the limit, 0 atm
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
strat.read(yield, statements);
|
||||
EXPECT_FALSE(strat.isTooBusy()); // after read completes it's 0 again
|
||||
});
|
||||
@@ -189,10 +259,9 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineMarksBusyIfReq
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([](auto const&, auto&& cb) {
|
||||
cb({}); // pretend we got data
|
||||
return FakeFutureWithCallback{};
|
||||
@@ -200,12 +269,14 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
|
||||
EXPECT_CALL(
|
||||
handle,
|
||||
asyncExecute(
|
||||
An<FakeStatement const&>(),
|
||||
An<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(3); // once per statement
|
||||
A<FakeStatement const&>(),
|
||||
A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(NUM_STATEMENTS); // once per statement
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadFinishedImpl(NUM_STATEMENTS));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
auto res = strat.readEach(yield, statements);
|
||||
EXPECT_EQ(res.size(), statements.size());
|
||||
});
|
||||
@@ -213,11 +284,10 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineThrowsOnFailure)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
auto callCount = std::atomic_int{0};
|
||||
|
||||
ON_CALL(handle, asyncExecute(An<FakeStatement const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.WillByDefault([&callCount](auto const&, auto&& cb) {
|
||||
if (callCount == 1)
|
||||
{ // error happens on one of the entries
|
||||
@@ -233,57 +303,59 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineThrowsOnFailure
|
||||
EXPECT_CALL(
|
||||
handle,
|
||||
asyncExecute(
|
||||
An<FakeStatement const&>(),
|
||||
An<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(3); // once per statement
|
||||
A<FakeStatement const&>(),
|
||||
A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(NUM_STATEMENTS); // once per statement
|
||||
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
|
||||
EXPECT_CALL(*counters, registerReadErrorImpl(1));
|
||||
EXPECT_CALL(*counters, registerReadFinishedImpl(2));
|
||||
|
||||
runSpawn([&strat](boost::asio::yield_context yield) {
|
||||
auto statements = std::vector<FakeStatement>(3);
|
||||
auto statements = std::vector<FakeStatement>(NUM_STATEMENTS);
|
||||
EXPECT_THROW(strat.readEach(yield, statements), DatabaseTimeout);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, WriteSyncFirstTrySuccessful)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
|
||||
ON_CALL(handle, execute(An<FakeStatement const&>())).WillByDefault([](auto const&) { return FakeResultOrError{}; });
|
||||
ON_CALL(handle, execute(A<FakeStatement const&>())).WillByDefault([](auto const&) { return FakeResultOrError{}; });
|
||||
EXPECT_CALL(handle,
|
||||
execute(An<FakeStatement const&>())).Times(1); // first one will succeed
|
||||
execute(A<FakeStatement const&>())).Times(1); // first one will succeed
|
||||
EXPECT_CALL(*counters, registerWriteSync());
|
||||
|
||||
EXPECT_TRUE(strat.writeSync({}));
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, WriteSyncRetrySuccessful)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto strat = makeStrategy();
|
||||
auto callCount = 0;
|
||||
|
||||
ON_CALL(handle, execute(An<FakeStatement const&>())).WillByDefault([&callCount](auto const&) {
|
||||
ON_CALL(handle, execute(A<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
|
||||
execute(A<FakeStatement const&>())).Times(2); // first one will fail, second will succeed
|
||||
EXPECT_CALL(*counters, registerWriteSyncRetry());
|
||||
EXPECT_CALL(*counters, registerWriteSync());
|
||||
|
||||
EXPECT_TRUE(strat.writeSync({}));
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
|
||||
{
|
||||
auto handle = MockHandle{};
|
||||
auto strat = DefaultExecutionStrategy{Settings{}, handle};
|
||||
auto totalRequests = 1024u;
|
||||
auto strat = makeStrategy();
|
||||
auto const totalRequests = 1024u;
|
||||
auto callCount = std::atomic_uint{0u};
|
||||
|
||||
auto work = std::optional<boost::asio::io_context::work>{ctx};
|
||||
auto thread = std::thread{[this]() { ctx.run(); }};
|
||||
|
||||
ON_CALL(
|
||||
handle, asyncExecute(An<std::vector<FakeStatement> const&>(), An<std::function<void(FakeResultOrError)>&&>()))
|
||||
ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<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::forward<decltype(cb)>(cb)] {
|
||||
@@ -295,9 +367,11 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
|
||||
EXPECT_CALL(
|
||||
handle,
|
||||
asyncExecute(
|
||||
An<std::vector<FakeStatement> const&>(),
|
||||
An<std::function<void(FakeResultOrError)>&&>()))
|
||||
A<std::vector<FakeStatement> const&>(),
|
||||
A<std::function<void(FakeResultOrError)>&&>()))
|
||||
.Times(totalRequests); // one per write call
|
||||
EXPECT_CALL(*counters, registerWriteStarted()).Times(totalRequests);
|
||||
EXPECT_CALL(*counters, registerWriteFinished()).Times(totalRequests);
|
||||
|
||||
auto makeStatements = [] { return std::vector<FakeStatement>(16); };
|
||||
for (auto i = 0u; i < totalRequests; ++i)
|
||||
@@ -309,3 +383,10 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
|
||||
work.reset();
|
||||
thread.join();
|
||||
}
|
||||
|
||||
TEST_F(BackendCassandraExecutionStrategyTest, StatsCallsCountersReport)
|
||||
{
|
||||
auto strat = makeStrategy();
|
||||
EXPECT_CALL(*counters, report());
|
||||
strat.stats();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user