wpiutil: Add C++ libuv wrappers (#1166)

- Provide an EventLoopRunner to run uv::Loop on a separate thread.

- Add raw_ostream wrapper for uv::Buffer.
This commit is contained in:
Peter Johnson
2018-07-17 01:06:24 -07:00
committed by GitHub
parent 340b26bada
commit e2314f3528
55 changed files with 5647 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
/*----------------------------------------------------------------------------*/
/* 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/raw_uv_ostream.h" // NOLINT(build/include_order)
#include "gtest/gtest.h"
namespace wpi {
TEST(RawUvStreamTest, BasicWrite) {
SmallVector<uv::Buffer, 4> bufs;
raw_uv_ostream os(bufs, 1024);
os << "12";
os << "34";
ASSERT_EQ(bufs.size(), 1u);
ASSERT_EQ(bufs[0].len, 4u);
ASSERT_EQ(bufs[0].base[0], '1');
ASSERT_EQ(bufs[0].base[1], '2');
ASSERT_EQ(bufs[0].base[2], '3');
ASSERT_EQ(bufs[0].base[3], '4');
}
TEST(RawUvStreamTest, BoundaryWrite) {
SmallVector<uv::Buffer, 4> bufs;
raw_uv_ostream os(bufs, 4);
ASSERT_EQ(bufs.size(), 0u);
os << "12";
ASSERT_EQ(bufs.size(), 1u);
os << "34";
ASSERT_EQ(bufs.size(), 1u);
os << "56";
ASSERT_EQ(bufs.size(), 2u);
}
TEST(RawUvStreamTest, LargeWrite) {
SmallVector<uv::Buffer, 4> bufs;
raw_uv_ostream os(bufs, 4);
os << "123456";
ASSERT_EQ(bufs.size(), 2u);
ASSERT_EQ(bufs[1].len, 2u);
ASSERT_EQ(bufs[1].base[0], '5');
}
TEST(RawUvStreamTest, PrevDataWrite) {
SmallVector<uv::Buffer, 4> bufs;
bufs.emplace_back(uv::Buffer::Allocate(1024));
raw_uv_ostream os(bufs, 1024);
os << "1234";
ASSERT_EQ(bufs.size(), 2u);
ASSERT_EQ(bufs[0].len, 1024u);
ASSERT_EQ(bufs[1].len, 4u);
}
} // namespace wpi

View File

@@ -0,0 +1,105 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
/* 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 "wpi/uv/Async.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include <atomic>
#include <thread>
#include "wpi/mutex.h"
#include "wpi/uv/Loop.h"
#include "wpi/uv/Prepare.h"
namespace wpi {
namespace uv {
TEST(UvAsync, Test) {
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();
}
} // namespace uv
} // namespace wpi

View File

@@ -0,0 +1,108 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
/* 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 "wpi/uv/GetAddrInfo.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpi/uv/Loop.h"
#define CONCURRENT_COUNT 10
namespace wpi {
namespace uv {
TEST(UvGetAddrInfo, 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(); }, Twine::createNull());
loop->Run();
ASSERT_EQ(fail_cb_called, 1);
}
TEST(UvGetAddrInfo, 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(UvGetAddrInfo, 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);
}
TEST(UvGetAddrInfo, 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);
}
}
} // namespace uv
} // namespace wpi

View File

@@ -0,0 +1,77 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
/* 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 "wpi/uv/GetNameInfo.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpi/uv/Loop.h"
namespace wpi {
namespace uv {
TEST(UvGetNameInfo, 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(UvGetNameInfo, 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 uv
} // namespace wpi

View File

@@ -0,0 +1,70 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
/* 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 "wpi/uv/Loop.h" // NOLINT(build/include_order)
#include "gtest/gtest.h" // NOLINT(build/include_order)
#include "wpi/uv/Timer.h"
namespace wpi {
namespace uv {
TEST(UvLoop, 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 uv
} // namespace wpi

View File

@@ -0,0 +1,74 @@
/*----------------------------------------------------------------------------*/
/* 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/uv/Timer.h" // NOLINT(build/include_order)
#include "gtest/gtest.h"
namespace wpi {
namespace uv {
TEST(UvTimer, 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(UvTimer, 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 uv
} // namespace wpi