X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ffutures%2Ftest%2FCollectTest.cpp;h=dbe94dbb11816f975e1b63827208715ae5a0cbce;hb=7da4ef82aee382777bb50aadd4af14a482739d10;hp=4c75fa5d87cf7dcf019fdab7da33e6008380bec9;hpb=b05969e4b4474cadf1cb1f8ebc37567cc63c9b88;p=folly.git diff --git a/folly/futures/test/CollectTest.cpp b/folly/futures/test/CollectTest.cpp index 4c75fa5d..dbe94dbb 100644 --- a/folly/futures/test/CollectTest.cpp +++ b/folly/futures/test/CollectTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2015-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. @@ -14,12 +14,13 @@ * limitations under the License. */ -#include +#include #include -#include #include +#include +#include #include using namespace folly; @@ -35,8 +36,9 @@ TEST(Collect, collectAll) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collectAll(futures); @@ -58,8 +60,9 @@ TEST(Collect, collectAll) { std::vector> promises(4); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collectAll(futures); @@ -90,18 +93,20 @@ TEST(Collect, collectAll) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } - auto allf = collectAll(futures) - .then([](Try>>&& ts) { - for (auto& f : ts.value()) - f.value(); - }); + auto allf = collectAll(futures).then([](Try>>&& ts) { + for (auto& f : ts.value()) { + f.value(); + } + }); std::shuffle(promises.begin(), promises.end(), rng); - for (auto& p : promises) + for (auto& p : promises) { p.setValue(); + } EXPECT_TRUE(allf.isReady()); } } @@ -112,8 +117,9 @@ TEST(Collect, collect) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collect(futures); @@ -134,8 +140,9 @@ TEST(Collect, collect) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collect(futures); @@ -169,8 +176,9 @@ TEST(Collect, collect) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collect(futures); @@ -188,8 +196,9 @@ TEST(Collect, collect) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collect(futures); @@ -223,8 +232,9 @@ TEST(Collect, collect) { std::vector>> promises(10); std::vector>> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } collect(futures); } @@ -246,8 +256,9 @@ TEST(Collect, collectNotDefaultConstructible) { std::iota(indices.begin(), indices.end(), 0); std::shuffle(indices.begin(), indices.end(), rng); - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto allf = collect(futures); @@ -269,8 +280,9 @@ TEST(Collect, collectAny) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } for (auto& f : futures) { EXPECT_FALSE(f.isReady()); @@ -297,8 +309,9 @@ TEST(Collect, collectAny) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } for (auto& f : futures) { EXPECT_FALSE(f.isReady()); @@ -318,8 +331,9 @@ TEST(Collect, collectAny) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } auto anyf = collectAny(futures) .then([](std::pair> p) { @@ -331,12 +345,80 @@ TEST(Collect, collectAny) { } } +TEST(Collect, collectAnyWithoutException) { + { + std::vector> promises(10); + std::vector> futures; + + for (auto& p : promises) { + futures.push_back(p.getFuture()); + } + + auto onef = collectAnyWithoutException(futures); + + /* futures were moved in, so these are invalid now */ + EXPECT_FALSE(onef.isReady()); + + promises[7].setValue(42); + EXPECT_TRUE(onef.isReady()); + auto& idx_fut = onef.value(); + EXPECT_EQ(7, idx_fut.first); + EXPECT_EQ(42, idx_fut.second); + } + + // some exception before ready + { + std::vector> promises(10); + std::vector> futures; + + for (auto& p : promises) { + futures.push_back(p.getFuture()); + } + + auto onef = collectAnyWithoutException(futures); + + EXPECT_FALSE(onef.isReady()); + + promises[3].setException(eggs); + EXPECT_FALSE(onef.isReady()); + promises[4].setException(eggs); + EXPECT_FALSE(onef.isReady()); + promises[0].setValue(99); + EXPECT_TRUE(onef.isReady()); + auto& idx_fut = onef.value(); + EXPECT_EQ(0, idx_fut.first); + EXPECT_EQ(99, idx_fut.second); + } + + // all exceptions + { + std::vector> promises(10); + std::vector> futures; + + for (auto& p : promises) { + futures.push_back(p.getFuture()); + } + + auto onef = collectAnyWithoutException(futures); + + EXPECT_FALSE(onef.isReady()); + for (int i = 0; i < 9; ++i) { + promises[i].setException(eggs); + } + EXPECT_FALSE(onef.isReady()); + + promises[9].setException(eggs); + EXPECT_TRUE(onef.isReady()); + EXPECT_TRUE(onef.hasException()); + } +} TEST(Collect, alreadyCompleted) { { std::vector> fs; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 10; i++) { fs.push_back(makeFuture()); + } collectAll(fs) .then([&](std::vector> ts) { @@ -345,8 +427,9 @@ TEST(Collect, alreadyCompleted) { } { std::vector> fs; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 10; i++) { fs.push_back(makeFuture(i)); + } collectAny(fs) .then([&](std::pair> p) { @@ -487,8 +570,9 @@ TEST(Collect, collectN) { std::vector> promises(10); std::vector> futures; - for (auto& p : promises) + for (auto& p : promises) { futures.push_back(p.getFuture()); + } bool flag = false; size_t n = 3; @@ -496,8 +580,9 @@ TEST(Collect, collectN) { .then([&](std::vector>> v) { flag = true; EXPECT_EQ(n, v.size()); - for (auto& tt : v) + for (auto& tt : v) { EXPECT_TRUE(tt.second.hasValue()); + } }); promises[0].setValue(); @@ -518,16 +603,18 @@ TEST(Collect, smallVector) { { folly::small_vector> futures; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 10; i++) { futures.push_back(makeFuture()); + } auto anyf = collectAny(futures); } { folly::small_vector> futures; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 10; i++) { futures.push_back(makeFuture()); + } auto allf = collectAll(futures); } @@ -630,3 +717,14 @@ TEST(Collect, collectAllNone) { auto f = collectAll(fs); EXPECT_TRUE(f.isReady()); } + +TEST(Collect, noDefaultConstructor) { + struct A { + explicit A(size_t /* x */) {} + }; + + auto f1 = makeFuture(A(1)); + auto f2 = makeFuture(A(2)); + + auto f = collect(std::move(f1), std::move(f2)); +}