Skip to content

Commit

Permalink
Merge pull request #147 from accelerated/invalid-future
Browse files Browse the repository at this point in the history
Fix validity check on generic futures when they are default constructed
  • Loading branch information
Alex Damian authored Mar 23, 2021
2 parents 21215c3 + 342ba0e commit b71dcbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion quantum/util/impl/quantum_generic_future_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ GenericFuture<T>::~GenericFuture()
template <typename T>
bool GenericFuture<T>::valid() const
{
return std::visit([](const auto& ctx)->bool { return ctx->valid(); }, _context);
return std::visit([](const auto& ctx)->bool { return ctx ? ctx->valid() : false; }, _context);
}

template <typename T>
Expand Down
8 changes: 8 additions & 0 deletions tests/quantum_generic_future_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ TEST(GenericFuture, WaitForIoFutureInCoroutine)
return ioFuture.get() + 10;
});

EXPECT_TRUE(threadFuture.valid());

EXPECT_EQ(43, threadFuture.get()); //block until value is available
}

Expand All @@ -98,4 +100,10 @@ TEST(GenericFuture, TestCopyable)

//read from the second future and we should throw
EXPECT_THROW(v.back().get(), FutureAlreadyRetrievedException);
}

TEST(GenericFuture, Invalid)
{
GenericFuture<int> future; //default constructed
EXPECT_FALSE(future.valid());
}

0 comments on commit b71dcbd

Please sign in to comment.