testutils: Factor out g_test_disable_crash_reporting()

We're already repeating this in 4 places, and in a subsequent commit
I'll extend it to do more.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2023-07-24 17:06:50 +01:00
parent e051f4abaf
commit be2c9220d6
5 changed files with 29 additions and 26 deletions

View File

@ -3359,6 +3359,7 @@ g_test_trap_assert_stdout_unmatched
g_test_trap_assert_stderr
g_test_trap_assert_stderr_unmatched
g_test_trap_fork
g_test_disable_crash_reporting
g_test_rand_bit
g_test_rand_int

View File

@ -1302,6 +1302,27 @@ g_test_log (GTestLogType lbit,
}
}
/**
* g_test_disable_crash_reporting:
*
* Attempt to disable system crash reporting infrastructure.
*
* This function should be called before exercising code paths that are
* expected or intended to crash, to avoid wasting resources in system-wide
* crash collection infrastructure such as systemd-coredump or abrt.
*
* Since: 2.78
*/
void
g_test_disable_crash_reporting (void)
{
#ifdef HAVE_SYS_RESOURCE_H
struct rlimit limit = { 0, 0 };
(void) setrlimit (RLIMIT_CORE, &limit);
#endif
}
/* We intentionally parse the command line without GOptionContext
* because otherwise you would never be able to test it.
*/
@ -1376,12 +1397,8 @@ parse_args (gint *argc_p,
* tests spawn a *lot* of them. Avoid spamming system crash
* collection programs such as systemd-coredump and abrt.
*/
#ifdef HAVE_SYS_RESOURCE_H
{
struct rlimit limit = { 0, 0 };
(void) setrlimit (RLIMIT_CORE, &limit);
}
#endif
g_test_disable_crash_reporting ();
argv[i] = NULL;
/* Force non-TAP output when spawning a subprocess, since people often
@ -3980,12 +3997,7 @@ g_test_trap_fork (guint64 usec_timeout,
* tests spawn a *lot* of them. Avoid spamming system crash
* collection programs such as systemd-coredump and abrt.
*/
#ifdef HAVE_SYS_RESOURCE_H
{
struct rlimit limit = { 0, 0 };
(void) setrlimit (RLIMIT_CORE, &limit);
}
#endif
g_test_disable_crash_reporting ();
return TRUE;
}

View File

@ -407,6 +407,8 @@ GLIB_AVAILABLE_IN_2_38
gboolean g_test_failed (void);
GLIB_AVAILABLE_IN_2_38
void g_test_set_nonfatal_assertions (void);
GLIB_AVAILABLE_IN_2_78
void g_test_disable_crash_reporting (void);
/**
* g_test_add:

View File

@ -19,20 +19,14 @@
*/
#include "config.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <glib.h>
int
main (int argc,
char **argv)
{
#ifdef HAVE_SYS_RESOURCE_H
/* We expect this test to abort, so try to avoid that creating a coredump */
struct rlimit limit = { 0, 0 };
(void) setrlimit (RLIMIT_CORE, &limit);
#endif
g_test_disable_crash_reporting ();
g_assert (42 < 0);
return 0;

View File

@ -23,9 +23,6 @@
#include "config.h"
#include <dlfcn.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <glib.h>
static gboolean malloc_eom = FALSE;
@ -59,11 +56,8 @@ int
main (int argc,
char *argv[])
{
#ifdef HAVE_SYS_RESOURCE_H
/* We expect this test to abort, so try to avoid that creating a coredump */
struct rlimit limit = { 0, 0 };
(void) setrlimit (RLIMIT_CORE, &limit);
#endif
g_test_disable_crash_reporting ();
g_setenv ("LC_ALL", "C", TRUE);