debuggerd_client_test: less racy test setup

Make sure that all the threads have started up, otherwise the main part
of the test might not be testing as stressful a situation as expected.
Note that the "race" moniker is still valid because of the debuggerd
timeout.

The test is now faster (405ms) when run under good conditions.

Test: atest 'debuggerd_test:debuggerd_client#race'
Test: Ran debuggerd_client.race 1000 times on its own.
Test: Ran the whole suite of debuggerd unit tests 1000 times.
Change-Id: I487e7654a71df9f1799f09c6f385c929ddf2f234
This commit is contained in:
Frederick Mayle 2022-12-15 16:08:52 -08:00 committed by Christopher Ferris
parent c51ed4c56e
commit 52205b8206

View file

@ -18,6 +18,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <chrono>
@ -51,23 +52,35 @@ static int getThreadCount() {
TEST(debuggerd_client, race) {
static int THREAD_COUNT = getThreadCount();
// Semaphore incremented once per thread started.
unique_fd barrier(eventfd(0, EFD_SEMAPHORE));
ASSERT_NE(-1, barrier.get());
pid_t forkpid = fork();
ASSERT_NE(-1, forkpid);
if (forkpid == 0) {
// Spawn a bunch of threads, to make crash_dump take longer.
std::vector<std::thread> threads;
threads.reserve(THREAD_COUNT);
for (int i = 0; i < THREAD_COUNT; ++i) {
threads.emplace_back([]() {
while (true) {
std::this_thread::sleep_for(60s);
threads.emplace_back([&barrier]() {
uint64_t count = 1;
ASSERT_NE(-1, write(barrier.get(), &count, sizeof(count)));
for (;;) {
pause();
}
});
}
for (;;) {
pause();
}
}
std::this_thread::sleep_for(60s);
exit(0);
// Wait for the child to spawn all of its threads.
for (int i = 0; i < THREAD_COUNT; ++i) {
uint64_t count;
ASSERT_NE(-1, read(barrier.get(), &count, sizeof(count)));
}
unique_fd pipe_read, pipe_write;
@ -77,9 +90,6 @@ TEST(debuggerd_client, race) {
constexpr int PIPE_SIZE = 16 * 1024 * 1024;
ASSERT_EQ(PIPE_SIZE, fcntl(pipe_read.get(), F_SETPIPE_SZ, PIPE_SIZE));
// Wait for a bit to let the child spawn all of its threads.
std::this_thread::sleep_for(1s);
ASSERT_TRUE(
debuggerd_trigger_dump(forkpid, kDebuggerdNativeBacktrace, 60000, std::move(pipe_write)));
// Immediately kill the forked child, to make sure that the dump didn't return early.