Skip to content

Commit ba3de7b

Browse files
authored
chore(spanner): add integration testing for lock hint in spanner (#15275)
* chore(spanner): add integration testing for lock hint in spanner
1 parent e29705b commit ba3de7b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

google/cloud/spanner/integration_tests/client_integration_test.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,63 @@ TEST_F(ClientIntegrationTest, ReadWithOrderByPrimaryKey) {
863863
EXPECT_THAT(actual_singer_ids, ::testing::ElementsAre(1, 2, 3));
864864
}
865865

866+
TEST_F(ClientIntegrationTest, ReadWithLockHint) {
867+
// Insert initial data: SingerId 1 and 2.
868+
ASSERT_NO_FATAL_FAILURE(InsertTwoSingers());
869+
870+
auto& client = *client_;
871+
872+
auto commit_result = client_->Commit(
873+
[&client](google::cloud::spanner::Transaction const& txn)
874+
-> google::cloud::StatusOr<google::cloud::spanner::Mutations> {
875+
// Read row 1 and apply LockHint to the Read operation
876+
auto rows = client.Read(
877+
txn, "Singers",
878+
google::cloud::spanner::KeySet().AddKey(
879+
google::cloud::spanner::MakeKey(1)),
880+
{"SingerId", "FirstName", "LastName"},
881+
Options{}.set<LockHintOption>(LockHint::kLockHintExclusive));
882+
883+
using RowType = std::tuple<std::int64_t, std::string, std::string>;
884+
auto row = GetSingularRow(StreamOf<RowType>(rows));
885+
if (!row) {
886+
return std::move(row).status();
887+
}
888+
EXPECT_EQ(std::get<0>(*row), 1);
889+
EXPECT_EQ(std::get<1>(*row), "test-fname-1");
890+
EXPECT_EQ(std::get<2>(*row), "test-lname-1");
891+
892+
// Update same row as read.
893+
auto update_result = client.ExecuteDml(
894+
txn, google::cloud::spanner::SqlStatement(
895+
"UPDATE Singers SET LastName = @new_lname "
896+
"WHERE SingerId = @id",
897+
{{"new_lname",
898+
google::cloud::spanner::Value("Updated-lname-1")},
899+
{"id", google::cloud::spanner::Value(1)}}));
900+
if (!update_result) {
901+
return std::move(update_result).status();
902+
}
903+
return google::cloud::spanner::Mutations{};
904+
});
905+
906+
// Verify that the transaction successfully committed.
907+
EXPECT_STATUS_OK(commit_result);
908+
909+
// After the commit, read the data directly to verify the update.
910+
auto rows_after_commit =
911+
client_->Read("Singers",
912+
google::cloud::spanner::KeySet().AddKey(
913+
google::cloud::spanner::MakeKey(1)),
914+
{"SingerId", "FirstName", "LastName"});
915+
using RowType = std::tuple<std::int64_t, std::string, std::string>;
916+
auto updated_row = GetSingularRow(StreamOf<RowType>(rows_after_commit));
917+
ASSERT_STATUS_OK(updated_row);
918+
EXPECT_EQ(std::get<0>(*updated_row), 1);
919+
EXPECT_EQ(std::get<1>(*updated_row), "test-fname-1");
920+
EXPECT_EQ(std::get<2>(*updated_row), "Updated-lname-1");
921+
}
922+
866923
StatusOr<std::vector<std::vector<Value>>> AddSingerDataToTable(Client client) {
867924
std::vector<std::vector<Value>> expected_rows;
868925
auto commit = client.Commit(

0 commit comments

Comments
 (0)