proxygen
NewThreadTest.cpp File Reference

Go to the source code of this file.

Classes

struct  countdownsingle
 
class  NewthreadExecutor
 

Typedefs

using NT = decltype(mi::new_thread())
 

Functions

auto make_time (mi::time_source<> &t, NT &ex)
 
 TEST_F (NewthreadExecutor, BlockingSubmitNow)
 
 TEST_F (NewthreadExecutor, BlockingGetNow)
 
 TEST_F (NewthreadExecutor, SubmissionsAreOrderedInTime)
 
 TEST_F (NewthreadExecutor, NowIsCalled)
 
 TEST_F (NewthreadExecutor, BlockingSubmit)
 
 TEST_F (NewthreadExecutor, BlockingGet)
 
 TEST_F (NewthreadExecutor, VirtualDerecursion)
 
 TEST_F (NewthreadExecutor, StaticDerecursion)
 
 TEST_F (NewthreadExecutor, UsedWithOn)
 
 TEST_F (NewthreadExecutor, UsedWithVia)
 

Typedef Documentation

using NT = decltype(mi::new_thread())

Definition at line 58 of file NewThreadTest.cpp.

Function Documentation

auto make_time ( mi::time_source<> &  t,
NT ex 
)
inline

Definition at line 60 of file NewThreadTest.cpp.

References folly::pushmi::time_source< E, TP >::make().

60  {
61  return t.make(mi::systemNowF{}, [ex]() { return ex; })();
62 }
requires Invocable< ExecutorFactory & > &&Executor< invoke_result_t< ExecutorFactory & > > &&NeverBlocking< invoke_result_t< ExecutorFactory & > > auto make(NF nf, ExecutorFactory ef)
Definition: time_source.h:545
TEST_F ( NewthreadExecutor  ,
BlockingSubmitNow   
)

Definition at line 78 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::Eq(), EXPECT_THAT, testing::Lt(), folly::pushmi::__adl::noexcept(), now(), start, and folly::pushmi::operators::transform.

78  {
79  auto signals = 0;
80  auto start = v::now(tnt_);
81  auto signaled = start;
82  tnt_ | op::transform([](auto tnt) { return tnt | ep::now(); }) |
84  [&](auto at) {
85  signaled = at;
86  signals += 100;
87  },
88  [&](auto) noexcept { signals += 1000; },
89  [&]() { signals += 10; });
90 
91  EXPECT_THAT(signals, Eq(110))
92  << "expected that the value and done signals are recorded once and the value signal did not drift much";
93  auto delay =
94  std::chrono::duration_cast<std::chrono::milliseconds>((signaled - start))
95  .count();
96  EXPECT_THAT(delay, Lt(1000)) << "The delay is " << delay;
97 }
internal::EqMatcher< T > Eq(T x)
std::chrono::steady_clock::time_point now()
requires E e noexcept(noexcept(s.error(std::move(e))))
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
internal::LtMatcher< Rhs > Lt(Rhs x)
PUSHMI_INLINE_VAR constexpr detail::blocking_submit_fn blocking_submit
Definition: submit.h:389
auto start
#define EXPECT_THAT(value, matcher)
TEST_F ( NewthreadExecutor  ,
BlockingGetNow   
)

Definition at line 99 of file NewThreadTest.cpp.

References EXPECT_THAT, testing::Lt(), now(), start, and folly::pushmi::operators::transform.

99  {
100  auto start = v::now(tnt_);
101  auto signaled = tnt_ | op::transform([](auto tnt) { return v::now(tnt); }) |
102  op::get<std::chrono::system_clock::time_point>;
103 
104  auto delay =
105  std::chrono::duration_cast<std::chrono::milliseconds>((signaled - start))
106  .count();
107 
108  EXPECT_THAT(delay, Lt(1000)) << "The delay is " << delay;
109 }
std::chrono::steady_clock::time_point now()
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
internal::LtMatcher< Rhs > Lt(Rhs x)
auto start
#define EXPECT_THAT(value, matcher)
TEST_F ( NewthreadExecutor  ,
SubmissionsAreOrderedInTime   
)

Definition at line 111 of file NewThreadTest.cpp.

References testing::ElementsAre(), EXPECT_THAT, now(), folly::pushmi::on_value(), submit, folly::pushmi::operators::submit_after, folly::pushmi::operators::submit_at, folly::detail::distributed_mutex::time(), folly::times(), and folly::fibers::yield().

111  {
112  std::vector<std::string> times;
113  std::atomic<int> pushed{0};
114  auto push = [&](int time) {
115  return v::on_value([&, time](auto) {
116  times.push_back(folly::to<std::string>(time));
117  ++pushed;
118  });
119  };
120  tnt_ | op::submit(v::on_value([push](auto tnt) {
121  auto now = tnt | ep::now();
122  tnt | op::submit_after(40ms, push(40)) |
123  op::submit_at(now + 10ms, push(10)) | op::submit_after(20ms, push(20)) |
124  op::submit_at(now + 10ms, push(11));
125  }));
126 
127  while (pushed.load() < 4) {
129  }
130 
131  EXPECT_THAT(times, ElementsAre("10", "11", "20", "40"))
132  << "expected that the items were pushed in time order not insertion order";
133 }
auto on_value(Fns...fns) -> on_value_fn< Fns... >
Definition: boosters.h:262
std::chrono::steady_clock::time_point now()
PUSHMI_INLINE_VAR constexpr detail::submit_at_fn submit_at
Definition: submit.h:387
#define EXPECT_THAT(value, matcher)
Future< Unit > times(const int n, F &&thunk)
Definition: Future-inl.h:2348
PUSHMI_INLINE_VAR constexpr detail::submit_after_fn submit_after
Definition: submit.h:388
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
std::chrono::nanoseconds time()
TEST_F ( NewthreadExecutor  ,
NowIsCalled   
)

Definition at line 135 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::Eq(), EXPECT_THAT, and now().

135  {
136  bool done = false;
137  tnt_ | ep::now();
138  tnt_ | op::blocking_submit([&](auto tnt) {
139  tnt | ep::now();
140  done = true;
141  });
142 
143  EXPECT_THAT(done, Eq(true)) << "exptected that both calls to now() complete";
144 }
internal::EqMatcher< T > Eq(T x)
std::chrono::steady_clock::time_point now()
PUSHMI_INLINE_VAR constexpr detail::blocking_submit_fn blocking_submit
Definition: submit.h:389
#define EXPECT_THAT(value, matcher)
TEST_F ( NewthreadExecutor  ,
BlockingSubmit   
)

Definition at line 146 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::Eq(), EXPECT_THAT, folly::pushmi::__adl::noexcept(), and folly::pushmi::operators::transform.

146  {
147  auto signals = 0;
148  nt_ | op::transform([](auto) { return 42; }) |
150  [&](auto) { signals += 100; },
151  [&](auto) noexcept { signals += 1000; },
152  [&]() { signals += 10; });
153 
154  EXPECT_THAT(signals, Eq(110))
155  << "the value and done signals are recorded once";
156 }
internal::EqMatcher< T > Eq(T x)
requires E e noexcept(noexcept(s.error(std::move(e))))
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
PUSHMI_INLINE_VAR constexpr detail::blocking_submit_fn blocking_submit
Definition: submit.h:389
#define EXPECT_THAT(value, matcher)
TEST_F ( NewthreadExecutor  ,
BlockingGet   
)

Definition at line 158 of file NewThreadTest.cpp.

References testing::Eq(), EXPECT_THAT, and folly::pushmi::operators::transform.

158  {
159  auto v = nt_ | op::transform([](auto) { return 42; }) | op::get<int>;
160 
161  EXPECT_THAT(v, Eq(42)) << "expected that the result would be different";
162 }
internal::EqMatcher< T > Eq(T x)
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
#define EXPECT_THAT(value, matcher)
TEST_F ( NewthreadExecutor  ,
VirtualDerecursion   
)

Definition at line 164 of file NewThreadTest.cpp.

References counter.

164  {
165  int counter = 100'000;
166  std::function<void(::folly::pushmi::any_executor_ref<> exec)> recurse;
167  recurse = [&](::folly::pushmi::any_executor_ref<> nt) {
168  if (--counter <= 0)
169  return;
170  nt | op::submit(recurse);
171  };
172  nt_ | op::blocking_submit([&](auto nt) { recurse(nt); });
173 
174  EXPECT_THAT(counter, Eq(0))
175  << "expected that all nested submissions complete";
176 }
std::atomic< int > counter
TEST_F ( NewthreadExecutor  ,
StaticDerecursion   
)

Definition at line 178 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::Eq(), EXPECT_THAT, and single.

178  {
179  int counter = 100'000;
180  countdownsingle single{counter};
181  nt_ | op::blocking_submit(single);
182 
183  EXPECT_THAT(counter, Eq(0))
184  << "expected that all nested submissions complete";
185 }
std::atomic< int > counter
TEST_F ( NewthreadExecutor  ,
UsedWithOn   
)

Definition at line 187 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::ElementsAre(), EXPECT_THAT, folly::pushmi::make_single_sender, max, min, folly::pushmi::operators::on, folly::pushmi::on_value(), folly::pushmi::set_done, folly::pushmi::set_value, and values().

187  {
188  std::vector<std::string> values;
189  auto sender = ::folly::pushmi::make_single_sender([](auto out) {
190  ::folly::pushmi::set_value(out, 2.0);
192  // ignored
196  });
197  sender | op::on([&]() { return nt_; }) |
199  [&](auto v) { values.push_back(folly::to<std::string>(v)); }));
200 
201  EXPECT_THAT(values, ElementsAre(folly::to<std::string>(2.0)))
202  << "expected that only the first item was pushed";
203 }
auto on_value(Fns...fns) -> on_value_fn< Fns... >
Definition: boosters.h:262
LogLevel max
Definition: LogLevel.cpp:31
PUSHMI_INLINE_VAR constexpr detail::blocking_submit_fn blocking_submit
Definition: submit.h:389
LogLevel min
Definition: LogLevel.cpp:30
#define EXPECT_THAT(value, matcher)
PUSHMI_INLINE_VAR constexpr __adl::set_value_fn set_value
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
PUSHMI_INLINE_VAR constexpr detail::on_fn on
Definition: on.h:100
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::make_single_sender_fn make_single_sender
std::vector< int > values(1'000)
PUSHMI_INLINE_VAR constexpr __adl::set_done_fn set_done
TEST_F ( NewthreadExecutor  ,
UsedWithVia   
)

Definition at line 205 of file NewThreadTest.cpp.

References folly::pushmi::operators::blocking_submit, testing::ElementsAre(), EXPECT_THAT, folly::pushmi::make_single_sender, max, min, folly::pushmi::on_value(), folly::pushmi::set_done, folly::pushmi::set_value, folly::pushmi::strands(), values(), and folly::pushmi::operators::via.

205  {
206  std::vector<std::string> values;
207  auto sender = ::folly::pushmi::make_single_sender([](auto out) {
208  ::folly::pushmi::set_value(out, 2.0);
210  // ignored
214  });
215  sender | op::via(mi::strands(nt_)) |
217  [&](auto v) { values.push_back(folly::to<std::string>(v)); }));
218 
219  EXPECT_THAT(values, ElementsAre(folly::to<std::string>(2.0)))
220  << "expected that only the first item was pushed";
221 }
requires Invocable< ExecutorFactory & > &&Executor< invoke_result_t< ExecutorFactory & > > &&ConcurrentSequence< invoke_result_t< ExecutorFactory & > > auto strands(ExecutorFactory ef)
Definition: strand.h:246
auto on_value(Fns...fns) -> on_value_fn< Fns... >
Definition: boosters.h:262
LogLevel max
Definition: LogLevel.cpp:31
PUSHMI_INLINE_VAR constexpr detail::via_fn via
Definition: via.h:166
PUSHMI_INLINE_VAR constexpr detail::blocking_submit_fn blocking_submit
Definition: submit.h:389
LogLevel min
Definition: LogLevel.cpp:30
#define EXPECT_THAT(value, matcher)
PUSHMI_INLINE_VAR constexpr __adl::set_value_fn set_value
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::make_single_sender_fn make_single_sender
std::vector< int > values(1'000)
PUSHMI_INLINE_VAR constexpr __adl::set_done_fn set_done