Skip to content

Commit

Permalink
fix oncall
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyyan317 committed Aug 16, 2024
1 parent 8c85f80 commit e3b1b95
Showing 1 changed file with 43 additions and 83 deletions.
126 changes: 43 additions & 83 deletions tests/unit/data/cassandra/ExecutionStrategyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineSuccessful)
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& /* statement */, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillOnce([](auto const& /* statement */, auto&& cb) {
cb({}); // pretend we got data
return FakeFutureWithCallback{};
});
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(1));
EXPECT_CALL(*counters, registerReadFinishedImpl(testing::_, 1));

Expand All @@ -139,14 +137,12 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnTimeoutF
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillOnce([](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(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(1));
EXPECT_CALL(*counters, registerReadErrorImpl(1));

Expand All @@ -160,14 +156,12 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadOneInCoroutineThrowsOnInvalidQ
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillOnce([](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(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(1));
EXPECT_CALL(*counters, registerReadErrorImpl(1));

Expand All @@ -181,16 +175,14 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineSuccessful)
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.WillOnce([](auto const& statements, auto&& cb) {
EXPECT_EQ(statements.size(), NUM_STATEMENTS);
cb({}); // pretend we got data
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadFinishedImpl(testing::_, NUM_STATEMENTS));

Expand All @@ -204,17 +196,15 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnTimeou
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.WillOnce([](auto const& statements, auto&& cb) {
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(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadErrorImpl(NUM_STATEMENTS));

Expand All @@ -228,17 +218,15 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineThrowsOnInvali
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const& statements, auto&& cb) {
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.WillOnce([](auto const& statements, auto&& cb) {
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(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadErrorImpl(NUM_STATEMENTS));

Expand All @@ -252,19 +240,17 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadBatchInCoroutineMarksBusyIfReq
{
auto strat = makeStrategy(Settings{.maxReadRequestsOutstanding = 2});

ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([this, &strat](auto const& statements, auto&& cb) {
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.WillOnce([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(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.Times(1);
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadFinishedImpl(testing::_, NUM_STATEMENTS));

Expand All @@ -280,19 +266,12 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineSuccessful)
{
auto strat = makeStrategy();

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([](auto const&, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(NUM_STATEMENTS)
.WillRepeatedly([](auto const&, auto&& cb) {
cb({}); // pretend we got data
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
A<FakeStatement const&>(),
A<std::function<void(FakeResultOrError)>&&>()
)
)
.Times(NUM_STATEMENTS); // once per statement
}); // once per statement
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadFinishedImpl(testing::_, NUM_STATEMENTS));

Expand All @@ -308,24 +287,17 @@ TEST_F(BackendCassandraExecutionStrategyTest, ReadEachInCoroutineThrowsOnFailure
auto strat = makeStrategy();
auto callCount = std::atomic_int{0};

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([&callCount](auto const&, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(NUM_STATEMENTS)
.WillRepeatedly([&callCount](auto const&, auto&& cb) {
if (callCount == 1) { // error happens on one of the entries
cb({CassandraError{"invalid data", CASS_ERROR_LIB_INVALID_DATA}});
} else {
cb({}); // pretend we got data
}
++callCount;
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
A<FakeStatement const&>(),
A<std::function<void(FakeResultOrError)>&&>()
)
)
.Times(NUM_STATEMENTS); // once per statement
}); // once per statement
EXPECT_CALL(*counters, registerReadStartedImpl(NUM_STATEMENTS));
EXPECT_CALL(*counters, registerReadErrorImpl(1));
EXPECT_CALL(*counters, registerReadFinishedImpl(testing::_, 2));
Expand All @@ -340,9 +312,9 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteSyncFirstTrySuccessful)
{
auto strat = makeStrategy();

ON_CALL(handle, execute(A<FakeStatement const&>())).WillByDefault([](auto const&) { return FakeResultOrError{}; });
EXPECT_CALL(handle,
execute(A<FakeStatement const&>())).Times(1); // first one will succeed
EXPECT_CALL(handle, execute(A<FakeStatement const&>())).WillOnce([](auto const&) {
return FakeResultOrError{};
}); // first one will succeed
EXPECT_CALL(*counters, registerWriteSync(testing::_));

EXPECT_TRUE(strat.writeSync({}));
Expand Down Expand Up @@ -375,23 +347,18 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteMultipleAndCallSyncSucceeds)
auto work = boost::asio::make_work_guard(ctx);
auto thread = std::thread{[this]() { ctx.run(); }};

ON_CALL(handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([this, &callCount](auto const&, auto&& cb) {
EXPECT_CALL(
handle, asyncExecute(A<std::vector<FakeStatement> const&>(), A<std::function<void(FakeResultOrError)>&&>())
)
.Times(totalRequests)
.WillRepeatedly([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)] {
++callCount;
cb({}); // pretend we got data
});
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
A<std::vector<FakeStatement> const&>(),
A<std::function<void(FakeResultOrError)>&&>()
)
)
.Times(totalRequests); // one per write call
}); // one per write call
EXPECT_CALL(*counters, registerWriteStarted()).Times(totalRequests);
EXPECT_CALL(*counters, registerWriteFinished(testing::_)).Times(totalRequests);

Expand All @@ -416,23 +383,16 @@ TEST_F(BackendCassandraExecutionStrategyTest, WriteEachAndCallSyncSucceeds)
auto work = boost::asio::make_work_guard(ctx);
auto thread = std::thread{[this]() { ctx.run(); }};

ON_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.WillByDefault([this, &callCount](auto const&, auto&& cb) {
EXPECT_CALL(handle, asyncExecute(A<FakeStatement const&>(), A<std::function<void(FakeResultOrError)>&&>()))
.Times(totalRequests * numStatements)
.WillRepeatedly([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)] {
++callCount;
cb({}); // pretend we got data
});
return FakeFutureWithCallback{};
});
EXPECT_CALL(
handle,
asyncExecute(
A<FakeStatement const&>(),
A<std::function<void(FakeResultOrError)>&&>()
)
)
.Times(totalRequests * numStatements); // numStatements per write call
}); // numStatements per write call
EXPECT_CALL(*counters, registerWriteStarted()).Times(totalRequests * numStatements);
EXPECT_CALL(*counters, registerWriteFinished(testing::_)).Times(totalRequests * numStatements);

Expand Down

0 comments on commit e3b1b95

Please sign in to comment.