mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
wpiutil: Change uv::AsyncFunction to use promise/future.
This allows the called function to pass along the promise to another asynchronous callback. To avoid memory allocations, add a home-rolled, simplified, non-allocating version of std::promise and std::future as wpi::promise and wpi::future.
This commit is contained in:
84
wpiutil/src/test/native/cpp/future_test.cpp
Normal file
84
wpiutil/src/test/native/cpp/future_test.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "wpi/future.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <thread>
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Future, Then) {
|
||||
promise<bool> inPromise;
|
||||
future<int> outFuture =
|
||||
inPromise.get_future().then([](bool v) { return v ? 5 : 6; });
|
||||
|
||||
inPromise.set_value(true);
|
||||
ASSERT_EQ(outFuture.get(), 5);
|
||||
}
|
||||
|
||||
TEST(Future, ThenSame) {
|
||||
promise<bool> inPromise;
|
||||
future<bool> outFuture =
|
||||
inPromise.get_future().then([](bool v) { return !v; });
|
||||
|
||||
inPromise.set_value(true);
|
||||
ASSERT_EQ(outFuture.get(), false);
|
||||
}
|
||||
|
||||
TEST(Future, ThenFromVoid) {
|
||||
promise<void> inPromise;
|
||||
future<int> outFuture = inPromise.get_future().then([] { return 5; });
|
||||
|
||||
inPromise.set_value();
|
||||
ASSERT_EQ(outFuture.get(), 5);
|
||||
}
|
||||
|
||||
TEST(Future, ThenToVoid) {
|
||||
promise<bool> inPromise;
|
||||
future<void> outFuture = inPromise.get_future().then([](bool v) {});
|
||||
|
||||
inPromise.set_value(true);
|
||||
ASSERT_TRUE(outFuture.is_ready());
|
||||
}
|
||||
|
||||
TEST(Future, ThenVoidVoid) {
|
||||
promise<void> inPromise;
|
||||
future<void> outFuture = inPromise.get_future().then([] {});
|
||||
|
||||
inPromise.set_value();
|
||||
ASSERT_TRUE(outFuture.is_ready());
|
||||
}
|
||||
|
||||
TEST(Future, Implicit) {
|
||||
promise<bool> inPromise;
|
||||
future<int> outFuture = inPromise.get_future();
|
||||
|
||||
inPromise.set_value(true);
|
||||
ASSERT_EQ(outFuture.get(), 1);
|
||||
}
|
||||
|
||||
TEST(Future, MoveSame) {
|
||||
promise<bool> inPromise;
|
||||
future<bool> outFuture1 = inPromise.get_future();
|
||||
future<bool> outFuture(std::move(outFuture1));
|
||||
|
||||
inPromise.set_value(true);
|
||||
ASSERT_EQ(outFuture.get(), true);
|
||||
}
|
||||
|
||||
TEST(Future, MoveVoid) {
|
||||
promise<void> inPromise;
|
||||
future<void> outFuture1 = inPromise.get_future();
|
||||
future<void> outFuture(std::move(outFuture1));
|
||||
|
||||
inPromise.set_value();
|
||||
ASSERT_TRUE(outFuture.is_ready());
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
@@ -34,21 +34,23 @@ TEST(UvAsyncFunction, Test) {
|
||||
prepare->prepare.connect([&] {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] {
|
||||
ASSERT_EQ(async->Call(0), 1);
|
||||
ASSERT_EQ(async->Call(1), 2);
|
||||
auto call0 = async->Call(0);
|
||||
auto call1 = async->Call(1);
|
||||
ASSERT_EQ(call0.get(), 1);
|
||||
ASSERT_EQ(call1.get(), 2);
|
||||
});
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->error.connect([](Error) { FAIL(); });
|
||||
async->closed.connect([&] { close_cb_called++; });
|
||||
async->wakeup = [&](int v) {
|
||||
async->wakeup = [&](promise<int> out, int v) {
|
||||
++async_cb_called[v];
|
||||
if (v == 1) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
}
|
||||
return v + 1;
|
||||
out.set_value(v + 1);
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
@@ -72,15 +74,15 @@ TEST(UvAsyncFunction, Ref) {
|
||||
|
||||
prepare->prepare.connect([&] {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] { ASSERT_EQ(async->Call(1, val), 2); });
|
||||
theThread = std::thread([&] { ASSERT_EQ(async->Call(1, val).get(), 2); });
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&](int v, int& r) {
|
||||
async->wakeup = [&](promise<int> out, int v, int& r) {
|
||||
r = v;
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
return v + 1;
|
||||
out.set_value(v + 1);
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
@@ -104,17 +106,18 @@ TEST(UvAsyncFunction, Movable) {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] {
|
||||
auto val = std::make_unique<int>(1);
|
||||
auto val2 = async->Call(std::move(val));
|
||||
auto val2 = async->Call(std::move(val)).get();
|
||||
ASSERT_NE(val2, nullptr);
|
||||
ASSERT_EQ(*val2, 1);
|
||||
});
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&](std::unique_ptr<int> v) {
|
||||
async->wakeup = [&](promise<std::unique_ptr<int>> out,
|
||||
std::unique_ptr<int> v) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
return v;
|
||||
out.set_value(std::move(v));
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
@@ -122,7 +125,7 @@ TEST(UvAsyncFunction, Movable) {
|
||||
if (theThread.joinable()) theThread.join();
|
||||
}
|
||||
|
||||
TEST(UvAsyncFunction, Send) {
|
||||
TEST(UvAsyncFunction, CallIgnoreResult) {
|
||||
int prepare_cb_called = 0;
|
||||
|
||||
std::thread theThread;
|
||||
@@ -134,14 +137,15 @@ TEST(UvAsyncFunction, Send) {
|
||||
|
||||
prepare->prepare.connect([&] {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] { async->Send(std::make_unique<int>(1)); });
|
||||
theThread = std::thread([&] { async->Call(std::make_unique<int>(1)); });
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&](std::unique_ptr<int> v) {
|
||||
async->wakeup = [&](promise<std::unique_ptr<int>> out,
|
||||
std::unique_ptr<int> v) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
return v;
|
||||
out.set_value(std::move(v));
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
@@ -164,9 +168,68 @@ TEST(UvAsyncFunction, VoidCall) {
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&]() {
|
||||
async->wakeup = [&](promise<void> out) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
out.set_value();
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
|
||||
if (theThread.joinable()) theThread.join();
|
||||
}
|
||||
|
||||
TEST(UvAsyncFunction, WaitFor) {
|
||||
int prepare_cb_called = 0;
|
||||
|
||||
std::thread theThread;
|
||||
|
||||
auto loop = Loop::Create();
|
||||
auto async = AsyncFunction<int()>::Create(loop);
|
||||
auto prepare = Prepare::Create(loop);
|
||||
|
||||
prepare->prepare.connect([&] {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] {
|
||||
ASSERT_FALSE(async->Call().wait_for(std::chrono::milliseconds(10)));
|
||||
});
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&](promise<int> out) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
out.set_value(1);
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
|
||||
if (theThread.joinable()) theThread.join();
|
||||
}
|
||||
|
||||
TEST(UvAsyncFunction, VoidWaitFor) {
|
||||
int prepare_cb_called = 0;
|
||||
|
||||
std::thread theThread;
|
||||
|
||||
auto loop = Loop::Create();
|
||||
auto async = AsyncFunction<void()>::Create(loop);
|
||||
auto prepare = Prepare::Create(loop);
|
||||
|
||||
prepare->prepare.connect([&] {
|
||||
if (prepare_cb_called++) return;
|
||||
theThread = std::thread([&] {
|
||||
ASSERT_FALSE(async->Call().wait_for(std::chrono::milliseconds(10)));
|
||||
});
|
||||
});
|
||||
prepare->Start();
|
||||
|
||||
async->wakeup = [&](promise<void> out) {
|
||||
async->Close();
|
||||
prepare->Close();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
out.set_value();
|
||||
};
|
||||
|
||||
loop->Run();
|
||||
|
||||
Reference in New Issue
Block a user