observable_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 <functional>
18 
19 #include "absl/status/status.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 
26 
29 
30 namespace grpc_core {
31 
32 // A simple Barrier type: stalls progress until it is 'cleared'.
33 class Barrier {
34  public:
35  struct Result {};
36 
38  return [this]() -> Poll<Result> {
39  MutexLock lock(&mu_);
40  if (cleared_) {
41  return Result{};
42  } else {
43  return wait_set_.AddPending(Activity::current()->MakeOwningWaker());
44  }
45  };
46  }
47 
48  void Clear() {
49  mu_.Lock();
50  cleared_ = true;
51  auto wakeup = wait_set_.TakeWakeupSet();
52  mu_.Unlock();
53  wakeup.Wakeup();
54  }
55 
56  private:
57  Mutex mu_;
58  WaitSet wait_set_ ABSL_GUARDED_BY(mu_);
59  bool cleared_ ABSL_GUARDED_BY(mu_) = false;
60 };
61 
62 TEST(ObservableTest, CanPushAndGet) {
63  StrictMock<MockFunction<void(absl::Status)>> on_done;
64  Observable<int> observable;
65  auto observer = observable.MakeObserver();
66  auto activity = MakeActivity(
67  [&observer]() {
68  return Seq(observer.Get(), [](absl::optional<int> i) {
69  return i == 42 ? absl::OkStatus() : absl::UnknownError("expected 42");
70  });
71  },
73  [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
74  EXPECT_CALL(on_done, Call(absl::OkStatus()));
75  observable.Push(42);
76 }
77 
78 TEST(ObservableTest, CanNext) {
79  StrictMock<MockFunction<void(absl::Status)>> on_done;
80  Observable<int> observable;
81  auto observer = observable.MakeObserver();
82  auto activity = MakeActivity(
83  [&observer]() {
84  return Seq(
85  observer.Get(),
86  [&observer](absl::optional<int> i) {
87  EXPECT_EQ(i, 42);
88  return observer.Next();
89  },
91  return i == 1 ? absl::OkStatus()
92  : absl::UnknownError("expected 1");
93  });
94  },
96  [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
97  observable.Push(42);
98  EXPECT_CALL(on_done, Call(absl::OkStatus()));
99  observable.Push(1);
100 }
101 
102 TEST(ObservableTest, CanWatch) {
103  StrictMock<MockFunction<void(absl::Status)>> on_done;
104  Observable<int> observable;
105  Barrier barrier;
106  auto activity = MakeActivity(
107  [&observable, &barrier]() {
108  return observable.Watch(
109  [&barrier](int x,
110  WatchCommitter* committer) -> Promise<absl::Status> {
111  if (x == 3) {
112  committer->Commit();
113  return Seq(barrier.Wait(), Immediate(absl::OkStatus()));
114  } else {
115  return Never<absl::Status>();
116  }
117  });
118  },
120  [&on_done](absl::Status status) { on_done.Call(std::move(status)); });
121  observable.Push(1);
122  observable.Push(2);
123  observable.Push(3);
124  observable.Push(4);
125  EXPECT_CALL(on_done, Call(absl::OkStatus()));
126  barrier.Clear();
127 }
128 
129 } // namespace grpc_core
130 
131 int main(int argc, char** argv) {
132  ::testing::InitGoogleTest(&argc, argv);
133  return RUN_ALL_TESTS();
134 }
testing::StrictMock
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-nice-strict.h:148
grpc_core::Never
Definition: promise/promise.h:53
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Activity::current
static Activity * current()
Definition: activity.h:124
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
main
int main(int argc, char **argv)
Definition: observable_test.cc:131
grpc_core::Observable::MakeObserver
Observer< T > MakeObserver()
Definition: observable.h:278
grpc_core::WatchCommitter::Commit
void Commit()
Definition: observable.h:49
grpc_core::Barrier
Definition: activity_test.cc:40
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::Barrier::Clear
void Clear()
Definition: observable_test.cc:48
seq.h
grpc_core::MakeActivity
ActivityPtr MakeActivity(Factory promise_factory, WakeupScheduler wakeup_scheduler, OnDone on_done, Contexts &&... contexts)
Definition: activity.h:522
grpc_core::Observable::Watch
promise_detail::ObservableWatch< T, F > Watch(F f)
Definition: observable.h:285
grpc_core::WatchCommitter
Definition: observable.h:47
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::Mutex::Lock
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: src/core/lib/gprpp/sync.h:69
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
grpc_core::WaitSet
Definition: wait_set.h:40
grpc_core::InlineWakeupScheduler
Definition: test_wakeup_schedulers.h:38
grpc_core::Barrier::Wait
Promise< Result > Wait()
Definition: observable_test.cc:37
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_core::Barrier::Result
Definition: activity_test.cc:42
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
promise.h
EXPECT_CALL
#define EXPECT_CALL(obj, call)
test_wakeup_schedulers.h
grpc_core::Barrier::ABSL_GUARDED_BY
WaitSet wait_set_ ABSL_GUARDED_BY(mu_)
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::Mutex::Unlock
void Unlock() ABSL_UNLOCK_FUNCTION()
Definition: src/core/lib/gprpp/sync.h:70
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
tests.unit._server_ssl_cert_config_test.Call
Call
Definition: _server_ssl_cert_config_test.py:70
grpc_core::Promise
std::function< Poll< T >()> Promise
Definition: promise/promise.h:37
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
grpc_core::Immediate
promise_detail::Immediate< T > Immediate(T value)
Definition: promise/promise.h:73
grpc_core::Observable::Push
void Push(T value)
Definition: observable.h:274
grpc_core::Seq
promise_detail::Seq< Functors... > Seq(Functors... functors)
Definition: seq.h:62
observable.h
grpc_core::Observable
Definition: observable.h:216
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::Barrier::mu_
Mutex mu_
Definition: activity_test.cc:64
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:45