mirror of
https://github.com/apple/foundationdb.git
synced 2025-06-03 03:41:53 +08:00
Add fdb_transaction_add_conflict_range unit test
This commit is contained in:
parent
98f7444104
commit
6aa199b9a2
@ -215,4 +215,15 @@ void Transaction::atomic_op(std::string_view key, const uint8_t* param,
|
||||
return fdb_transaction_get_committed_version(tr_, out_version);
|
||||
}
|
||||
|
||||
fdb_error_t Transaction::add_conflict_range(std::string_view begin_key,
|
||||
std::string_view end_key,
|
||||
FDBConflictRangeType type) {
|
||||
return fdb_transaction_add_conflict_range(tr_,
|
||||
(const uint8_t*)begin_key.data(),
|
||||
begin_key.size(),
|
||||
(const uint8_t*)end_key.data(),
|
||||
end_key.size(),
|
||||
type);
|
||||
}
|
||||
|
||||
} // namespace fdb
|
||||
|
@ -231,6 +231,11 @@ class Transaction final {
|
||||
// Wrapper around fdb_transaction_get_committed_version.
|
||||
fdb_error_t get_committed_version(int64_t* out_version);
|
||||
|
||||
// Wrapper around fdb_transaction_add_conflict_range.
|
||||
fdb_error_t add_conflict_range(std::string_view begin_key,
|
||||
std::string_view end_key,
|
||||
FDBConflictRangeType type);
|
||||
|
||||
private:
|
||||
FDBTransaction* tr_;
|
||||
};
|
||||
|
@ -1688,6 +1688,59 @@ TEST_CASE("fdb_transaction_cancel") {
|
||||
fdb_check(wait_future(f2));
|
||||
}
|
||||
|
||||
TEST_CASE("fdb_transaction_add_conflict_range") {
|
||||
bool success = false;
|
||||
|
||||
bool retry = true;
|
||||
while (retry) {
|
||||
fdb::Transaction tr(db);
|
||||
fdb::Int64Future f1 = tr.get_read_version();
|
||||
|
||||
fdb::Transaction tr2(db);
|
||||
while (1) {
|
||||
fdb_check(tr2.add_conflict_range(KEY("a"), strinc(KEY("a")),
|
||||
FDB_CONFLICT_RANGE_TYPE_WRITE));
|
||||
fdb::EmptyFuture f1 = tr2.commit();
|
||||
|
||||
fdb_error_t err = wait_future(f1);
|
||||
if (err) {
|
||||
fdb::EmptyFuture f2 = tr2.on_error(err);
|
||||
fdb_check(wait_future(f2));
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
fdb_check(tr.add_conflict_range(KEY("a"), strinc(KEY("a")),
|
||||
FDB_CONFLICT_RANGE_TYPE_READ));
|
||||
fdb_check(tr.add_conflict_range(KEY("a"), strinc(KEY("a")),
|
||||
FDB_CONFLICT_RANGE_TYPE_WRITE));
|
||||
fdb::EmptyFuture f1 = tr.commit();
|
||||
|
||||
fdb_error_t err = wait_future(f1);
|
||||
if (err == 1020) { // not_committed
|
||||
// Test should pass if transactions conflict.
|
||||
success = true;
|
||||
retry = false;
|
||||
} else if (err) {
|
||||
fdb::EmptyFuture f2 = tr.on_error(err);
|
||||
fdb_check(wait_future(f2));
|
||||
retry = true;
|
||||
} else {
|
||||
// If the transaction succeeded, something went wrong.
|
||||
CHECK(false);
|
||||
retry = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Double check that failure was achieved and the loop wasn't just broken out
|
||||
// of.
|
||||
CHECK(success);
|
||||
}
|
||||
|
||||
TEST_CASE("fdb_error_predicate") {
|
||||
CHECK(fdb_error_predicate(FDB_ERROR_PREDICATE_RETRYABLE, 1007)); // transaction_too_old
|
||||
CHECK(fdb_error_predicate(FDB_ERROR_PREDICATE_RETRYABLE, 1020)); // not_committed
|
||||
|
Loading…
x
Reference in New Issue
Block a user