#include #include #include #include #include #include #include #include #include #include #include namespace { struct NonDefaultResult { int value; explicit NonDefaultResult(int v) : value(v) {} }; } // namespace Void::Testing::Test testing_mock_session_reentrant_move_only_and_sequences() { using namespace Void::TestingMock; MockSession session; Sequence seq; // 1. Same-thread reentrant mock action works because actions run outside // internal locks. MockFunction reentrant{session, "reentrant"}; auto e1 = reentrant.try_expect(Eq(1)); Void::Testing::require(e1.has_value()); (*e1)->in_sequence(seq).invokes([&](int) -> int { return reentrant(2) + 1; }); auto e2 = reentrant.try_expect(Eq(2)); Void::Testing::require(e2.has_value()); (*e2)->in_sequence(seq).returns(41); // 2. Move-only argument and move-only return value via returns_once / // invokes_once. MockFunction(std::unique_ptr)> move_fn{session, "move_fn"}; auto me1 = move_fn.try_expect(NotNull>()); Void::Testing::require(me1.has_value()); (*me1)->invokes_once([bonus = std::make_unique(100)]( std::unique_ptr in) { return std::make_unique(*in + *bonus); }); auto me2 = move_fn.try_expect(IsNull>()); Void::Testing::require(me2.has_value()); (*me2)->returns_once(std::make_unique(7)); // 3. Matcher combinators: AllOf, AnyOf, NotOf, Ne, EndsWith. MockFunction combo{session, "combo"}; auto ce = combo.try_expect(AllOf( StartsWith("pre_"), EndsWith("_suf"), NotOf(Eq("pre_bad_suf")))); Void::Testing::require(ce.has_value()); (*ce)->returns(99).at_least(1); // Freeze session in SingleThread mode. Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value()); // Mutating a frozen session returns InvalidState. auto frozen_add = reentrant.try_expect(Eq(999)); Void::Testing::expect(!frozen_add.has_value()); // Execute reentrant sequenced calls. Void::Testing::expect_eq(reentrant(1), 42); // Execute move-only calls. auto r1 = move_fn(std::make_unique(23)); Void::Testing::require(r1 != nullptr); Void::Testing::expect_eq(*r1, 123); auto r2 = move_fn(nullptr); Void::Testing::require(r2 != nullptr); Void::Testing::expect_eq(*r2, 7); // Execute combinator match. Void::Testing::expect_eq(combo("pre_ok_suf"), 99); // verify_and_close() is idempotent and succeeds. auto v1 = verify_and_close(session); Void::Testing::require(v1.has_value()); Void::Testing::expect(v1->ok()); auto v2 = verify_and_close(session); Void::Testing::require(v2.has_value()); Void::Testing::expect(v2->ok()); } Void::Testing::Test testing_mock_prohibited_never_non_default_and_deadlock_check() { using namespace Void::TestingMock; MockSession session; MockFunction fn{session, "strict_fn"}; // Explicit never() checked before ordinary wildcard matcher. auto p = fn.try_expect(Eq(13)); Void::Testing::require(p.has_value()); (*p)->never(); auto ok = fn.try_expect(_()); Void::Testing::require(ok.has_value()); (*ok)->invokes([&](int x) -> NonDefaultResult { // Calling verify_and_close from inside an active action must return // WouldDeadlock instead of deadlocking. auto dl = verify_and_close(session); Void::Testing::expect(!dl.has_value()); return NonDefaultResult{x * 2}; }).times(1); Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value()); auto res = fn(5); Void::Testing::expect_eq(res.value, 10); // Prohibited call with non-default-constructible return type records // prohibited failure and throws InvocationAbortException. bool aborted = false; try { (void)fn(13); } catch (const InvocationAbortException&) { aborted = true; } Void::Testing::expect(aborted); auto report = verify_and_close(session); Void::Testing::require(report.has_value()); Void::Testing::expect(!report->ok()); Void::Testing::expect(!report->prohibited.empty()); } Void::Testing::Test testing_mock_concurrent_actions_and_bounds() { using namespace Void::TestingMock; MockLimits lim; lim.max_expectations = 4; lim.max_events = 8; MockSession session{lim}; MockFunction fn{session, "concurrent_fn"}; // Two separate expectations with 8 calls each, contested by 4 threads. auto e1 = fn.try_expect(Lt(100)); auto e2 = fn.try_expect(Lt(100)); Void::Testing::require(e1.has_value()); Void::Testing::require(e2.has_value()); (*e1)->invokes([](int x) { return x + 1; }).times(8); (*e2)->invokes([](int x) { return x + 2; }).times(8); // Enforce max_expectations = 4. auto e3 = fn.try_expect(Eq(200)); auto e4 = fn.try_expect(Eq(201)); auto e5 = fn.try_expect(Eq(202)); Void::Testing::require(e3.has_value()); Void::Testing::require(e4.has_value()); Void::Testing::expect(!e5.has_value()); (*e3)->at_most(1); (*e4)->at_most(1); Void::Testing::require( freeze(session, Concurrency::ConcurrentActions).has_value()); std::atomic sum{0}; std::vector threads; for (int t = 0; t < 4; ++t) { threads.emplace_back([&]() { for (int i = 0; i < 4; ++i) { sum.fetch_add(fn(10), std::memory_order_relaxed); } }); } for (auto& th : threads) { th.join(); } // 8 * 11 + 8 * 12 = 184 Void::Testing::expect_eq(sum.load(), 184); auto report = verify_and_close(session); Void::Testing::require(report.has_value()); Void::Testing::expect(report->ok()); Void::Testing::expect_eq(report->total_events, std::uint64_t{16}); Void::Testing::expect(report->events_truncated); Void::Testing::expect(report->events.size() <= lim.max_events); } Void::Testing::Test testing_mock_single_thread_detects_wrong_thread_and_matcher_exception() { using namespace Void::TestingMock; MockSession session; MockFunction fn{session, "st_fn"}; auto e = fn.try_expect(Pred([](const int& v) -> bool { if (v < 0) { throw std::runtime_error("boom-matcher"); } return v == 1; })); Void::Testing::require(e.has_value()); (*e)->returns(7).at_most(1); Void::Testing::require(freeze(session, Concurrency::SingleThread).has_value()); // Throwing matcher becomes MatcherFailure instead of crashing/aborting. (void)fn(-5); // Wrong-thread call under Concurrency::SingleThread records threading error. std::thread other([&]() { (void)fn(1); }); other.join(); auto report = verify_and_close(session); Void::Testing::require(report.has_value()); Void::Testing::expect(!report->ok()); Void::Testing::expect(!report->matcher_errors.empty()); Void::Testing::expect(!report->threading_errors.empty()); }