[promises] Always assert that contexts are non-null (#31205)

* [promises] Always assert that contexts are non-null

* Automated change: Fix sanity tests

* fix

* fix

Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/31397/head
Craig Tiller 2 years ago committed by GitHub
parent 51ab596d44
commit b8b9fd7235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      BUILD
  2. 1
      CMakeLists.txt
  3. 3
      build_autogenerated.yaml
  4. 12
      src/core/lib/promise/context.h
  5. 4
      src/core/lib/transport/call_fragments.h
  6. 12
      test/core/promise/context_test.cc

@ -1381,7 +1381,7 @@ grpc_cc_library(
public_hdrs = [
"//src/core:lib/promise/context.h",
],
deps = ["gpr_platform"],
deps = ["gpr"],
)
grpc_cc_library(

1
CMakeLists.txt generated

@ -8543,6 +8543,7 @@ target_include_directories(context_test
target_link_libraries(context_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
gpr
)

@ -5291,7 +5291,8 @@ targets:
- src/core/lib/promise/context.h
src:
- test/core/promise/context_test.cc
deps: []
deps:
- gpr
uses_polling: false
- name: core_configuration_test
gtest: true

@ -19,6 +19,8 @@
#include <utility>
#include <grpc/support/log.h>
namespace grpc_core {
// To avoid accidentally creating context types, we require an explicit
@ -65,10 +67,18 @@ class WithContext {
} // namespace promise_detail
// Return true if a context of type T is currently active.
template <typename T>
bool HasContext() {
return promise_detail::Context<T>::get() != nullptr;
}
// Retrieve the current value of a context.
template <typename T>
T* GetContext() {
return promise_detail::Context<T>::get();
auto* p = promise_detail::Context<T>::get();
GPR_ASSERT(p != nullptr);
return p;
}
// Given a promise and a context, return a promise that has that context set.

@ -205,10 +205,10 @@ FragmentHandle<T>::FragmentHandle(const absl::Status& status) {
// need to do the hacky thing promise_based_filter does.
// This all goes away when promise_based_filter goes away, and this code will
// just assume there's an allocator present and move forward.
if (auto* allocator = GetContext<FragmentAllocator>()) {
if (HasContext<FragmentAllocator>()) {
handle_ = nullptr;
allocated_by_allocator_ = false;
*this = allocator->MakeServerMetadata();
*this = GetContext<FragmentAllocator>()->MakeServerMetadata();
} else {
handle_ = GetContext<Arena>()->New<T>(GetContext<Arena>());
allocated_by_allocator_ = false;

@ -26,11 +26,17 @@ template <>
struct ContextType<TestContext> {};
TEST(Context, WithContext) {
EXPECT_EQ(GetContext<TestContext>(), nullptr);
EXPECT_FALSE(HasContext<TestContext>());
TestContext test;
EXPECT_EQ(GetContext<TestContext>(), nullptr);
EXPECT_FALSE(HasContext<TestContext>());
EXPECT_EQ(test.done, false);
WithContext([]() { GetContext<TestContext>()->done = true; }, &test)();
WithContext(
[]() {
EXPECT_TRUE(HasContext<TestContext>());
GetContext<TestContext>()->done = true;
},
&test)();
EXPECT_FALSE(HasContext<TestContext>());
EXPECT_EQ(test.done, true);
}

Loading…
Cancel
Save