proxygen
SequencedExecutorTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 <future>
18 
22 
24 
25 namespace folly {
26 
28  // Add can be called from different threads, but it should be sequenced.
29  auto cpuExecutor = std::make_shared<CPUThreadPoolExecutor>(4);
30  auto producer =
32 
33  std::atomic<size_t> nextCallIndex{0};
34  std::atomic<bool> result{true};
35 
36  auto joinPromise = std::make_shared<std::promise<void>>();
37  auto joinFuture = joinPromise->get_future();
38 
39  constexpr size_t kNumCalls = 10000;
40  for (size_t callIndex = 0; callIndex < kNumCalls; ++callIndex) {
41  producer->add([&result, &executor, &nextCallIndex, callIndex, joinPromise] {
42  executor.add([&result, &nextCallIndex, callIndex, joinPromise] {
43  if (nextCallIndex != callIndex) {
44  result = false;
45  }
47  if (nextCallIndex.exchange(callIndex + 1) != callIndex) {
48  result = false;
49  }
50  });
51  });
52  }
53 
54  joinPromise.reset();
55  joinFuture.wait();
56 
57  return result;
58 }
59 
62 }
63 
66 }
67 
70  testExecutor(executor);
71 }
72 
73 TEST(SequencedExecutor, SerialCPUThreadPoolExecutor) {
74  auto cpuExecutor = std::make_shared<CPUThreadPoolExecutor>(4);
75  auto executor =
77  testExecutor(*executor);
78 }
79 
82 }
83 
84 } // namespace folly
EventBase * getEventBase()
virtual void add(Func)=0
bool isSequencedExecutor(folly::Executor &executor)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static KeepAlive< SerialExecutor > create(KeepAlive< Executor > parent=getKeepAliveToken(getCPUExecutor().get()))
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
void testExecutor(folly::Executor &executor)
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST(SequencedExecutor, CPUThreadPoolExecutor)