[wpinet] Move network portions of wpiutil into new wpinet library (#4077)

This commit is contained in:
Peter Johnson
2022-05-07 10:54:14 -07:00
committed by GitHub
parent b33715db15
commit d673ead481
327 changed files with 1783 additions and 1179 deletions

View File

@@ -0,0 +1,264 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpinet/uv/AsyncFunction.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include <thread>
#include "wpinet/uv/Loop.h"
#include "wpinet/uv/Prepare.h"
namespace wpi::uv {
TEST(UvAsyncFunctionTest, Basic) {
int prepare_cb_called = 0;
int async_cb_called[2] = {0, 0};
int close_cb_called = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = AsyncFunction<int(int)>::Create(loop);
auto prepare = Prepare::Create(loop);
loop->error.connect([](Error) { FAIL(); });
prepare->error.connect([](Error) { FAIL(); });
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] {
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 = [&](promise<int> out, int v) {
++async_cb_called[v];
if (v == 1) {
async->Close();
prepare->Close();
}
out.set_value(v + 1);
};
loop->Run();
ASSERT_EQ(async_cb_called[0], 1);
ASSERT_EQ(async_cb_called[1], 1);
ASSERT_EQ(close_cb_called, 1);
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncFunctionTest, Ref) {
int prepare_cb_called = 0;
int val = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = AsyncFunction<int(int, int&)>::Create(loop);
auto prepare = Prepare::Create(loop);
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] { ASSERT_EQ(async->Call(1, val).get(), 2); });
});
prepare->Start();
async->wakeup = [&](promise<int> out, int v, int& r) {
r = v;
async->Close();
prepare->Close();
out.set_value(v + 1);
};
loop->Run();
ASSERT_EQ(val, 1);
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncFunctionTest, Movable) {
int prepare_cb_called = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async =
AsyncFunction<std::unique_ptr<int>(std::unique_ptr<int>)>::Create(loop);
auto prepare = Prepare::Create(loop);
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] {
auto val = std::make_unique<int>(1);
auto val2 = async->Call(std::move(val)).get();
ASSERT_NE(val2, nullptr);
ASSERT_EQ(*val2, 1);
});
});
prepare->Start();
async->wakeup = [&](promise<std::unique_ptr<int>> out,
std::unique_ptr<int> v) {
async->Close();
prepare->Close();
out.set_value(std::move(v));
};
loop->Run();
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncFunctionTest, CallIgnoreResult) {
int prepare_cb_called = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async =
AsyncFunction<std::unique_ptr<int>(std::unique_ptr<int>)>::Create(loop);
auto prepare = Prepare::Create(loop);
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] { async->Call(std::make_unique<int>(1)); });
});
prepare->Start();
async->wakeup = [&](promise<std::unique_ptr<int>> out,
std::unique_ptr<int> v) {
async->Close();
prepare->Close();
out.set_value(std::move(v));
};
loop->Run();
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncFunctionTest, VoidCall) {
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([&] { async->Call(); });
});
prepare->Start();
async->wakeup = [&](promise<void> out) {
async->Close();
prepare->Close();
out.set_value();
};
loop->Run();
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncFunctionTest, 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(UvAsyncFunctionTest, 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();
if (theThread.joinable()) {
theThread.join();
}
}
} // namespace wpi::uv

View File

@@ -0,0 +1,186 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "wpinet/uv/Async.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include <atomic>
#include <thread>
#include <wpi/mutex.h>
#include "wpinet/uv/Loop.h"
#include "wpinet/uv/Prepare.h"
namespace wpi::uv {
TEST(UvAsyncTest, CallbackOnly) {
std::atomic_int async_cb_called{0};
int prepare_cb_called = 0;
int close_cb_called = 0;
wpi::mutex mutex;
mutex.lock();
std::thread theThread;
auto loop = Loop::Create();
auto async = Async<>::Create(loop);
auto prepare = Prepare::Create(loop);
loop->error.connect([](Error) { FAIL(); });
prepare->error.connect([](Error) { FAIL(); });
prepare->closed.connect([&] { close_cb_called++; });
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] {
for (;;) {
mutex.lock();
int n = async_cb_called;
mutex.unlock();
if (n == 3) {
break;
}
async->Send();
std::this_thread::yield();
}
});
mutex.unlock();
});
prepare->Start();
async->error.connect([](Error) { FAIL(); });
async->closed.connect([&] { close_cb_called++; });
async->wakeup.connect([&] {
mutex.lock();
int n = ++async_cb_called;
mutex.unlock();
if (n == 3) {
async->Close();
prepare->Close();
}
});
loop->Run();
ASSERT_GT(prepare_cb_called, 0);
ASSERT_EQ(async_cb_called, 3);
ASSERT_EQ(close_cb_called, 2);
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncTest, Data) {
int prepare_cb_called = 0;
int async_cb_called[2] = {0, 0};
int close_cb_called = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = Async<int, std::function<void(int)>>::Create(loop);
auto prepare = Prepare::Create(loop);
loop->error.connect([](Error) { FAIL(); });
prepare->error.connect([](Error) { FAIL(); });
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] {
async->Send(0, [&](int v) {
ASSERT_EQ(v, 0);
++async_cb_called[0];
});
async->Send(1, [&](int v) {
ASSERT_EQ(v, 1);
++async_cb_called[1];
async->Close();
prepare->Close();
});
});
});
prepare->Start();
async->error.connect([](Error) { FAIL(); });
async->closed.connect([&] { close_cb_called++; });
async->wakeup.connect([&](int v, std::function<void(int)> f) { f(v); });
loop->Run();
ASSERT_EQ(async_cb_called[0], 1);
ASSERT_EQ(async_cb_called[1], 1);
ASSERT_EQ(close_cb_called, 1);
if (theThread.joinable()) {
theThread.join();
}
}
TEST(UvAsyncTest, DataRef) {
int prepare_cb_called = 0;
int val = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = Async<int, int&>::Create(loop);
auto prepare = Prepare::Create(loop);
prepare->prepare.connect([&] {
if (prepare_cb_called++) {
return;
}
theThread = std::thread([&] { async->Send(1, val); });
});
prepare->Start();
async->wakeup.connect([&](int v, int& r) {
r = v;
async->Close();
prepare->Close();
});
loop->Run();
ASSERT_EQ(val, 1);
if (theThread.joinable()) {
theThread.join();
}
}
} // namespace wpi::uv

View File

@@ -0,0 +1,48 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpinet/uv/Buffer.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
namespace wpi::uv {
TEST(UvSimpleBufferPoolTest, ConstructDefault) {
SimpleBufferPool<> pool;
auto buf1 = pool.Allocate();
ASSERT_EQ(buf1.len, 4096u); // NOLINT
pool.Release({&buf1, 1});
}
TEST(UvSimpleBufferPoolTest, ConstructSize) {
SimpleBufferPool<4> pool{8192};
auto buf1 = pool.Allocate();
ASSERT_EQ(buf1.len, 8192u); // NOLINT
pool.Release({&buf1, 1});
}
TEST(UvSimpleBufferPoolTest, ReleaseReuse) {
SimpleBufferPool<4> pool;
auto buf1 = pool.Allocate();
auto buf1copy = buf1;
auto origSize = buf1.len;
buf1.len = 8;
pool.Release({&buf1, 1});
ASSERT_EQ(buf1.base, nullptr);
auto buf2 = pool.Allocate();
ASSERT_EQ(buf1copy.base, buf2.base);
ASSERT_EQ(buf2.len, origSize);
pool.Release({&buf2, 1});
}
TEST(UvSimpleBufferPoolTest, ClearRemaining) {
SimpleBufferPool<4> pool;
auto buf1 = pool.Allocate();
pool.Release({&buf1, 1});
ASSERT_EQ(pool.Remaining(), 1u);
pool.Clear();
ASSERT_EQ(pool.Remaining(), 0u);
}
} // namespace wpi::uv

View File

@@ -0,0 +1,109 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "wpinet/uv/GetAddrInfo.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpinet/uv/Loop.h"
#define CONCURRENT_COUNT 10
namespace wpi::uv {
TEST(UvGetAddrInfoTest, BothNull) {
int fail_cb_called = 0;
auto loop = Loop::Create();
loop->error.connect([&](Error err) {
ASSERT_EQ(err.code(), UV_EINVAL);
fail_cb_called++;
});
GetAddrInfo(
loop, [](const addrinfo&) { FAIL(); }, "");
loop->Run();
ASSERT_EQ(fail_cb_called, 1);
}
TEST(UvGetAddrInfoTest, FailedLookup) {
int fail_cb_called = 0;
auto loop = Loop::Create();
loop->error.connect([&](Error err) {
ASSERT_EQ(fail_cb_called, 0);
ASSERT_LT(err.code(), 0);
fail_cb_called++;
});
// Use a FQDN by ending in a period
GetAddrInfo(
loop, [](const addrinfo&) { FAIL(); }, "xyzzy.xyzzy.xyzzy.");
loop->Run();
ASSERT_EQ(fail_cb_called, 1);
}
TEST(UvGetAddrInfoTest, Basic) {
int getaddrinfo_cbs = 0;
auto loop = Loop::Create();
loop->error.connect([](Error) { FAIL(); });
GetAddrInfo(
loop, [&](const addrinfo&) { getaddrinfo_cbs++; }, "localhost");
loop->Run();
ASSERT_EQ(getaddrinfo_cbs, 1);
}
#ifndef _WIN32
TEST(UvGetAddrInfoTest, Concurrent) {
int getaddrinfo_cbs = 0;
int callback_counts[CONCURRENT_COUNT];
auto loop = Loop::Create();
loop->error.connect([](Error) { FAIL(); });
for (int i = 0; i < CONCURRENT_COUNT; i++) {
callback_counts[i] = 0;
GetAddrInfo(
loop,
[i, &callback_counts, &getaddrinfo_cbs](const addrinfo&) {
callback_counts[i]++;
getaddrinfo_cbs++;
},
"localhost");
}
loop->Run();
for (int i = 0; i < CONCURRENT_COUNT; i++) {
ASSERT_EQ(callback_counts[i], 1);
}
}
#endif
} // namespace wpi::uv

View File

@@ -0,0 +1,74 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "wpinet/uv/GetNameInfo.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpinet/uv/Loop.h"
namespace wpi::uv {
TEST(UvGetNameInfoTest, BasicIp4) {
int getnameinfo_cbs = 0;
auto loop = Loop::Create();
loop->error.connect([](Error) { FAIL(); });
GetNameInfo4(
loop,
[&](const char* hostname, const char* service) {
ASSERT_NE(hostname, nullptr);
ASSERT_NE(service, nullptr);
getnameinfo_cbs++;
},
"127.0.0.1", 80);
loop->Run();
ASSERT_EQ(getnameinfo_cbs, 1);
}
TEST(UvGetNameInfoTest, BasicIp6) {
int getnameinfo_cbs = 0;
auto loop = Loop::Create();
loop->error.connect([](Error) { FAIL(); });
GetNameInfo6(
loop,
[&](const char* hostname, const char* service) {
ASSERT_NE(hostname, nullptr);
ASSERT_NE(service, nullptr);
getnameinfo_cbs++;
},
"::1", 80);
loop->Run();
ASSERT_EQ(getnameinfo_cbs, 1);
}
} // namespace wpi::uv

View File

@@ -0,0 +1,69 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "wpinet/uv/Loop.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpinet/uv/Timer.h"
namespace wpi::uv {
TEST(UvLoopTest, Walk) {
int seen_timer_handle = 0;
auto loop = Loop::Create();
auto timer = Timer::Create(loop);
loop->error.connect([](Error) { FAIL(); });
timer->error.connect([](Error) { FAIL(); });
timer->timeout.connect([&, theTimer = timer.get()] {
theTimer->GetLoopRef().Walk([&](Handle& it) {
if (&it == timer.get()) {
seen_timer_handle++;
}
});
theTimer->Close();
});
timer->Start(Timer::Time{1});
// Start event loop, expect to see the timer handle
ASSERT_EQ(seen_timer_handle, 0);
loop->Run();
ASSERT_EQ(seen_timer_handle, 1);
// Loop is finished, should not see our timer handle
seen_timer_handle = 0;
loop->Walk([&](Handle& it) {
if (&it == timer.get()) {
seen_timer_handle++;
}
});
ASSERT_EQ(seen_timer_handle, 0);
}
} // namespace wpi::uv

View File

@@ -0,0 +1,69 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpinet/uv/Timer.h" // NOLINT(build/include_order)
#include "gtest/gtest.h"
namespace wpi::uv {
TEST(UvTimerTest, StartAndStop) {
auto loop = Loop::Create();
auto handleNoRepeat = Timer::Create(loop);
auto handleRepeat = Timer::Create(loop);
bool checkTimerNoRepeatEvent = false;
bool checkTimerRepeatEvent = false;
handleNoRepeat->error.connect([](Error) { FAIL(); });
handleRepeat->error.connect([](Error) { FAIL(); });
handleNoRepeat->timeout.connect(
[&checkTimerNoRepeatEvent, handle = handleNoRepeat.get()] {
ASSERT_FALSE(checkTimerNoRepeatEvent);
checkTimerNoRepeatEvent = true;
handle->Stop();
handle->Close();
ASSERT_TRUE(handle->IsClosing());
});
handleRepeat->timeout.connect(
[&checkTimerRepeatEvent, handle = handleRepeat.get()] {
if (checkTimerRepeatEvent) {
handle->Stop();
handle->Close();
ASSERT_TRUE(handle->IsClosing());
} else {
checkTimerRepeatEvent = true;
ASSERT_FALSE(handle->IsClosing());
}
});
handleNoRepeat->Start(Timer::Time{0}, Timer::Time{0});
handleRepeat->Start(Timer::Time{0}, Timer::Time{1});
ASSERT_TRUE(handleNoRepeat->IsActive());
ASSERT_FALSE(handleNoRepeat->IsClosing());
ASSERT_TRUE(handleRepeat->IsActive());
ASSERT_FALSE(handleRepeat->IsClosing());
loop->Run();
ASSERT_TRUE(checkTimerNoRepeatEvent);
ASSERT_TRUE(checkTimerRepeatEvent);
}
TEST(UvTimerTest, Repeat) {
auto loop = Loop::Create();
auto handle = Timer::Create(loop);
handle->SetRepeat(Timer::Time{42});
ASSERT_EQ(handle->GetRepeat(), Timer::Time{42});
handle->Close();
loop->Run(); // forces close callback to run
}
} // namespace wpi::uv