proxygen
TimesTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 #include <mutex>
19 #include <queue>
20 
21 #include <folly/futures/Future.h>
22 #include <folly/futures/Promise.h>
24 
25 using namespace folly;
26 
28  std::queue<std::shared_ptr<Promise<Unit>>>& ps,
29  std::mutex& ps_mutex) {
30  ps_mutex.lock();
31  auto p = ps.front();
32  ps.pop();
33  ps_mutex.unlock();
34  p->setValue();
35 }
36 
37 inline std::function<Future<Unit>(void)> makeThunk(
38  std::queue<std::shared_ptr<Promise<Unit>>>& ps,
39  int& interrupt,
40  std::mutex& ps_mutex) {
41  return [&]() mutable {
42  auto p = std::make_shared<Promise<Unit>>();
43  p->setInterruptHandler(
44  [&](exception_wrapper const& /* e */) { ++interrupt; });
45  ps_mutex.lock();
46  ps.push(p);
47  ps_mutex.unlock();
48 
49  return p->getFuture();
50  };
51 }
52 
53 inline std::function<bool(void)> makePred(int& i) {
54  return [&]() {
55  bool res = i < 3;
56  ++i;
57  return res;
58  };
59 }
60 
61 TEST(Times, success) {
62  std::queue<std::shared_ptr<Promise<Unit>>> ps;
63  std::mutex ps_mutex;
64  int interrupt = 0;
65  bool complete = false;
66  bool failure = false;
67 
68  auto thunk = makeThunk(ps, interrupt, ps_mutex);
69  auto f = folly::times(3, thunk)
70  .thenValue([&](auto&&) mutable { complete = true; })
71  .onError([&](FutureException& /* e */) { failure = true; });
72 
73  popAndFulfillPromise(ps, ps_mutex);
74  EXPECT_FALSE(complete);
75  EXPECT_FALSE(failure);
76 
77  popAndFulfillPromise(ps, ps_mutex);
78  EXPECT_FALSE(complete);
79  EXPECT_FALSE(failure);
80 
81  popAndFulfillPromise(ps, ps_mutex);
82  EXPECT_TRUE(f.isReady());
83  EXPECT_TRUE(complete);
84  EXPECT_FALSE(failure);
85 }
86 
87 TEST(Times, failure) {
88  std::queue<std::shared_ptr<Promise<Unit>>> ps;
89  std::mutex ps_mutex;
90  int interrupt = 0;
91  bool complete = false;
92  bool failure = false;
93 
94  auto thunk = makeThunk(ps, interrupt, ps_mutex);
95  auto f = folly::times(3, thunk)
96  .thenValue([&](auto&&) mutable { complete = true; })
97  .onError([&](FutureException& /* e */) { failure = true; });
98 
99  popAndFulfillPromise(ps, ps_mutex);
100  EXPECT_FALSE(complete);
101  EXPECT_FALSE(failure);
102 
103  ps_mutex.lock();
104  auto p2 = ps.front();
105  ps.pop();
106  ps_mutex.unlock();
107  FutureException eggs("eggs");
108  p2->setException(eggs);
109 
110  EXPECT_TRUE(f.isReady());
111  EXPECT_FALSE(complete);
112  EXPECT_TRUE(failure);
113 }
114 
115 TEST(Times, interrupt) {
116  std::queue<std::shared_ptr<Promise<Unit>>> ps;
117  std::mutex ps_mutex;
118  int interrupt = 0;
119  bool complete = false;
120  bool failure = false;
121 
122  auto thunk = makeThunk(ps, interrupt, ps_mutex);
123  auto f = folly::times(3, thunk)
124  .thenValue([&](auto&&) mutable { complete = true; })
125  .onError([&](FutureException& /* e */) { failure = true; });
126 
127  EXPECT_EQ(0, interrupt);
128 
129  FutureException eggs("eggs");
130  f.raise(eggs);
131 
132  for (int i = 1; i <= 3; ++i) {
133  EXPECT_EQ(1, interrupt);
134  popAndFulfillPromise(ps, ps_mutex);
135  }
136 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::function< bool(void)> makePred(int &i)
Definition: TimesTest.cpp:53
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static eggs_t eggs("eggs")
std::mutex mutex
std::function< Future< Unit >void)> makeThunk(std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
Definition: TimesTest.cpp:37
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Future< Unit > times(const int n, F &&thunk)
Definition: Future-inl.h:2348
TEST(SequencedExecutor, CPUThreadPoolExecutor)
void popAndFulfillPromise(std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
Definition: TimesTest.cpp:27