mirror of https://github.com/grpc/grpc.git
Add common EventEngine Closure types (#30438)
* Add common EventEngine Closure types These have been written a few times, so let's standardize on a spot for them. * sanitize * formatpull/30443/head
parent
57e06148e5
commit
e18d335b0e
8 changed files with 234 additions and 3 deletions
@ -0,0 +1,71 @@ |
|||||||
|
// Copyright 2022 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_COMMON_CLOSURES_H |
||||||
|
#define GRPC_CORE_LIB_EVENT_ENGINE_COMMON_CLOSURES_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <utility> |
||||||
|
|
||||||
|
#include "absl/functional/any_invocable.h" |
||||||
|
|
||||||
|
#include <grpc/event_engine/event_engine.h> |
||||||
|
|
||||||
|
namespace grpc_event_engine { |
||||||
|
namespace experimental { |
||||||
|
|
||||||
|
class AnyInvocableClosure : public EventEngine::Closure { |
||||||
|
public: |
||||||
|
explicit AnyInvocableClosure(absl::AnyInvocable<void()> cb) |
||||||
|
: cb_(std::move(cb)) {} |
||||||
|
void Run() override { cb_(); } |
||||||
|
|
||||||
|
private: |
||||||
|
absl::AnyInvocable<void()> cb_; |
||||||
|
}; |
||||||
|
|
||||||
|
class SelfDeletingClosure : public EventEngine::Closure { |
||||||
|
public: |
||||||
|
// Creates a SelfDeletingClosure.
|
||||||
|
// The closure will be deleted after Run is called.
|
||||||
|
static Closure* Create(absl::AnyInvocable<void()> cb) { |
||||||
|
return new SelfDeletingClosure(std::move(cb), nullptr); |
||||||
|
} |
||||||
|
// Creates a SelfDeletingClosure with a custom destructor.
|
||||||
|
static Closure* Create(absl::AnyInvocable<void()> cb, |
||||||
|
absl::AnyInvocable<void()> dest_cb) { |
||||||
|
return new SelfDeletingClosure(std::move(cb), std::move(dest_cb)); |
||||||
|
} |
||||||
|
~SelfDeletingClosure() override { |
||||||
|
if (dest_cb_) dest_cb_(); |
||||||
|
}; |
||||||
|
|
||||||
|
void Run() override { |
||||||
|
cb_(); |
||||||
|
delete this; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
explicit SelfDeletingClosure(absl::AnyInvocable<void()> cb, |
||||||
|
absl::AnyInvocable<void()> dest_cb) |
||||||
|
: cb_(std::move(cb)), dest_cb_(std::move(dest_cb)) {} |
||||||
|
absl::AnyInvocable<void()> cb_; |
||||||
|
absl::AnyInvocable<void()> dest_cb_; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace experimental
|
||||||
|
} // namespace grpc_event_engine
|
||||||
|
|
||||||
|
#endif // GRPC_CORE_LIB_EVENT_ENGINE_COMMON_CLOSURES_H
|
@ -0,0 +1,63 @@ |
|||||||
|
// Copyright 2022 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/lib/event_engine/common_closures.h" |
||||||
|
|
||||||
|
#include <gtest/gtest.h> |
||||||
|
|
||||||
|
#include "src/core/lib/event_engine/promise.h" |
||||||
|
#include "test/core/util/test_config.h" |
||||||
|
|
||||||
|
using ::grpc_event_engine::experimental::AnyInvocableClosure; |
||||||
|
using ::grpc_event_engine::experimental::Promise; |
||||||
|
using ::grpc_event_engine::experimental::SelfDeletingClosure; |
||||||
|
|
||||||
|
class AnyInvocableClosureTest : public testing::Test {}; |
||||||
|
|
||||||
|
TEST_F(AnyInvocableClosureTest, CallsItsFunction) { |
||||||
|
Promise<bool> promise; |
||||||
|
AnyInvocableClosure closure([&promise] { promise.Set(true); }); |
||||||
|
closure.Run(); |
||||||
|
ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3))); |
||||||
|
} |
||||||
|
|
||||||
|
class SelfDeletingClosureTest : public testing::Test {}; |
||||||
|
|
||||||
|
TEST_F(SelfDeletingClosureTest, CallsItsFunction) { |
||||||
|
Promise<bool> promise; |
||||||
|
auto* closure = |
||||||
|
SelfDeletingClosure::Create([&promise] { promise.Set(true); }); |
||||||
|
closure->Run(); |
||||||
|
ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3))); |
||||||
|
// ASAN should catch if this closure is not deleted
|
||||||
|
} |
||||||
|
|
||||||
|
TEST_F(SelfDeletingClosureTest, CallsItsFunctionAndIsDestroyed) { |
||||||
|
Promise<bool> fn_called; |
||||||
|
Promise<bool> destroyed; |
||||||
|
auto* closure = |
||||||
|
SelfDeletingClosure::Create([&fn_called] { fn_called.Set(true); }, |
||||||
|
[&destroyed] { destroyed.Set(true); }); |
||||||
|
closure->Run(); |
||||||
|
ASSERT_TRUE(fn_called.WaitWithTimeout(absl::Seconds(3))); |
||||||
|
ASSERT_TRUE(destroyed.WaitWithTimeout(absl::Seconds(3))); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
testing::InitGoogleTest(&argc, argv); |
||||||
|
auto result = RUN_ALL_TESTS(); |
||||||
|
return result; |
||||||
|
} |
Loading…
Reference in new issue