Fix copyright lines
[folly.git] / folly / io / async / test / EventBaseTest.cpp
index b952640feea43e0b8b72c0038267c58858768a63..ac2ff0c156321a8531251ea73ae1bb62220a7b3c 100644 (file)
@@ -1,20 +1,17 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
+ * Copyright 2014-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include <folly/Memory.h>
@@ -1188,7 +1185,7 @@ TEST(EventBaseTest, RunInEventBaseThreadAndWait) {
   vector<unique_ptr<atomic<size_t>>> atoms(c);
   for (size_t i = 0; i < c; ++i) {
     auto& atom = atoms.at(i);
-    atom = make_unique<atomic<size_t>>(0);
+    atom = std::make_unique<atomic<size_t>>(0);
   }
   vector<thread> threads;
   for (size_t i = 0; i < c; ++i) {
@@ -1214,7 +1211,9 @@ TEST(EventBaseTest, RunInEventBaseThreadAndWait) {
     th.join();
   }
   size_t sum = 0;
-  for (auto& atom : atoms) sum += *atom;
+  for (auto& atom : atoms) {
+    sum += *atom;
+  }
   EXPECT_EQ(c, sum);
 }
 
@@ -1350,6 +1349,21 @@ TEST(EventBaseTest, RunInLoopStopLoop) {
   ASSERT_LE(c1.getCount(), 11);
 }
 
+TEST(EventBaseTest, messageAvailableException) {
+  auto deadManWalking = [] {
+    EventBase eventBase;
+    std::thread t([&] {
+      // Call this from another thread to force use of NotificationQueue in
+      // runInEventBaseThread
+      eventBase.runInEventBaseThread(
+          []() { throw std::runtime_error("boom"); });
+    });
+    t.join();
+    eventBase.loopForever();
+  };
+  EXPECT_DEATH(deadManWalking(), ".*");
+}
+
 TEST(EventBaseTest, TryRunningAfterTerminate) {
   EventBase eventBase;
   CountedLoopCallback c1(&eventBase, 1,
@@ -1400,7 +1414,7 @@ TEST(EventBaseTest, CancelRunInLoop) {
   // Run the loop
   eventBase.loop();
 
-  // cancelC1 and cancelC3 should have both fired after 10 iterations and
+  // cancelC1 and cancelC2 should have both fired after 10 iterations and
   // stopped re-installing themselves
   ASSERT_EQ(cancelC1.getCount(), 0);
   ASSERT_EQ(cancelC2.getCount(), 0);
@@ -1578,7 +1592,7 @@ TEST(EventBaseTest, IdleTime) {
   bool hostOverloaded = false;
 
   int latencyCallbacks = 0;
-  eventBase.setMaxLatency(6000, [&]() {
+  eventBase.setMaxLatency(6000us, [&]() {
     ++latencyCallbacks;
     if (latencyCallbacks != 1) {
       FAIL() << "Unexpected latency callback";
@@ -1596,7 +1610,7 @@ TEST(EventBaseTest, IdleTime) {
     ASSERT_EQ(6, tos0.getTimeouts());
     ASSERT_GE(6100, eventBase.getAvgLoopTime() - 1200);
     ASSERT_LE(6100, eventBase.getAvgLoopTime() + 1200);
-    tos.reset(new IdleTimeTimeoutSeries(&eventBase, timeouts));
+    tos = std::make_unique<IdleTimeTimeoutSeries>(&eventBase, timeouts);
   });
 
   // Kick things off with an "immedite" timeout
@@ -1650,7 +1664,7 @@ TEST(EventBaseTest, EventBaseThreadLoop) {
   });
   base.loop();
 
-  ASSERT_EQ(true, ran);
+  ASSERT_TRUE(ran);
 }
 
 TEST(EventBaseTest, EventBaseThreadName) {
@@ -1689,7 +1703,7 @@ TEST(EventBaseTest, RunBeforeLoopWait) {
 }
 
 class PipeHandler : public EventHandler {
-public:
+ public:
   PipeHandler(EventBase* eventBase, int fd)
     : EventHandler(eventBase, fd) {}
 
@@ -1736,7 +1750,7 @@ TEST(EventBaseTest, LoopKeepAlive) {
   EventBase evb;
 
   bool done = false;
-  std::thread t([&, loopKeepAlive = evb.loopKeepAlive() ]() mutable {
+  std::thread t([&, loopKeepAlive = evb.getKeepAliveToken() ]() mutable {
     /* sleep override */ std::this_thread::sleep_for(
         std::chrono::milliseconds(100));
     evb.runInEventBaseThread(
@@ -1757,7 +1771,7 @@ TEST(EventBaseTest, LoopKeepAliveInLoop) {
   std::thread t;
 
   evb.runInEventBaseThread([&] {
-    t = std::thread([&, loopKeepAlive = evb.loopKeepAlive() ]() mutable {
+    t = std::thread([&, loopKeepAlive = evb.getKeepAliveToken() ]() mutable {
       /* sleep override */ std::this_thread::sleep_for(
           std::chrono::milliseconds(100));
       evb.runInEventBaseThread(
@@ -1773,7 +1787,7 @@ TEST(EventBaseTest, LoopKeepAliveInLoop) {
 }
 
 TEST(EventBaseTest, LoopKeepAliveWithLoopForever) {
-  std::unique_ptr<EventBase> evb = folly::make_unique<EventBase>();
+  std::unique_ptr<EventBase> evb = std::make_unique<EventBase>();
 
   bool done = false;
 
@@ -1785,15 +1799,15 @@ TEST(EventBaseTest, LoopKeepAliveWithLoopForever) {
 
   {
     auto* ev = evb.get();
-    EventBase::LoopKeepAlive keepAlive;
+    Executor::KeepAlive keepAlive;
     ev->runInEventBaseThreadAndWait(
-        [&ev, &keepAlive] { keepAlive = ev->loopKeepAlive(); });
+        [&ev, &keepAlive] { keepAlive = ev->getKeepAliveToken(); });
     ASSERT_FALSE(done) << "Loop finished before we asked it to";
     ev->terminateLoopSoon();
     /* sleep override */
     std::this_thread::sleep_for(std::chrono::milliseconds(30));
     ASSERT_FALSE(done) << "Loop terminated early";
-    ev->runInEventBaseThread([&ev, keepAlive = std::move(keepAlive) ]{});
+    ev->runInEventBaseThread([keepAlive = std::move(keepAlive)]{});
   }
 
   evThread.join();
@@ -1801,13 +1815,13 @@ TEST(EventBaseTest, LoopKeepAliveWithLoopForever) {
 }
 
 TEST(EventBaseTest, LoopKeepAliveShutdown) {
-  auto evb = folly::make_unique<EventBase>();
+  auto evb = std::make_unique<EventBase>();
 
   bool done = false;
 
   std::thread t([
     &done,
-    loopKeepAlive = evb->loopKeepAlive(),
+    loopKeepAlive = evb->getKeepAliveToken(),
     evbPtr = evb.get()
   ]() mutable {
     /* sleep override */ std::this_thread::sleep_for(
@@ -1824,7 +1838,7 @@ TEST(EventBaseTest, LoopKeepAliveShutdown) {
 }
 
 TEST(EventBaseTest, LoopKeepAliveAtomic) {
-  auto evb = folly::make_unique<EventBase>();
+  auto evb = std::make_unique<EventBase>();
 
   static constexpr size_t kNumThreads = 100;
   static constexpr size_t kNumTasks = 100;
@@ -1839,9 +1853,9 @@ TEST(EventBaseTest, LoopKeepAliveAtomic) {
 
   for (size_t i = 0; i < kNumThreads; ++i) {
     ts.emplace_back([ evbPtr = evb.get(), batonPtr = batons[i].get(), &done ] {
-      std::vector<EventBase::LoopKeepAlive> keepAlives;
+      std::vector<Executor::KeepAlive> keepAlives;
       for (size_t j = 0; j < kNumTasks; ++j) {
-        keepAlives.emplace_back(evbPtr->loopKeepAliveAtomic());
+        keepAlives.emplace_back(evbPtr->getKeepAliveToken());
       }
 
       batonPtr->post();
@@ -1900,15 +1914,44 @@ TEST(EventBaseTest, DrivableExecutorTest) {
 TEST(EventBaseTest, RequestContextTest) {
   EventBase evb;
   auto defaultCtx = RequestContext::get();
+  std::weak_ptr<RequestContext> rctx_weak_ptr;
 
   {
     RequestContextScopeGuard rctx;
+    rctx_weak_ptr = RequestContext::saveContext();
     auto context = RequestContext::get();
     EXPECT_NE(defaultCtx, context);
     evb.runInLoop([context] { EXPECT_EQ(context, RequestContext::get()); });
+    evb.loop();
   }
 
+  // Ensure that RequestContext created for the scope has been released and
+  // deleted.
+  EXPECT_EQ(rctx_weak_ptr.expired(), true);
+
   EXPECT_EQ(defaultCtx, RequestContext::get());
-  evb.loop();
+}
+
+TEST(EventBaseTest, CancelLoopCallbackRequestContextTest) {
+  EventBase evb;
+  CountedLoopCallback c(&evb, 1);
+
+  auto defaultCtx = RequestContext::get();
+  EXPECT_EQ(defaultCtx, RequestContext::get());
+  std::weak_ptr<RequestContext> rctx_weak_ptr;
+
+  {
+    RequestContextScopeGuard rctx;
+    rctx_weak_ptr = RequestContext::saveContext();
+    auto context = RequestContext::get();
+    EXPECT_NE(defaultCtx, context);
+    evb.runInLoop(&c);
+    c.cancelLoopCallback();
+  }
+
+  // Ensure that RequestContext created for the scope has been released and
+  // deleted.
+  EXPECT_EQ(rctx_weak_ptr.expired(), true);
+
   EXPECT_EQ(defaultCtx, RequestContext::get());
 }