| 854 | // Reference passing |
| 855 | std::string s = "foo"; |
| 856 | ASSERT_OK_AND_ASSIGN(auto fut, |
| 857 | pool->Submit(inplace_add<std::string>, std::ref(s), "bar")); |
| 858 | ASSERT_OK_AND_EQ("foobar", fut.result()); |
| 859 | ASSERT_EQ(s, "foobar"); |
| 860 | } |
| 861 | { |
| 862 | // `void` return type |
| 863 | ASSERT_OK_AND_ASSIGN(auto fut, pool->Submit(SleepFor, 0.001)); |
| 864 | ASSERT_OK(fut.status()); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | TEST_F(TestThreadPool, SubmitWithStopToken) { |
| 869 | auto pool = this->MakeThreadPool(3); |
| 870 | { |
| 871 | StopSource stop_source; |
| 872 | ASSERT_OK_AND_ASSIGN(Future<int> fut, |
| 873 | pool->Submit(stop_source.token(), add<int>, 4, 5)); |
| 874 | Result<int> res = fut.result(); |
| 875 | ASSERT_OK_AND_EQ(9, res); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | TEST_F(TestThreadPool, SubmitWithStopTokenCancelled) { |
| 880 | auto pool = this->MakeThreadPool(3); |
| 881 | { |
| 882 | const int n_futures = 100; |
| 883 | StopSource stop_source; |
| 884 | StopToken stop_token = stop_source.token(); |
| 885 | std::vector<Future<int>> futures; |
| 886 | for (int i = 0; i < n_futures; ++i) { |
| 887 | ASSERT_OK_AND_ASSIGN( |
| 888 | auto fut, pool->Submit(stop_token, slow_add<int>, 0.01 /*seconds*/, i, 1)); |
| 889 | futures.push_back(std::move(fut)); |
| 890 | } |
| 891 | SleepFor(0.05); // Let some work finish |
| 892 | stop_source.RequestStop(); |
| 893 | int n_success = 0; |
| 894 | int n_cancelled = 0; |
| 895 | for (int i = 0; i < n_futures; ++i) { |
| 896 | Result<int> res = futures[i].result(); |
| 897 | if (res.ok()) { |
| 898 | ASSERT_EQ(i + 1, *res); |
| 899 | ++n_success; |
| 900 | } else { |
| 901 | ASSERT_RAISES(Cancelled, res); |
| 902 | ++n_cancelled; |
| 903 | } |
| 904 | } |
| 905 | ASSERT_GT(n_success, 0); |
| 906 | ASSERT_GT(n_cancelled, 0); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | // Test fork safety on Unix |
| 911 | |
| 912 | #if !(defined(_WIN32) || defined(ARROW_VALGRIND) || defined(ADDRESS_SANITIZER) || \ |
| 913 | defined(THREAD_SANITIZER)) |
no test coverage detected