for_each_test.cc
Go to the documentation of this file.
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 
16 
17 #include <memory>
18 
19 #include "absl/memory/memory.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 
24 
36 
37 using testing::Mock;
40 
41 namespace grpc_core {
42 
44  ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
45 
46 TEST(ForEachTest, SendThriceWithPipe) {
47  int num_received = 0;
48  StrictMock<MockFunction<void(absl::Status)>> on_done;
49  EXPECT_CALL(on_done, Call(absl::OkStatus()));
51  [&num_received] {
52  Pipe<int> pipe;
53  auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>(
54  absl::make_unique<PipeSender<int>>(std::move(pipe.sender)));
55  return Map(
56  Join(
57  // Push 3 things into a pipe -- 1, 2, then 3 -- then close.
58  Seq((*sender)->Push(1), [sender] { return (*sender)->Push(2); },
59  [sender] { return (*sender)->Push(3); },
60  [sender] {
61  sender->reset();
62  return absl::OkStatus();
63  }),
64  // Use a ForEach loop to read them out and verify all values are
65  // seen.
67  [&num_received](int i) {
68  num_received++;
69  EXPECT_EQ(num_received, i);
70  return absl::OkStatus();
71  })),
72  JustElem<1>());
73  },
75  [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
77  Mock::VerifyAndClearExpectations(&on_done);
78  EXPECT_EQ(num_received, 3);
79 }
80 
81 } // namespace grpc_core
82 
83 int main(int argc, char** argv) {
84  ::testing::InitGoogleTest(&argc, argv);
85  return RUN_ALL_TESTS();
86 }
testing::StrictMock
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-nice-strict.h:148
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_core
Definition: call_metric_recorder.h:31
arena.h
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
status
absl::Status status
Definition: rls.cc:251
grpc_core::ForEach
for_each_detail::ForEach< Reader, Action > ForEach(Reader reader, Action action)
For each item acquired by calling Reader::Next, run the promise Action.
Definition: for_each.h:133
seq.h
for_each.h
grpc_core::Pipe::receiver
PipeReceiver< T > receiver
Definition: pipe.h:315
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::MakeActivity
ActivityPtr MakeActivity(Factory promise_factory, WakeupScheduler wakeup_scheduler, OnDone on_done, Contexts &&... contexts)
Definition: activity.h:522
map.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
pipe.h
grpc_core::Pipe::sender
PipeSender< T > sender
Definition: pipe.h:314
grpc_core::g_memory_allocator
static auto * g_memory_allocator
Definition: call_finalization_test.cc:24
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
main
int main(int argc, char **argv)
Definition: for_each_test.cc:83
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
resource_quota.h
grpc_core::JustElem
Definition: src/core/lib/promise/map.h:73
EXPECT_CALL
#define EXPECT_CALL(obj, call)
test_wakeup_schedulers.h
testing::Mock
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9549
join.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::Pipe
Definition: pipe.h:34
tests.unit._server_ssl_cert_config_test.Call
Call
Definition: _server_ssl_cert_config_test.py:70
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
testing::MockFunction
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:11854
ref_counted_ptr.h
memory_quota.h
grpc_core::NoWakeupScheduler
Definition: test_wakeup_schedulers.h:28
grpc_core::Map
promise_detail::Map< Promise, Fn > Map(Promise promise, Fn fn)
Definition: src/core/lib/promise/map.h:67
memory_allocator.h
grpc_core::Seq
promise_detail::Seq< Functors... > Seq(Functors... functors)
Definition: seq.h:62
activity.h
basic_seq.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::Join
promise_detail::Join< Promise... > Join(Promise... promises)
Definition: join.h:49


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:24