Skip to content

Commit

Permalink
rework RemoteDatabaseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Sep 18, 2024
1 parent a01d3d6 commit 45cf43f
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions silkworm/rpc/ethdb/kv/remote_database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
namespace silkworm::rpc::ethdb::kv {

class RemoteDatabaseTest : public db::test_util::KVTestBase {
public:
protected:
// RemoteDatabase holds the KV stub by std::unique_ptr, so we cannot rely on mock stub from base class
StrictMockKVStub* kv_stub = new StrictMockKVStub;
test::BackEndMock backend;
RemoteDatabase remote_db{&backend, &state_cache_, grpc_context_, std::unique_ptr<StrictMockKVStub>{kv_stub}};
StrictMockKVStub* kv_stub_ = new StrictMockKVStub;
RemoteDatabase remote_db_{&backend_, &state_cache_, grpc_context_, std::unique_ptr<StrictMockKVStub>{kv_stub_}};

private:
test::BackEndMock backend_;
db::kv::api::CoherentStateCache state_cache_;
};

Expand All @@ -48,36 +48,36 @@ TEST_CASE_METHOD(RemoteDatabaseTest, "RemoteDatabase::begin", "[rpc][ethdb][kv][
SECTION("success") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncTxRaw call succeeds
expect_request_async_tx(*kv_stub, true);
expect_request_async_tx(*kv_stub_, true);
// 2. AsyncReaderWriter<remote::Cursor, remote::Pair>::Read call succeeds setting the specified transaction ID
remote::Pair pair;
pair.set_view_id(4);
EXPECT_CALL(reader_writer_, Read).WillOnce(test::read_success_with(grpc_context_, pair));

// Execute the test: RemoteDatabase::begin should return transaction w/ expected transaction ID
const auto txn = spawn_and_wait(remote_db.begin());
const auto txn = spawn_and_wait(remote_db_.begin());
CHECK(txn->view_id() == 4);
}

SECTION("open failure") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncTxRaw call fails
expect_request_async_tx(*kv_stub, /*ok=*/false);
expect_request_async_tx(*kv_stub_, /*ok=*/false);
// 2. AsyncReaderWriter<remote::Cursor, remote::Pair>::WritesDone call returns w/ failure
EXPECT_CALL(reader_writer_, WritesDone).WillOnce(test::writes_done_failure(grpc_context_));
// 3. AsyncReaderWriter<remote::Cursor, remote::Pair>::Finish call succeeds w/ status cancelled
EXPECT_CALL(reader_writer_, Finish).WillOnce(test::finish_streaming_cancelled(grpc_context_));

// Execute the test: RemoteDatabase::begin should raise an exception w/ expected gRPC status code
CHECK_THROWS_MATCHES(spawn_and_wait(remote_db.begin()),
CHECK_THROWS_MATCHES(spawn_and_wait(remote_db_.begin()),
boost::system::system_error,
test::exception_has_cancelled_grpc_status_code());
}

SECTION("read failure") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncTxRaw call succeeds
expect_request_async_tx(*kv_stub, true);
expect_request_async_tx(*kv_stub_, true);
// 2. AsyncReaderWriter<remote::Cursor, remote::Pair>::Read call fails
EXPECT_CALL(reader_writer_, Read).WillOnce([&](auto*, void* tag) {
agrpc::process_grpc_tag(grpc_context_, tag, /*ok=*/false);
Expand All @@ -88,7 +88,7 @@ TEST_CASE_METHOD(RemoteDatabaseTest, "RemoteDatabase::begin", "[rpc][ethdb][kv][
EXPECT_CALL(reader_writer_, Finish).WillOnce(test::finish_streaming_cancelled(grpc_context_));

// Execute the test: RemoteDatabase::begin should raise an exception w/ expected gRPC status code
CHECK_THROWS_MATCHES(spawn_and_wait(remote_db.begin()),
CHECK_THROWS_MATCHES(spawn_and_wait(remote_db_.begin()),
boost::system::system_error,
test::exception_has_cancelled_grpc_status_code());
}
Expand Down

0 comments on commit 45cf43f

Please sign in to comment.