try_seq_metadata_test.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <gtest/gtest.h>
  15. #include "src/core/lib/promise/try_seq.h"
  16. #include "src/core/lib/resource_quota/resource_quota.h"
  17. #include "src/core/lib/transport/metadata_batch.h"
  18. namespace grpc_core {
  19. static auto* g_memory_allocator = new MemoryAllocator(
  20. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
  21. struct TestMap : public MetadataMap<TestMap, GrpcStatusMetadata> {
  22. using MetadataMap<TestMap, GrpcStatusMetadata>::MetadataMap;
  23. };
  24. TEST(PromiseTest, SucceedAndThenFail) {
  25. auto arena = MakeScopedArena(1024, g_memory_allocator);
  26. Poll<TestMap> r = TrySeq(
  27. [&arena] {
  28. TestMap m(arena.get());
  29. m.Set(GrpcStatusMetadata(), GRPC_STATUS_OK);
  30. return m;
  31. },
  32. [&arena]() {
  33. TestMap m(arena.get());
  34. m.Set(GrpcStatusMetadata(), GRPC_STATUS_UNAVAILABLE);
  35. return m;
  36. })();
  37. EXPECT_EQ(absl::get<TestMap>(r).get(GrpcStatusMetadata()),
  38. GRPC_STATUS_UNAVAILABLE);
  39. }
  40. } // namespace grpc_core
  41. int main(int argc, char** argv) {
  42. ::testing::InitGoogleTest(&argc, argv);
  43. return RUN_ALL_TESTS();
  44. }