| 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 |
|
| 15 |
#include "src/core/lib/promise/observable.h" |
| 16 |
|
| 17 |
#include <functional> |
| 18 |
|
| 19 |
#include "absl/status/status.h" |
| 20 |
#include "gmock/gmock.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
|
| 23 |
#include "src/core/lib/promise/promise.h" |
| 24 |
#include "src/core/lib/promise/seq.h" |
| 25 |
#include "test/core/promise/test_wakeup_schedulers.h" |
| 26 |
|
| 27 |
using testing::MockFunction; |
| 28 |
using testing::StrictMock; |
| 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 |
|
| 37 |
Promise<Result> Wait() { |
| 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 |
}, |
| 72 |
InlineWakeupScheduler(), |
| 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 |
}, |
| 90 |
[](absl::optional<int> i) { |
| 91 |
return i == 1 ? absl::OkStatus() |
| 92 |
: absl::UnknownError("expected 1"); |
| 93 |
}); |
| 94 |
}, |
| 95 |
InlineWakeupScheduler(), |
| 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 |
}, |
| 119 |
InlineWakeupScheduler(), |
| 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 |
} |