| 885 | // Reference passing |
| 886 | std::string s = "foo"; |
| 887 | ASSERT_OK_AND_ASSIGN(auto fut, |
| 888 | pool->Submit(inplace_add<std::string>, std::ref(s), "bar")); |
| 889 | ASSERT_OK_AND_EQ("foobar", fut.result()); |
| 890 | ASSERT_EQ(s, "foobar"); |
| 891 | } |
| 892 | { |
| 893 | // `void` return type |
| 894 | ASSERT_OK_AND_ASSIGN(auto fut, pool->Submit(SleepFor, 0.001)); |
| 895 | ASSERT_OK(fut.status()); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | TEST_F(TestThreadPool, SubmitWithStopToken) { |
| 900 | auto pool = this->MakeThreadPool(3); |
| 901 | { |
| 902 | StopSource stop_source; |
| 903 | ASSERT_OK_AND_ASSIGN(Future<int> fut, |
| 904 | pool->Submit(stop_source.token(), add<int>, 4, 5)); |
| 905 | Result<int> res = fut.result(); |
| 906 | ASSERT_OK_AND_EQ(9, res); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | TEST_F(TestThreadPool, SubmitWithStopTokenCancelled) { |
| 911 | auto pool = this->MakeThreadPool(3); |
| 912 | { |
| 913 | const int n_futures = 100; |
| 914 | StopSource stop_source; |
| 915 | StopToken stop_token = stop_source.token(); |
| 916 | std::vector<Future<int>> futures; |
| 917 | for (int i = 0; i < n_futures; ++i) { |
| 918 | ASSERT_OK_AND_ASSIGN( |
| 919 | auto fut, pool->Submit(stop_token, slow_add<int>, 0.01 /*seconds*/, i, 1)); |
| 920 | futures.push_back(std::move(fut)); |
| 921 | } |
| 922 | SleepFor(0.05); // Let some work finish |
| 923 | stop_source.RequestStop(); |
| 924 | int n_success = 0; |
| 925 | int n_cancelled = 0; |
| 926 | for (int i = 0; i < n_futures; ++i) { |
| 927 | Result<int> res = futures[i].result(); |
| 928 | if (res.ok()) { |
| 929 | ASSERT_EQ(i + 1, *res); |
| 930 | ++n_success; |
| 931 | } else { |
| 932 | ASSERT_RAISES(Cancelled, res); |
| 933 | ++n_cancelled; |
| 934 | } |
| 935 | } |
| 936 | ASSERT_GT(n_success, 0); |
| 937 | ASSERT_GT(n_cancelled, 0); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Test fork safety on Unix |
| 942 | |
| 943 | #if !(defined(_WIN32) || defined(ARROW_VALGRIND) || defined(ADDRESS_SANITIZER) || \ |
| 944 | defined(THREAD_SANITIZER)) |
no test coverage detected