当前位置: 首页>>代码示例>>C++>>正文


C++ Promise::getFuture方法代码示例

本文整理汇总了C++中Promise::getFuture方法的典型用法代码示例。如果您正苦于以下问题:C++ Promise::getFuture方法的具体用法?C++ Promise::getFuture怎么用?C++ Promise::getFuture使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Promise的用法示例。


在下文中一共展示了Promise::getFuture方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

TEST(Future, unwrap) {
  Promise<int> a;
  Promise<int> b;

  auto fa = a.getFuture();
  auto fb = b.getFuture();

  bool flag1 = false;
  bool flag2 = false;

  // do a, then do b, and get the result of a + b.
  Future<int> f = fa.then([&](Try<int>&& ta) {
    auto va = ta.value();
    flag1 = true;
    return fb.then([va, &flag2](Try<int>&& tb) {
      flag2 = true;
      return va + tb.value();
    });
  });

  EXPECT_FALSE(flag1);
  EXPECT_FALSE(flag2);
  EXPECT_FALSE(f.isReady());

  a.setValue(3);
  EXPECT_TRUE(flag1);
  EXPECT_FALSE(flag2);
  EXPECT_FALSE(f.isReady());

  b.setValue(4);
  EXPECT_TRUE(flag1);
  EXPECT_TRUE(flag2);
  EXPECT_EQ(7, f.value());
}
开发者ID:SamSaffron,项目名称:DiscourseMobile,代码行数:34,代码来源:FutureTest.cpp

示例2: test_promise

void test_promise() {
  Promise<bool> p;

  auto f = p.getFuture();
  try {
    p.getFuture();
    ok(false, "should throw");
  } catch (const std::logic_error& exc) {
    ok(!strcmp(exc.what(), "Future already obtained"), "can't getFuture twice");
  }
  ok(!f.isReady(), "not yet ready");

  p.setValue(true);
  try {
    p.setValue(false);
    ok(false, "should throw");
  } catch (const std::logic_error& exc) {
    ok(!strcmp(exc.what(), "Promise already fulfilled"),
       "can't setValue twice");
  }

  ok(f.isReady(), "now ready");
  ok(f.get() == true, "got our true value");

  Promise<std::string> s;
  s.setException(std::make_exception_ptr(std::runtime_error("boo")));
  auto f2 = s.getFuture();
  ok(f2.result().hasError(), "holds an error");
  try {
    f2.get();
  } catch (const std::runtime_error& exc) {
    ok(!strcmp(exc.what(), "boo"), "has boo string");
  }
}
开发者ID:Stevenzwzhai,项目名称:watchman,代码行数:34,代码来源:FutureTest.cpp

示例3: collect

TEST(Collect, collectVariadicWithException) {
  Promise<bool> pb;
  Promise<int> pi;
  Future<bool> fb = pb.getFuture();
  Future<int> fi = pi.getFuture();
  auto f = collect(std::move(fb), std::move(fi));
  pb.setValue(true);
  EXPECT_FALSE(f.isReady());
  pi.setException(eggs);
  EXPECT_TRUE(f.isReady());
  EXPECT_TRUE(f.getTry().hasException());
  EXPECT_THROW(f.get(), eggs_t);
}
开发者ID:derek-zhang,项目名称:folly,代码行数:13,代码来源:CollectTest.cpp

示例4:

TEST(Promise, setWith) {
    {
        Promise<int> p;
        auto f = p.getFuture();
        p.setWith([] { return 42; });
        EXPECT_EQ(42, f.value());
    }
    {
        Promise<int> p;
        auto f = p.getFuture();
        p.setWith([]() -> int { throw eggs; });
        EXPECT_THROW(f.value(), eggs_t);
    }
}
开发者ID:BocaiFire,项目名称:folly,代码行数:14,代码来源:PromiseTest.cpp

示例5: sizeof

TEST(Future, finishBigLambda) {
  auto x = std::make_shared<int>(0);

  // bulk_data, to be captured in the lambda passed to Future::then.
  // This is meant to force that the lambda can't be stored inside
  // the Future object.
  std::array<char, sizeof(detail::Core<int>)> bulk_data = {0};

  // suppress gcc warning about bulk_data not being used
  EXPECT_EQ(bulk_data[0], 0);

  Promise<int> p;
  auto f = p.getFuture().then([x, bulk_data](Try<int>&& t) { *x = t.value(); });

  // The callback hasn't executed
  EXPECT_EQ(0, *x);

  // The callback has a reference to x
  EXPECT_EQ(2, x.use_count());

  p.setValue(42);

  // the callback has executed
  EXPECT_EQ(42, *x);

  // the callback has been destructed
  // and has released its reference to x
  EXPECT_EQ(1, x.use_count());
}
开发者ID:SamSaffron,项目名称:DiscourseMobile,代码行数:29,代码来源:FutureTest.cpp

示例6:

TEST(Interrupt, interruptThenHandle) {
  Promise<int> p;
  bool flag = false;
  p.getFuture().cancel();
  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
  EXPECT_TRUE(flag);
}
开发者ID:genorm,项目名称:folly,代码行数:7,代码来源:InterruptTest.cpp

示例7: t

TEST(Wait, wait) {
  Promise<int> p;
  Future<int> f = p.getFuture();
  std::atomic<bool> flag{false};
  std::atomic<int> result{1};
  std::atomic<std::thread::id> id;

  std::thread t([&](Future<int>&& tf){
      auto n = tf.then([&](Try<int> && t) {
          id = std::this_thread::get_id();
          return t.value();
        });
      flag = true;
      result.store(n.wait().value());
    },
    std::move(f)
    );
  while(!flag){}
  EXPECT_EQ(result.load(), 1);
  p.setValue(42);
  t.join();
  // validate that the callback ended up executing in this thread, which
  // is more to ensure that this test actually tests what it should
  EXPECT_EQ(id, std::this_thread::get_id());
  EXPECT_EQ(result.load(), 42);
}
开发者ID:BocaiFire,项目名称:folly,代码行数:26,代码来源:WaitTest.cpp

示例8: makeFuture

TEST(Future, thenTry) {
  bool flag = false;

  makeFuture<int>(42).then([&](Try<int>&& t) {
                              flag = true;
                              EXPECT_EQ(42, t.value());
                            });
  EXPECT_TRUE(flag); flag = false;

  makeFuture<int>(42)
    .then([](Try<int>&& t) { return t.value(); })
    .then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
  EXPECT_TRUE(flag); flag = false;

  makeFuture().then([&](Try<Unit>&& t) { flag = true; t.value(); });
  EXPECT_TRUE(flag); flag = false;

  Promise<Unit> p;
  auto f = p.getFuture().then([&](Try<Unit>&& /* t */) { flag = true; });
  EXPECT_FALSE(flag);
  EXPECT_FALSE(f.isReady());
  p.setValue();
  EXPECT_TRUE(flag);
  EXPECT_TRUE(f.isReady());
}
开发者ID:SamSaffron,项目名称:DiscourseMobile,代码行数:25,代码来源:FutureTest.cpp

示例9: TestData

TEST(Context, basic) {

  // Start a new context
  folly::RequestContextScopeGuard rctx;

  EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));

  // Set some test data
  RequestContext::get()->setContextData(
    "test",
    std::unique_ptr<TestData>(new TestData(10)));

  // Start a future
  Promise<Unit> p;
  auto future = p.getFuture().then([&]{
    // Check that the context followed the future
    EXPECT_TRUE(RequestContext::get() != nullptr);
    auto a = dynamic_cast<TestData*>(
      RequestContext::get()->getContextData("test"));
    auto data = a->data_;
    EXPECT_EQ(10, data);
  });

  // Clear the context
  RequestContext::setContext(nullptr);

  EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));

  // Fulfill the promise
  p.setValue();
}
开发者ID:GYGit,项目名称:folly,代码行数:31,代码来源:ContextTest.cpp

示例10: move

TEST(NonCopyableLambda, unique_ptr) {
  Promise<Unit> promise;
  auto int_ptr = std::make_unique<int>(1);

  EXPECT_EQ(*int_ptr, 1);

  auto future = promise.getFuture().thenValue(std::bind(
      [](std::unique_ptr<int>& p, folly::Unit) mutable {
        ++*p;
        return std::move(p);
      },
      std::move(int_ptr),
      std::placeholders::_1));

  // The previous statement can be simplified in C++14:
  //  auto future =
  //      promise.getFuture().thenValue([int_ptr = std::move(int_ptr)](
  //          auto&&) mutable {
  //        ++*int_ptr;
  //        return std::move(int_ptr);
  //      });

  EXPECT_FALSE(future.isReady());
  promise.setValue();
  EXPECT_TRUE(future.isReady());
  EXPECT_EQ(*std::move(future).get(), 2);
}
开发者ID:JacobMao,项目名称:folly,代码行数:27,代码来源:NonCopyableLambdaTest.cpp

示例11:

TEST(Timekeeper, futureWithinThrows) {
  Promise<int> p;
  auto f = p.getFuture()
    .within(one_ms)
    .onError([](TimedOut&) { return -1; });

  EXPECT_EQ(-1, f.get());
}
开发者ID:chjp2046,项目名称:folly,代码行数:8,代码来源:TimekeeperTest.cpp

示例12: collect

TEST(Collect, collectVariadic) {
  Promise<bool> pb;
  Promise<int> pi;
  Future<bool> fb = pb.getFuture();
  Future<int> fi = pi.getFuture();
  bool flag = false;
  collect(std::move(fb), std::move(fi))
    .then([&](std::tuple<bool, int> tup) {
      flag = true;
      EXPECT_EQ(std::get<0>(tup), true);
      EXPECT_EQ(std::get<1>(tup), 42);
    });
  pb.setValue(true);
  EXPECT_FALSE(flag);
  pi.setValue(42);
  EXPECT_TRUE(flag);
}
开发者ID:lamoreauxdy,项目名称:coral,代码行数:17,代码来源:CollectTest.cpp

示例13:

TEST(SemiFuture, DeferWithGetTimedGet) {
  std::atomic<int> innerResult{0};
  Promise<folly::Unit> p;
  auto f = p.getFuture();
  auto sf = std::move(f).semi().defer([&]() { innerResult = 17; });
  EXPECT_THROW(std::move(sf).get(std::chrono::milliseconds(100)), TimedOut);
  ASSERT_EQ(innerResult, 0);
}
开发者ID:simpkins,项目名称:folly,代码行数:8,代码来源:SemiFutureTest.cpp

示例14: catch

TEST(Promise, setException) {
    {
        Promise<Unit> p;
        auto f = p.getFuture();
        p.setException(eggs);
        EXPECT_THROW(f.value(), eggs_t);
    }
    {
        Promise<Unit> p;
        auto f = p.getFuture();
        try {
            throw eggs;
        } catch (...) {
            p.setException(exception_wrapper(std::current_exception()));
        }
        EXPECT_THROW(f.value(), eggs_t);
    }
}
开发者ID:BocaiFire,项目名称:folly,代码行数:18,代码来源:PromiseTest.cpp

示例15:

TEST(Timekeeper, futureWithinHandlesNullTimekeeperSingleton) {
  Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
  SCOPE_EXIT {
    Singleton<ThreadWheelTimekeeper>::make_mock();
  };
  Promise<int> p;
  auto f = p.getFuture().within(one_ms);
  EXPECT_THROW(f.get(), NoTimekeeper);
}
开发者ID:Orvid,项目名称:folly,代码行数:9,代码来源:TimekeeperTest.cpp


注:本文中的Promise::getFuture方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。