diff --git a/test/boost/memtable_test.cc b/test/boost/memtable_test.cc index f1394b7c7a..191e37d31a 100644 --- a/test/boost/memtable_test.cc +++ b/test/boost/memtable_test.cc @@ -443,18 +443,10 @@ SEASTAR_TEST_CASE(test_exception_safety_of_partition_range_reads) { mt->apply(m); } - auto& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { - try { - injector.fail_after(i++); - assert_that(mt->make_flat_reader(s, tests::make_permit(), query::full_partition_range)) - .produces(ms); - injector.cancel(); - } catch (const std::bad_alloc&) { - // expected - } - } while (injector.failed()); + memory::with_allocation_failures([&] { + assert_that(mt->make_flat_reader(s, tests::make_permit(), query::full_partition_range)) + .produces(ms); + }); }); } @@ -469,19 +461,13 @@ SEASTAR_TEST_CASE(test_exception_safety_of_flush_reads) { mt->apply(m); } - auto& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { - try { - injector.fail_after(i++); - assert_that(mt->make_flush_reader(s, default_priority_class())) - .produces(ms); - injector.cancel(); - } catch (const std::bad_alloc&) { - // expected - } - mt->revert_flushed_memory(); - } while (injector.failed()); + memory::with_allocation_failures([&] { + auto revert = defer([&] { + mt->revert_flushed_memory(); + }); + assert_that(mt->make_flush_reader(s, default_priority_class())) + .produces(ms); + }); }); } @@ -496,18 +482,10 @@ SEASTAR_TEST_CASE(test_exception_safety_of_single_partition_reads) { mt->apply(m); } - auto& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { - try { - injector.fail_after(i++); - assert_that(mt->make_flat_reader(s, tests::make_permit(), dht::partition_range::make_singular(ms[1].decorated_key()))) - .produces(ms[1]); - injector.cancel(); - } catch (const std::bad_alloc&) { - // expected - } - } while (injector.failed()); + memory::with_allocation_failures([&] { + assert_that(mt->make_flat_reader(s, tests::make_permit(), dht::partition_range::make_singular(ms[1].decorated_key()))) + .produces(ms[1]); + }); }); } diff --git a/test/boost/mutation_test.cc b/test/boost/mutation_test.cc index 9c6666295b..7f29fafb44 100644 --- a/test/boost/mutation_test.cc +++ b/test/boost/mutation_test.cc @@ -473,17 +473,9 @@ SEASTAR_THREAD_TEST_CASE(test_large_collection_serialization_exception_safety) { // We need an undisturbed run first to create all thread_local variables. cmd.serialize(*collection_type); - auto& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { - try { - injector.fail_after(i++); - cmd.serialize(*collection_type); - injector.cancel(); - } catch (const std::bad_alloc&) { - // expected - } - } while (injector.failed()); + memory::with_allocation_failures([&] { + cmd.serialize(*collection_type); + }); } SEASTAR_TEST_CASE(test_multiple_memtables_one_partition) { @@ -1010,18 +1002,10 @@ SEASTAR_TEST_CASE(test_apply_monotonically_is_monotonic) { auto expected = target + second; - auto& injector = memory::local_failure_injector(); - size_t fail_offset = 0; - do { - mutation m = target; - auto m2 = mutation_partition(*m.schema(), second.partition()); - injector.fail_after(fail_offset++); - try { - m.partition().apply_monotonically(*m.schema(), std::move(m2), no_cache_tracker, app_stats); - injector.cancel(); - assert_that(m).is_equal_to(expected) - .has_same_continuity(expected); - } catch (const std::bad_alloc&) { + mutation m = target; + auto m2 = mutation_partition(*m.schema(), second.partition()); + memory::with_allocation_failures([&] { + auto d = defer([&] { auto&& s = *gen.schema(); auto c1 = m.partition().get_continuity(s); auto c2 = m2.get_continuity(s); @@ -1036,8 +1020,11 @@ SEASTAR_TEST_CASE(test_apply_monotonically_is_monotonic) { } m.partition().apply_monotonically(*m.schema(), std::move(m2), no_cache_tracker, app_stats); assert_that(m).is_equal_to(expected); - } - } while (injector.failed()); + }); + m.partition().apply_monotonically(*m.schema(), std::move(m2), no_cache_tracker, app_stats); + d.cancel(); + }); + assert_that(m).is_equal_to(expected).has_same_continuity(expected); }); }; diff --git a/test/boost/range_tombstone_list_test.cc b/test/boost/range_tombstone_list_test.cc index d4e9d56fa5..3566e7a92e 100644 --- a/test/boost/range_tombstone_list_test.cc +++ b/test/boost/range_tombstone_list_test.cc @@ -30,6 +30,8 @@ #include "test/boost/range_tombstone_list_assertions.hh" #include "test/lib/log.hh" +#include + static thread_local schema_ptr s = schema_builder("ks", "cf") .with_column("pk", int32_type, column_kind::partition_key) .with_column("ck1", int32_type, column_kind::clustering_key) @@ -837,33 +839,26 @@ BOOST_AUTO_TEST_CASE(test_exception_safety) { expected.apply(*s, to_apply); // test apply_monotonically() - auto&& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { + memory::with_allocation_failures([&] () { auto list = original; - try { - injector.fail_after(i++); - list.apply_monotonically(*s, to_apply); - injector.cancel(); - assert_that(*s, list).is_equal_to(expected); - } catch (const std::bad_alloc&) { // expected + auto d = defer([&] { + memory::disable_failure_guard dfg; assert_that(*s, list).has_no_less_information_than(original); - } - } while (injector.failed()); + }); + list.apply_monotonically(*s, to_apply); + assert_that(*s, list).is_equal_to(expected); + }); // test apply_reversibly() - i = 0; - do { + memory::with_allocation_failures([&] () { auto list = original; - try { - injector.fail_after(i++); - list.apply_reversibly(*s, to_apply).cancel(); - injector.cancel(); - assert_that(*s, list).is_equal_to(expected); - } catch (const std::bad_alloc&) { // expected + auto d = defer([&] () { + memory::disable_failure_guard dfg; assert_that(*s, list).is_equal_to_either(original, expected); - } - } while (injector.failed()); + }); + list.apply_reversibly(*s, to_apply).cancel(); + assert_that(*s, list).is_equal_to(expected); + }); } BOOST_AUTO_TEST_CASE(test_accumulator) { diff --git a/test/boost/row_cache_test.cc b/test/boost/row_cache_test.cc index 52b2f6bf72..ae6688a382 100644 --- a/test/boost/row_cache_test.cc +++ b/test/boost/row_cache_test.cc @@ -2280,11 +2280,10 @@ SEASTAR_TEST_CASE(test_exception_safety_of_update_from_memtable) { orig.push_back(muts[3]); orig.push_back(muts[4]); - auto&& injector = memory::local_failure_injector(); - uint64_t i = 0; - do { - memtable_snapshot_source underlying(s.schema()); + memtable_snapshot_source underlying(s.schema()); + memory::with_allocation_failures([&] { for (auto&& m : orig) { + memory::disable_failure_guard dfg; underlying.apply(m); } @@ -2304,41 +2303,8 @@ SEASTAR_TEST_CASE(test_exception_safety_of_update_from_memtable) { auto rd1_v1 = assert_that(make_reader(population_range)); std::optional snap; - try { - auto mt = make_lw_shared(cache.schema()); - for (auto&& m : muts2) { - mt->apply(m); - } - - injector.fail_after(i++); - - // Make snapshot on pkeys[2] - auto pr = dht::partition_range::make_singular(pkeys[2]); - snap = mt->make_flat_reader(s.schema(), tests::make_permit(), pr); - snap->set_max_buffer_size(1); - snap->fill_buffer(db::no_timeout).get(); - - cache.update([&] { - auto mt2 = make_lw_shared(cache.schema()); - for (auto&& m : muts2) { - mt2->apply(m); - } - underlying.apply(std::move(mt2)); - }, *mt).get(); - - injector.cancel(); - - assert_that(cache.make_reader(cache.schema(), tests::make_permit())) - .produces(muts2) - .produces_end_of_stream(); - - rd1_v1.produces(muts[0]) - .produces(muts2[1]) - .produces(muts2[2]) - .produces(muts2[3]) - .produces_end_of_stream(); - } catch (const std::bad_alloc&) { - // expected + auto d = defer([&] { + memory::disable_failure_guard dfg; assert_that(cache.make_reader(cache.schema(), tests::make_permit())) .produces(orig) .produces_end_of_stream(); @@ -2346,8 +2312,39 @@ SEASTAR_TEST_CASE(test_exception_safety_of_update_from_memtable) { rd1_v1.produces(muts[0]) .produces(muts[3]) .produces_end_of_stream(); + }); + + auto mt = make_lw_shared(cache.schema()); + for (auto&& m : muts2) { + mt->apply(m); } - } while (injector.failed()); + + // Make snapshot on pkeys[2] + auto pr = dht::partition_range::make_singular(pkeys[2]); + snap = mt->make_flat_reader(s.schema(), tests::make_permit(), pr); + snap->set_max_buffer_size(1); + snap->fill_buffer(db::no_timeout).get(); + + cache.update([&] { + auto mt2 = make_lw_shared(cache.schema()); + for (auto&& m : muts2) { + mt2->apply(m); + } + underlying.apply(std::move(mt2)); + }, *mt).get(); + + d.cancel(); + + assert_that(cache.make_reader(cache.schema(), tests::make_permit())) + .produces(muts2) + .produces_end_of_stream(); + + rd1_v1.produces(muts[0]) + .produces(muts2[1]) + .produces(muts2[2]) + .produces(muts2[3]) + .produces_end_of_stream(); + }); tracker.cleaner().drain().get(); BOOST_REQUIRE_EQUAL(0, tracker.get_stats().rows); BOOST_REQUIRE_EQUAL(0, tracker.get_stats().partitions); @@ -2365,50 +2362,39 @@ SEASTAR_TEST_CASE(test_exception_safety_of_reads) { underlying.apply(mut); row_cache cache(s, snapshot_source([&] { return underlying(); }), tracker); - auto&& injector = memory::local_failure_injector(); auto run_queries = [&] { auto slice = partition_slice_builder(*s).with_ranges(gen.make_random_ranges(3)).build(); auto&& ranges = slice.row_ranges(*s, mut.key()); - uint64_t i = 0; - while (true) { - try { - injector.fail_after(i++); - auto rd = cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice); - auto got_opt = read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0(); - BOOST_REQUIRE(got_opt); - BOOST_REQUIRE(!read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0()); - injector.cancel(); - assert_that(*got_opt).is_equal_to(mut, ranges); - assert_that(cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice)) - .produces(mut, ranges); + memory::with_allocation_failures([&] { + auto rd = cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice); + auto got_opt = read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0(); + BOOST_REQUIRE(got_opt); + BOOST_REQUIRE(!read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0()); - if (!injector.failed()) { - break; - } - } catch (const std::bad_alloc&) { - // expected - } - } + assert_that(*got_opt).is_equal_to(mut, ranges); + assert_that(cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice)) + .produces(mut, ranges); + }); }; auto run_query = [&] { auto slice = partition_slice_builder(*s).with_ranges(gen.make_random_ranges(3)).build(); auto&& ranges = slice.row_ranges(*s, mut.key()); - injector.fail_after(0); - assert_that(cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice)) - .produces(mut, ranges); - injector.cancel(); + memory::with_allocation_failures([&] { + assert_that(cache.make_reader(s, tests::make_permit(), query::full_partition_range, slice)) + .produces(mut, ranges); + }); }; run_queries(); + auto&& injector = memory::local_failure_injector(); injector.run_with_callback([&] { if (tracker.region().reclaiming_enabled()) { tracker.region().full_compaction(); } - injector.fail_after(0); }, run_query); injector.run_with_callback([&] { @@ -2436,36 +2422,26 @@ SEASTAR_TEST_CASE(test_exception_safety_of_transitioning_from_underlying_read_to row_cache cache(s.schema(), snapshot_source([&] { return underlying(); }), tracker); - auto&& injector = memory::local_failure_injector(); - auto slice = partition_slice_builder(*s.schema()) .with_range(s.make_ckey_range(0, 1)) .with_range(s.make_ckey_range(3, 6)) .build(); - uint64_t i = 0; - while (true) { - try { + memory::with_allocation_failures([&] { + { + memory::disable_failure_guard dfg; cache.evict(); populate_range(cache, pr, s.make_ckey_range(6, 10)); - - injector.fail_after(i++); - auto rd = cache.make_reader(s.schema(), tests::make_permit(), pr, slice); - auto got_opt = read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0(); - BOOST_REQUIRE(got_opt); - auto mfopt = rd(db::no_timeout).get0(); - BOOST_REQUIRE(!mfopt); - injector.cancel(); - - assert_that(*got_opt).is_equal_to(mut); - - if (!injector.failed()) { - break; - } - } catch (const std::bad_alloc&) { - // expected } - } + + auto rd = cache.make_reader(s.schema(), tests::make_permit(), pr, slice); + auto got_opt = read_mutation_from_flat_mutation_reader(rd, db::no_timeout).get0(); + BOOST_REQUIRE(got_opt); + auto mfopt = rd(db::no_timeout).get0(); + BOOST_REQUIRE(!mfopt); + + assert_that(*got_opt).is_equal_to(mut); + }); }); } @@ -2487,24 +2463,18 @@ SEASTAR_TEST_CASE(test_exception_safety_of_partition_scan) { row_cache cache(s.schema(), snapshot_source([&] { return underlying(); }), tracker); - auto&& injector = memory::local_failure_injector(); - - uint64_t i = 0; - do { - try { + memory::with_allocation_failures([&] { + { + memory::disable_failure_guard dfg; cache.evict(); populate_range(cache, dht::partition_range::make_singular(pkeys[1])); populate_range(cache, dht::partition_range::make({pkeys[3]}, {pkeys[5]})); - - injector.fail_after(i++); - assert_that(cache.make_reader(s.schema(), tests::make_permit())) - .produces(muts) - .produces_end_of_stream(); - injector.cancel(); - } catch (const std::bad_alloc&) { - // expected } - } while (injector.failed()); + + assert_that(cache.make_reader(s.schema(), tests::make_permit())) + .produces(muts) + .produces_end_of_stream(); + }); }); }