-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow a weak_ptr to be passed to each callback along with a normal pointer. This will make it easier to not accidentally hold on to resources when juggling multiple threads. Many tests were duplicated into test/*-wp.cc just to make sure the weak_ptr API has test coverage. PR-URL: #31 Co-authored-by: Santiago Gimeno <[email protected]>
- Loading branch information
1 parent
8d56c39
commit d4f4c64
Showing
15 changed files
with
4,650 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "../include/nsuv-inl.h" | ||
#include "./catch.hpp" | ||
#include "./helpers.h" | ||
|
||
using nsuv::ns_addrinfo; | ||
|
||
static const char* invalid_name = "xyzzy.xyzzy.xyzzy."; | ||
static const char* valid_name = "localhost"; | ||
|
||
static std::string* my_data_ptr = nullptr; | ||
|
||
static void gettaddrinfo_failure_cb(ns_addrinfo* info, | ||
int status, | ||
std::weak_ptr<size_t> d) { | ||
auto sp = d.lock(); | ||
ASSERT(sp); | ||
ASSERT_EQ(42, *sp); | ||
ASSERT_GT(0, status); | ||
ASSERT_NULL(info->info()); | ||
} | ||
|
||
TEST_CASE("invalid_get_async_wp", "[addrinfo]") { | ||
std::shared_ptr<size_t> sp = std::make_shared<size_t>(42); | ||
ns_addrinfo info; | ||
|
||
ASSERT_EQ(0, info.get(uv_default_loop(), | ||
gettaddrinfo_failure_cb, | ||
invalid_name, | ||
nullptr, | ||
nullptr, | ||
TO_WEAK(sp))); | ||
|
||
ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_DEFAULT)); | ||
|
||
make_valgrind_happy(); | ||
} | ||
|
||
static void gettaddrinfo_success_cb(ns_addrinfo* info, | ||
int status, | ||
std::weak_ptr<size_t> d) { | ||
auto sp = d.lock(); | ||
ASSERT(sp); | ||
ASSERT_EQ(42, *sp); | ||
ASSERT_EQ(0, status); | ||
ASSERT(info->info()); | ||
} | ||
|
||
TEST_CASE("valid_get_async_wp", "[addrinfo]") { | ||
std::shared_ptr<size_t> sp = std::make_shared<size_t>(42); | ||
ns_addrinfo info; | ||
|
||
ASSERT_EQ(0, info.get(uv_default_loop(), | ||
gettaddrinfo_success_cb, | ||
valid_name, | ||
nullptr, | ||
nullptr, | ||
TO_WEAK(sp))); | ||
|
||
ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_DEFAULT)); | ||
|
||
make_valgrind_happy(); | ||
} | ||
|
||
static void gettaddrinfo_void_data_cb(ns_addrinfo* info, | ||
int status, | ||
std::weak_ptr<std::string> d) { | ||
auto data = d.lock(); | ||
ASSERT_EQ(0, status); | ||
ASSERT(info->info()); | ||
ASSERT(data); | ||
ASSERT_EQ(data.get(), my_data_ptr); | ||
} | ||
|
||
TEST_CASE("valid_get_async_void_data_wp", "[addrinfo]") { | ||
auto my_data = std::make_shared<std::string>("my_data"); | ||
std::weak_ptr<std::string> wp = my_data; | ||
ns_addrinfo info; | ||
struct addrinfo hints; | ||
|
||
my_data_ptr = my_data.get(); | ||
|
||
memset(&hints, 0, sizeof(struct addrinfo)); | ||
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | ||
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ | ||
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ | ||
hints.ai_protocol = 0; /* Any protocol */ | ||
hints.ai_canonname = nullptr; | ||
hints.ai_addr = nullptr; | ||
hints.ai_next = nullptr; | ||
|
||
ASSERT_EQ(0, info.get(uv_default_loop(), | ||
gettaddrinfo_void_data_cb, | ||
valid_name, | ||
nullptr, | ||
&hints, | ||
wp)); | ||
|
||
ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_DEFAULT)); | ||
|
||
my_data_ptr = nullptr; | ||
make_valgrind_happy(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "../include/nsuv-inl.h" | ||
#include "./catch.hpp" | ||
#include "./helpers.h" | ||
|
||
#include <atomic> | ||
|
||
using nsuv::ns_thread; | ||
using nsuv::ns_async; | ||
using nsuv::ns_mutex; | ||
using nsuv::ns_prepare; | ||
|
||
struct resources { | ||
ns_thread* thread; | ||
ns_async* async; | ||
ns_mutex* mutex; | ||
ns_prepare* prepare; | ||
}; | ||
|
||
static std::atomic<int> async_cb_called; | ||
static int prepare_cb_called; | ||
static int close_cb_called; | ||
|
||
|
||
static void thread_cb(ns_thread*, std::weak_ptr<resources> wp) { | ||
auto res = wp.lock(); | ||
ASSERT(res); | ||
for (;;) { | ||
res->mutex->lock(); | ||
res->mutex->unlock(); | ||
if (async_cb_called == 3) { | ||
break; | ||
} | ||
CHECK(0 == res->async->send()); | ||
uv_sleep(0); | ||
} | ||
} | ||
|
||
|
||
template <class H_T> | ||
static void close_cb(H_T* handle) { | ||
CHECK(handle != nullptr); | ||
close_cb_called++; | ||
} | ||
|
||
|
||
static void async_cb(ns_async* handle, std::weak_ptr<resources> wp) { | ||
auto res = wp.lock(); | ||
ASSERT(res); | ||
CHECK(handle == res->async); | ||
res->mutex->lock(); | ||
res->mutex->unlock(); | ||
if (++async_cb_called == 3) { | ||
res->async->close(close_cb); | ||
res->prepare->close(close_cb); | ||
} | ||
} | ||
|
||
|
||
static void prepare_cb(ns_prepare* handle, std::weak_ptr<resources> wp) { | ||
auto res = wp.lock(); | ||
ASSERT(res); | ||
if (prepare_cb_called++) | ||
return; | ||
CHECK(handle == res->prepare); | ||
CHECK(0 == res->thread->create(thread_cb, wp)); | ||
res->mutex->unlock(); | ||
} | ||
|
||
|
||
TEST_CASE("async_operations_wp", "[async]") { | ||
ns_thread thread; | ||
ns_async async; | ||
ns_mutex mutex; | ||
ns_prepare prepare; | ||
std::shared_ptr<resources> sp( | ||
new resources{ &thread, &async, &mutex, &prepare }); | ||
std::weak_ptr<resources> res = sp; | ||
|
||
ASSERT_EQ(0, prepare.init(uv_default_loop())); | ||
ASSERT_EQ(0, prepare.start(prepare_cb, res)); | ||
ASSERT_EQ(0, async.init(uv_default_loop(), async_cb, res)); | ||
ASSERT_EQ(0, mutex.init()); | ||
|
||
mutex.lock(); | ||
|
||
ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_DEFAULT)); | ||
ASSERT_LT(0, prepare_cb_called); | ||
ASSERT_EQ(3, async_cb_called); | ||
ASSERT_EQ(2, close_cb_called); | ||
ASSERT_EQ(0, thread.join()); | ||
|
||
make_valgrind_happy(); | ||
} |
Oops, something went wrong.