From 78e711c8112b8f8ce4fba180e0650ab87194512d Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 13 May 2024 09:49:07 -0700 Subject: [PATCH] x --- test/core/call/yodel/yodel_test.cc | 49 ++++++++++--------- test/core/call/yodel/yodel_test.h | 27 ++++++---- .../client_channel/client_channel_test.cc | 37 +++++++++++++- .../transport/test_suite/transport_test.cc | 10 ++-- 4 files changed, 84 insertions(+), 39 deletions(-) diff --git a/test/core/call/yodel/yodel_test.cc b/test/core/call/yodel/yodel_test.cc index 0bb8dc75bc7..d451d171ae2 100644 --- a/test/core/call/yodel/yodel_test.cc +++ b/test/core/call/yodel/yodel_test.cc @@ -14,8 +14,11 @@ #include "test/core/call/yodel/yodel_test.h" +#include + #include "absl/random/random.h" +#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/iomgr/timer_manager.h" #include "src/core/lib/resource_quota/resource_quota.h" @@ -103,13 +106,13 @@ void SimpleTestRegistry::RegisterTest( class YodelTest::WatchDog { public: explicit WatchDog(YodelTest* test) : test_(test) {} - ~WatchDog() { test_->event_engine_->Cancel(timer_); } + ~WatchDog() { test_->state_->event_engine->Cancel(timer_); } private: YodelTest* const test_; grpc_event_engine::experimental::EventEngine::TaskHandle const timer_{ - test_->event_engine_->RunAfter(Duration::Minutes(5), - [this]() { test_->Timeout(); })}; + test_->state_->event_engine->RunAfter(Duration::Minutes(5), + [this]() { test_->Timeout(); })}; }; /////////////////////////////////////////////////////////////////////////////// @@ -117,36 +120,38 @@ class YodelTest::WatchDog { YodelTest::YodelTest(const fuzzing_event_engine::Actions& actions, absl::BitGenRef rng) - : rng_(rng), - event_engine_{ - std::make_shared( - []() { - grpc_timer_manager_set_threading(false); - grpc_event_engine::experimental::FuzzingEventEngine::Options - options; - return options; - }(), - actions)}, - call_arena_allocator_{MakeRefCounted( - MakeResourceQuota("test-quota") - ->memory_quota() - ->CreateMemoryAllocator("test-allocator"), - 1024)} {} + : rng_(rng), actions_(actions) {} void YodelTest::RunTest() { + CoreConfiguration::Reset(); + InitCoreConfiguration(); + state_ = std::make_unique(); + state_->event_engine = + std::make_shared( + []() { + grpc_timer_manager_set_threading(false); + grpc_event_engine::experimental::FuzzingEventEngine::Options + options; + return options; + }(), + actions_); + state_->call_arena_allocator = MakeRefCounted( + ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( + "test-allocator"), + 1024); TestImpl(); EXPECT_EQ(pending_actions_.size(), 0) << "There are still pending actions: did you forget to call " "WaitForAllPendingWork()?"; Shutdown(); - event_engine_->TickUntilIdle(); - event_engine_->UnsetGlobalHooks(); + state_->event_engine->TickUntilIdle(); + state_->event_engine->UnsetGlobalHooks(); } void YodelTest::TickUntilTrue(absl::FunctionRef poll) { WatchDog watchdog(this); while (!poll()) { - event_engine_->Tick(); + state_->event_engine->Tick(); } } @@ -157,7 +162,7 @@ void YodelTest::WaitForAllPendingWork() { pending_actions_.pop(); continue; } - event_engine_->Tick(); + state_->event_engine->Tick(); } } diff --git a/test/core/call/yodel/yodel_test.h b/test/core/call/yodel/yodel_test.h index f0fa5e07868..34d095e7b20 100644 --- a/test/core/call/yodel/yodel_test.h +++ b/test/core/call/yodel/yodel_test.h @@ -340,7 +340,7 @@ class YodelTest : public ::testing::Test { yodel_detail::SequenceSpawner( name_and_location, yodel_detail::SpawnerForContext(std::move(context), - event_engine_.get()), + state_->event_engine.get()), [this](yodel_detail::NameAndLocation name_and_location, int step) { auto action = std::make_shared( name_and_location, step); @@ -351,9 +351,10 @@ class YodelTest : public ::testing::Test { } auto MakeCall(ClientMetadataHandle client_initial_metadata) { - auto* arena = call_arena_allocator_->MakeArena(); - return MakeCallPair(std::move(client_initial_metadata), event_engine_.get(), - arena, call_arena_allocator_, nullptr); + auto* arena = state_->call_arena_allocator->MakeArena(); + return MakeCallPair(std::move(client_initial_metadata), + state_->event_engine.get(), arena, + state_->call_arena_allocator, nullptr); } void WaitForAllPendingWork(); @@ -374,25 +375,33 @@ class YodelTest : public ::testing::Test { const std::shared_ptr& event_engine() { - return event_engine_; + return state_->event_engine; } private: class WatchDog; + struct State { + grpc::testing::TestGrpcScope grpc_scope; + std::shared_ptr + event_engine; + RefCountedPtr call_arena_allocator; + }; virtual void TestImpl() = 0; void Timeout(); void TickUntilTrue(absl::FunctionRef poll); + // Called before the test runs, after core configuration has been reset + // and before the event engine is started. + // This is a good time to register any custom core configuration builders. + virtual void InitCoreConfiguration() {} // Called after the test has run, but before the event engine is shut down. virtual void Shutdown() {} - grpc::testing::TestGrpcScope grpc_scope_; absl::BitGenRef rng_; - const std::shared_ptr - event_engine_; - const RefCountedPtr call_arena_allocator_; + fuzzing_event_engine::Actions actions_; + std::unique_ptr state_; std::queue> pending_actions_; }; diff --git a/test/core/client_channel/client_channel_test.cc b/test/core/client_channel/client_channel_test.cc index 39996582db6..a48d3d22466 100644 --- a/test/core/client_channel/client_channel_test.cc +++ b/test/core/client_channel/client_channel_test.cc @@ -18,12 +18,13 @@ #include "gtest/gtest.h" +#include "src/core/lib/config/core_configuration.h" #include "test/core/call/yodel/yodel_test.h" namespace grpc_core { namespace { -const absl::string_view kTestTarget = "test_target"; +const absl::string_view kTestTarget = "test:///target"; const absl::string_view kTestPath = "/test_method"; } // namespace @@ -50,6 +51,15 @@ class ClientChannelTest : public YodelTest { return client_initial_metadata; } + CallHandler TickUntilCallStarted() { + auto poll = [this]() -> Poll { + auto handler = call_destination_->PopHandler(); + if (handler.has_value()) return std::move(*handler); + return Pending(); + }; + return TickUntil(absl::FunctionRef()>(poll)); + } + private: class TestClientChannelFactory final : public ClientChannelFactory { public: @@ -62,10 +72,21 @@ class ClientChannelTest : public YodelTest { class TestCallDestination final : public UnstartedCallDestination { public: void StartCall(UnstartedCallHandler unstarted_call_handler) override { - Crash("unimplemented"); + handlers_.push( + unstarted_call_handler.V2HackToStartCallWithoutACallFilterStack()); + } + + absl::optional PopHandler() { + if (handlers_.empty()) return absl::nullopt; + auto handler = std::move(handlers_.front()); + handlers_.pop(); + return handler; } void Orphaned() override {} + + private: + std::queue handlers_; }; class TestCallDestinationFactory final @@ -93,6 +114,12 @@ class ClientChannelTest : public YodelTest { grpc_event_engine::experimental::EventEngine>(event_engine())); } + void InitCoreConfiguration() override { + CoreConfiguration::RegisterBuilder( + + ); + } + void Shutdown() override { channel_.reset(); picker_.reset(); @@ -115,4 +142,10 @@ CLIENT_CHANNEL_TEST(CreateCall) { channel.CreateCall(MakeClientInitialMetadata()); } +CLIENT_CHANNEL_TEST(StartCall) { + auto& channel = InitChannel(ChannelArgs()); + auto call_initiator = channel.CreateCall(MakeClientInitialMetadata()); + auto call_handler = TickUntilCallStarted(); +} + } // namespace grpc_core diff --git a/test/core/transport/test_suite/transport_test.cc b/test/core/transport/test_suite/transport_test.cc index 1dd3d9db500..12e2932faa7 100644 --- a/test/core/transport/test_suite/transport_test.cc +++ b/test/core/transport/test_suite/transport_test.cc @@ -58,12 +58,10 @@ void TransportTest::ServerCallDestination::StartCall( } absl::optional TransportTest::ServerCallDestination::PopHandler() { - if (!handlers_.empty()) { - auto handler = std::move(handlers_.front()); - handlers_.pop(); - return handler; - } - return absl::nullopt; + if (handlers_.empty()) return absl::nullopt; + auto handler = std::move(handlers_.front()); + handlers_.pop(); + return handler; } } // namespace grpc_core