Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _Exit() on test failure, to avoid LeakSanitizer spam #1088

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions include/aws/testing/aws_test_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ the AWS_UNSTABLE_TESTING_API compiler flag

/** Prints a message to AWS_TESTING_REPORT_FD using printf format that appends the function, file and line number.
* If format is null, returns 0 without printing anything; otherwise returns 1.
* If function or file are null, the function, file and line number are not appended.
*/
static int s_cunit_failure_message0(
const char *prefix,
Expand All @@ -61,7 +62,11 @@ static int s_cunit_failure_message0(
vfprintf(AWS_TESTING_REPORT_FD, format, ap);
va_end(ap);

fprintf(AWS_TESTING_REPORT_FD, " [%s(): %s:%d]\n", function, file, line);
if (function && file) {
fprintf(AWS_TESTING_REPORT_FD, " [%s(): %s:%d]\n", function, file, line);
} else {
fprintf(AWS_TESTING_REPORT_FD, "\n");
}

return 1;
}
Expand All @@ -70,9 +75,6 @@ static int s_cunit_failure_message0(
#define CUNIT_FAILURE_MESSAGE(func, file, line, format, ...) \
s_cunit_failure_message0(FAIL_PREFIX, func, file, line, format, #__VA_ARGS__)

static int total_failures;
static int total_skip;

#define SUCCESS (0)
#define FAILURE (-1)
/* The exit code returned to ctest to indicate the test is skipped. Refer to cmake doc:
Expand All @@ -83,7 +85,6 @@ static int total_skip;

#define POSTSKIP_INTERNAL() \
do { \
total_skip++; \
return SKIP; \
} while (0)

Expand All @@ -106,9 +107,11 @@ static int total_skip;
#define PRINT_FAIL_INTERNAL0(...) \
s_cunit_failure_message0(FAIL_PREFIX, __func__, __FILE__, __LINE__, ##__VA_ARGS__, (const char *)NULL)

#define PRINT_FAIL_WITHOUT_LOCATION(...) \
s_cunit_failure_message0(FAIL_PREFIX, NULL, NULL, __LINE__, ##__VA_ARGS__, (const char *)NULL)

#define POSTFAIL_INTERNAL() \
do { \
total_failures++; \
return FAILURE; \
} while (0)

Expand Down Expand Up @@ -478,7 +481,8 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) {
const size_t leaked_bytes = aws_mem_tracer_bytes(allocator);
if (leaked_bytes) {
aws_mem_tracer_dump(allocator);
PRINT_FAIL_INTERNAL0("Test leaked memory: %zu bytes %zu allocations", leaked_bytes, leaked_allocations);
PRINT_FAIL_WITHOUT_LOCATION(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It always annoyed me how the final "test leaked" and "test failed" messages printed their file location and line no. It made it look like the aws_test_runner.h code is what had bugs

"Test leaked memory: %zu bytes %zu allocations", leaked_bytes, leaked_allocations);
goto fail;
}

Expand All @@ -495,7 +499,14 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) {
}

fail:
FAIL("%s [ \033[31mFAILED\033[0m ]", harness->test_name);
PRINT_FAIL_WITHOUT_LOCATION("%s [ \033[31mFAILED\033[0m ]", harness->test_name);
/* Use _Exit() to terminate without cleaning up resources.
* This prevents LeakSanitizer spam (yes, we know failing tests don't bother cleaning up).
* It also prevents errors where threads that haven't cleaned are still using the logger declared in this fn. */
fflush(AWS_TESTING_REPORT_FD);
fflush(stdout);
fflush(stderr);
_Exit(FAILURE);
}

/* Enables terminal escape sequences for text coloring on Windows. */
Expand Down
Loading