32 auto waitingObserver =
makeObserver([observer, &baton]() {
55 auto waitingObserver =
makeObserver([observer, &baton]() {
76 return std::make_shared<int>(**
child + 2);
80 [observer1, observer2]() {
return (**observer1) * (**observer2); });
82 EXPECT_EQ(43 * 44, *observer.getSnapshot());
85 auto waitingObserver =
makeObserver([observer, &baton]() {
94 EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
100 struct ExpectedException {};
103 []() -> std::shared_ptr<int> {
throw ExpectedException(); }),
108 makeObserver([]() -> std::shared_ptr<int> {
return nullptr; }),
115 auto value = **innerObserver;
117 if (
value % 2 != 0) {
121 throw std::logic_error(
"I prefer odd numbers");
125 auto waitingObserver =
makeObserver([oddObserver, &baton]() {
137 EXPECT_FALSE(baton.try_wait_for(std::chrono::seconds{1}));
144 EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
154 auto observerA =
makeObserver([observer, &observerB]() {
155 auto value = **observer;
162 observerB =
makeObserver([observerA]() {
return **observerA; });
164 auto collectObserver =
makeObserver([observer, observerA, &observerB]() {
165 auto value = **observer;
166 auto valueA = **observerA;
167 auto valueB = ***observerB;
176 }
else if (
value == 2) {
188 auto waitingObserver =
makeObserver([collectObserver, &baton]() {
197 for (
size_t i = 1;
i <= 3; ++
i) {
200 EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
210 auto values = std::make_shared<folly::Synchronized<std::vector<int>>>();
214 values->withWLock([&](std::vector<int>& vals) { vals.push_back(
value); });
219 values->withRLock([](
const std::vector<int>& vals) {
224 constexpr
size_t numIters = 10000;
226 for (
size_t i = 1;
i <= numIters; ++
i) {
230 while (**observer != numIters * 10) {
234 values->withRLock([numIters = numIters](
const std::vector<int>& vals) {
241 for (
auto value : vals) {
245 for (
size_t i = 0;
i < vals.size() - 1; ++
i) {
252 auto createTLObserver = [](
int value) {
257 std::make_unique<folly::observer::TLObserver<int>>(createTLObserver(42));
259 k = std::make_unique<folly::observer::TLObserver<int>>(createTLObserver(41));
264 static auto mainThreadId = std::this_thread::get_id();
265 static std::function<void()> updatesCob;
266 static bool slowGet =
false;
267 static std::atomic<size_t> getCallsStart{0};
268 static std::atomic<size_t> getCallsFinish{0};
272 EXPECT_EQ(mainThreadId, std::this_thread::get_id());
276 using element_type = int;
277 static std::shared_ptr<const int>
get(Observable&) {
280 std::this_thread::sleep_for(
281 std::chrono::seconds{2});
284 return std::make_shared<const int>(42);
287 static void subscribe(Observable&, std::function<
void()> cob) {
291 static void unsubscribe(Observable&) {}
294 std::thread cobThread;
308 cobThread = std::thread([] { updatesCob(); });
309 std::this_thread::sleep_for(std::chrono::seconds{1});
326 int callbackValue = 0;
327 size_t callbackCallsCount = 0;
329 auto callbackHandle =
331 ++callbackCallsCount;
332 callbackValue = *snapshot;
346 callbackHandle.cancel();
#define EXPECT_LE(val1, val2)
int makeObserverRecursion(int n)
#define EXPECT_THROW(statement, expected_exception)
void subscribe(uint32_t iters, int N)
#define EXPECT_EQ(val1, val2)
constexpr detail::Map< Move > move
Observer< T > getObserver()
FOLLY_ALWAYS_INLINE bool timed_wait(const std::chrono::duration< Rep, Period > &timeout) noexcept
Alias to try_wait_for. Deprecated.
Observer< observer_detail::ResultOfUnwrapSharedPtr< F > > makeObserver(F &&creator)
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
TLObserver< T > makeTLObserver(Observer< T > observer)
#define EXPECT_TRUE(condition)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
folly::Function< void()> child
#define EXPECT_FALSE(condition)
#define EXPECT_LT(val1, val2)
TEST(SequencedExecutor, CPUThreadPoolExecutor)
std::vector< int > values(1'000)