Skip to content

Commit

Permalink
Add a unit test checking the ref counts (fails in master)
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Nov 4, 2024
1 parent dc457c4 commit 8ea3723
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pdns/recursordist/test-mtasker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,84 @@ BOOST_AUTO_TEST_CASE(test_Simple)
BOOST_CHECK_EQUAL(events.size(), 0U);
}

struct MTKey
{
int key;
shared_ptr<int> ptr;

bool operator<(const MTKey& /* b */) const
{
// We don't want explicit PacketID compare here, but always via predicate class below
assert(0); // NOLINT: lib
}
};

struct MTKeyCompare
{
bool operator()(const std::shared_ptr<MTKey>& lhs, const std::shared_ptr<MTKey>& rhs) const
{
return lhs->key < rhs->key;
}
};

using KeyMT_t = MTasker<std::shared_ptr<MTKey>, int, MTKeyCompare>;

// Test the case of #14807
static void doSomethingKey(void* ptr)
{
auto* multiTasker = reinterpret_cast<KeyMT_t*>(ptr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
auto key = std::make_shared<MTKey>();
key->key = 12;
key->ptr = std::make_shared<int>(13);
BOOST_CHECK_EQUAL(key.use_count(), 1U);
BOOST_CHECK_EQUAL(key->ptr.use_count(), 1U);
int value = 0;
if (multiTasker->waitEvent(key, &value) == 1) {
g_result = value;
}
BOOST_CHECK_EQUAL(key.use_count(), 1U);
BOOST_CHECK_EQUAL(key->key, 12);
BOOST_REQUIRE(key->ptr != nullptr);
BOOST_CHECK_EQUAL(key->ptr.use_count(), 1U);
BOOST_CHECK_EQUAL(*key->ptr, 13);
}

BOOST_AUTO_TEST_CASE(test_SharedPointer)
{
g_result = 0;
KeyMT_t multiTasker;
multiTasker.makeThread(doSomethingKey, &multiTasker);
timeval now{};
gettimeofday(&now, nullptr);
bool first = true;
int value = 24;
auto key = std::make_shared<MTKey>();
key->key = 12;
key->ptr = std::make_shared<int>(1);
BOOST_CHECK_EQUAL(key->ptr.use_count(), 1U);
for (;;) {
while (multiTasker.schedule(now)) {
}

if (first) {
multiTasker.sendEvent(key, &value);
BOOST_CHECK_EQUAL(key.use_count(), 1U);
BOOST_CHECK_EQUAL(key->ptr.use_count(), 1U);
first = false;
}
if (multiTasker.noProcesses()) {
break;
}
}
BOOST_CHECK_EQUAL(g_result, value);
vector<std::shared_ptr<MTKey>> events;
multiTasker.getEvents(events);
BOOST_CHECK_EQUAL(events.size(), 0U);
BOOST_CHECK_EQUAL(key.use_count(), 1U);
BOOST_CHECK_EQUAL(key->ptr.use_count(), 1U);
BOOST_CHECK_EQUAL(*key->ptr, 1);
}

static const size_t stackSize = 8 * 1024;
static const size_t headroom = 1536; // Decrease to hit stackoverflow

Expand Down

0 comments on commit 8ea3723

Please sign in to comment.