suppress warnings in tests for deprecated functions
[folly.git] / folly / futures / test / InterruptTest.cpp
index 1963c0a7b17af50887531c4a09f4667fce989d2f..48ddc6da641600da267590719da43a0108598cb2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * 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.
  * limitations under the License.
  */
 
-#include <gtest/gtest.h>
-
 #include <folly/futures/Future.h>
 #include <folly/futures/Promise.h>
+#include <folly/portability/GTest.h>
+#include <folly/synchronization/Baton.h>
 
 using namespace folly;
 
 TEST(Interrupt, raise) {
-  std::runtime_error eggs("eggs");
-  Promise<void> p;
+  using eggs_t = std::runtime_error;
+  Promise<Unit> p;
   p.setInterruptHandler([&](const exception_wrapper& e) {
-    EXPECT_THROW(e.throwException(), decltype(eggs));
+    EXPECT_THROW(e.throw_exception(), eggs_t);
   });
-  p.getFuture().raise(eggs);
+  p.getFuture().raise(eggs_t("eggs"));
 }
 
 TEST(Interrupt, cancel) {
-  Promise<void> p;
+  Promise<Unit> p;
   p.setInterruptHandler([&](const exception_wrapper& e) {
-    EXPECT_THROW(e.throwException(), FutureCancellation);
+    EXPECT_THROW(e.throw_exception(), FutureCancellation);
   });
   p.getFuture().cancel();
 }
@@ -41,7 +41,7 @@ TEST(Interrupt, cancel) {
 TEST(Interrupt, handleThenInterrupt) {
   Promise<int> p;
   bool flag = false;
-  p.setInterruptHandler([&](const exception_wrapper& e) { flag = true; });
+  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
   p.getFuture().cancel();
   EXPECT_TRUE(flag);
 }
@@ -50,25 +50,34 @@ TEST(Interrupt, interruptThenHandle) {
   Promise<int> p;
   bool flag = false;
   p.getFuture().cancel();
-  p.setInterruptHandler([&](const exception_wrapper& e) { flag = true; });
+  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
   EXPECT_TRUE(flag);
 }
 
 TEST(Interrupt, interruptAfterFulfilNoop) {
-  Promise<void> p;
+  Promise<Unit> p;
   bool flag = false;
-  p.setInterruptHandler([&](const exception_wrapper& e) { flag = true; });
+  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
   p.setValue();
   p.getFuture().cancel();
   EXPECT_FALSE(flag);
 }
 
 TEST(Interrupt, secondInterruptNoop) {
-  Promise<void> p;
+  Promise<Unit> p;
   int count = 0;
-  p.setInterruptHandler([&](const exception_wrapper& e) { count++; });
+  p.setInterruptHandler([&](const exception_wrapper& /* e */) { count++; });
   auto f = p.getFuture();
   f.cancel();
   f.cancel();
   EXPECT_EQ(1, count);
 }
+
+TEST(Interrupt, withinTimedOut) {
+  Promise<int> p;
+  Baton<> done;
+  p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); });
+  p.getFuture().within(std::chrono::milliseconds(1));
+  // Give it 100ms to time out and call the interrupt handler
+  EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100)));
+}