本文整理汇总了C++中Promise::setException方法的典型用法代码示例。如果您正苦于以下问题:C++ Promise::setException方法的具体用法?C++ Promise::setException怎么用?C++ Promise::setException使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Promise
的用法示例。
在下文中一共展示了Promise::setException方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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");
}
}
示例2: 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);
}
}
示例3: move
Future<int> future_throwing() {
Promise<int> p;
auto f = p.getFuture();
Xception x;
x.errorCode = 32;
x.message = "test";
p.setException(x);
return std::move(f);
}
示例4:
Future<Unit> future_voidThrowing() override {
Promise<Unit> p;
auto f = p.getFuture();
Xception x;
x.errorCode = 42;
x.message = "test2";
p.setException(x);
return f;
}
示例5: 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);
}
示例6:
TEST(SemiFuture, MakeSemiFutureFromFutureWithTry) {
Promise<int> p;
auto f = p.getFuture();
auto sf = std::move(f).semi().defer([&](Try<int> t) {
if (auto err = t.tryGetExceptionObject<std::logic_error>()) {
return Try<std::string>(err->what());
}
return Try<std::string>(
make_exception_wrapper<std::logic_error>("Exception"));
});
p.setException(make_exception_wrapper<std::logic_error>("Try"));
auto tryResult = std::move(sf).get();
ASSERT_EQ(tryResult.value(), "Try");
}
示例7: collectAll
TEST(Collect, collectAllVariadicWithException) {
Promise<bool> pb;
Promise<int> pi;
Future<bool> fb = pb.getFuture();
Future<int> fi = pi.getFuture();
bool flag = false;
collectAll(std::move(fb), std::move(fi))
.then([&](std::tuple<Try<bool>, Try<int>> tup) {
flag = true;
EXPECT_TRUE(std::get<0>(tup).hasValue());
EXPECT_EQ(std::get<0>(tup).value(), true);
EXPECT_TRUE(std::get<1>(tup).hasException());
EXPECT_THROW(std::get<1>(tup).value(), eggs_t);
});
pb.setValue(true);
EXPECT_FALSE(flag);
pi.setException(eggs);
EXPECT_TRUE(flag);
}
示例8: transportActive
TEST_F(ObservingHandlerTest, ConnectErrorHandlerDeletion) {
// Test when an error occurs while fetching the broadcast handler
// after the handler is deleted
InSequence dummy;
EXPECT_CALL(*prevHandler, transportActive(_))
.WillOnce(Invoke([&](MockBytesToBytesHandler::Context* ctx) {
ctx->fireTransportActive();
}));
// Verify that ingress is paused
EXPECT_CALL(*prevHandler, transportInactive(_)).WillOnce(Return());
Promise<BroadcastHandler<int, std::string>*> promise;
EXPECT_CALL(pool, mockGetHandler(_))
.WillOnce(Return(makeMoveWrapper(promise.getFuture())));
// Initialize the pipeline
pipeline->transportActive();
// Delete the handler and then inject an error
pipeline.reset();
promise.setException(std::exception());
}