proxygen
GlobalExecutorTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (GlobalExecutorTest, GlobalCPUExecutor)
 
 TEST (GlobalExecutorTest, GlobalIOExecutor)
 

Function Documentation

TEST ( GlobalExecutorTest  ,
GlobalCPUExecutor   
)

Definition at line 23 of file GlobalExecutorTest.cpp.

References add, count, dummy(), EXPECT_EQ, f, folly::getCPUExecutor(), and folly::setCPUExecutor().

23  {
24  class DummyExecutor : public folly::Executor {
25  public:
26  void add(Func f) override {
27  f();
28  count++;
29  }
30  int count{0};
31  };
32 
33  // The default CPU executor is a synchronous inline executor, lets verify
34  // that work we add is executed
35  auto count = 0;
36  auto f = [&]() { count++; };
37 
38  // Don't explode, we should create the default global CPUExecutor lazily here.
39  getCPUExecutor()->add(f);
40  EXPECT_EQ(1, count);
41 
42  {
43  auto dummy = std::make_shared<DummyExecutor>();
45  getCPUExecutor()->add(f);
46  // Make sure we were properly installed.
47  EXPECT_EQ(1, dummy->count);
48  EXPECT_EQ(2, count);
49  }
50 
51  // Don't explode, we should restore the default global CPUExecutor because our
52  // weak reference to dummy has expired
53  getCPUExecutor()->add(f);
54  EXPECT_EQ(3, count);
55 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
virtual void add(Func)=0
Function< void()> Func
Definition: Executor.h:27
void dummy()
int * count
std::shared_ptr< Executor > getCPUExecutor()
void setCPUExecutor(std::weak_ptr< Executor > executor)
TEST ( GlobalExecutorTest  ,
GlobalIOExecutor   
)

Definition at line 57 of file GlobalExecutorTest.cpp.

References add, count, dummy(), EXPECT_EQ, f, folly::getEventBase(), folly::getIOExecutor(), and folly::setIOExecutor().

57  {
58  class DummyExecutor : public IOExecutor {
59  public:
60  void add(Func) override {
61  count++;
62  }
63  folly::EventBase* getEventBase() override {
64  return nullptr;
65  }
66  int count{0};
67  };
68 
69  auto f = []() {};
70 
71  // Don't explode, we should create the default global IOExecutor lazily here.
72  getIOExecutor()->add(f);
73 
74  {
75  auto dummy = std::make_shared<DummyExecutor>();
77  getIOExecutor()->add(f);
78  // Make sure we were properly installed.
79  EXPECT_EQ(1, dummy->count);
80  }
81 
82  // Don't explode, we should restore the default global IOExecutor because our
83  // weak reference to dummy has expired
84  getIOExecutor()->add(f);
85 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
virtual void add(Func)=0
void setIOExecutor(std::weak_ptr< IOExecutor > executor)
Function< void()> Func
Definition: Executor.h:27
virtual folly::EventBase * getEventBase()=0
void dummy()
int * count
std::shared_ptr< IOExecutor > getIOExecutor()