Skip to content

Commit

Permalink
Add tests for moving and swapping promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-pando committed Dec 3, 2024
1 parent 984b50b commit af224b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ cc_test(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
Expand Down
31 changes: 31 additions & 0 deletions common/promise_test.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "common/promise.h"

#include <string>
#include <utility>

#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/notification.h"
#include "absl/time/time.h"
#include "common/mock_clock.h"
#include "common/scheduler.h"
Expand Down Expand Up @@ -1411,4 +1413,33 @@ TEST_F(PromiseTest, RejectVoid) {
EXPECT_TRUE(done);
}

TEST_F(PromiseTest, MoveConstruct) {
auto p1 = Promise<int>::Resolve(42);
Promise<int> p2{std::move(p1)};
std::move(p2).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(42)); });
}

TEST_F(PromiseTest, MoveAssign) {
auto p1 = Promise<int>::Resolve(42);
Promise<int> p2;
p2 = std::move(p1);
std::move(p2).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(42)); });
}

TEST_F(PromiseTest, Swap) {
auto p1 = Promise<int>::Resolve(42);
auto p2 = Promise<int>::Resolve(43);
p1.swap(p2);
std::move(p1).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(43)); });
std::move(p2).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(42)); });
}

TEST_F(PromiseTest, AdlSwap) {
auto p1 = Promise<int>::Resolve(42);
auto p2 = Promise<int>::Resolve(43);
swap(p1, p2);
std::move(p1).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(43)); });
std::move(p2).Then([](absl::StatusOr<int> const value) { EXPECT_THAT(value, IsOkAndHolds(42)); });
}

} // namespace

0 comments on commit af224b5

Please sign in to comment.