tests: rework error handling and stop overusing g_error()
PATCH 1/1 tests: rework error handling and stop overusing g_error()
From: Bartosz Golaszewski <[email protected]>
GLib recommends using g_error() only for programming errors and unrecoverable situations. It calls abort() and triggers a core dump. This means that if we encounter an error when running a test case, we'll exit immediately and leave a "leaked" GPIO simulator in configfs.
Rework the error handling: use g_error() only when a programming bug is encountered (e.g. invalid enum value) while everywhere else use the regular GError-based error handling. This way, in case of an error in libgpiosim, we'll simply fail the current test case and release all resources as usual.
In order not to pollute the test cases with error handling, let's hide the actual error checking behind macros for g_gpiosim_chip_new() and g_gpiosim_chip_get_value(). For g_gpiosim_chip_set_pull() let's just emit a log and do nothing as the test case in question will inevitably fail if the expected value is not correctly read back.
In order for the chip to get a failing constructor, we need to link the test-suite against the entire libgio, not only libgobject (for the GInitable interface).
--- configure.ac | 2 +- tests/Makefile.am | 4 +- tests/gpiod-test-sim.c | 329 +++++++++++++++++++++++++++++------------ tests/gpiod-test-sim.h | 42 +++++- tests/gpiod-test.c | 33 +++-- 5 files changed, 298 insertions(+), 112 deletions(-)
configure.ac +1 -1
@@ -153,7 +153,7 @@ then # For core library tests PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50]) - PKG_CHECK_MODULES([GOBJECT], [gobject-2.0 >= 2.50]) + PKG_CHECK_MODULES([GIO], [gio-2.0 >= 2.50]) AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_LIB([asprintf])]) AC_CHECK_FUNC([prctl], [], [FUNC_NOT_FOUND_LIB([prctl])])
tests/Makefile.am +2 -2
@@ -5,12 +5,12 @@ SUBDIRS = gpiosim AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/gpiosim/ AM_CFLAGS += -include $(top_builddir)/config.h -AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) +AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) $(GIO_CFLAGS) AM_CFLAGS += -DG_LOG_DOMAIN=\"gpiod-test\" AM_CFLAGS += $(PROFILING_CFLAGS) LDADD = $(top_builddir)/lib/libgpiod.la LDADD += $(top_builddir)/tests/gpiosim/libgpiosim.la -LDADD += $(GLIB_LIBS) $(GOBJECT_LIBS) +LDADD += $(GLIB_LIBS) $(GIO_LIBS) bin_PROGRAMS = gpiod-test
tests/gpiod-test-sim.c +238 -91
430 lines not on this page · raw message
tests/gpiod-test-sim.h +37 -5
64 lines not on this page · raw message
tests/gpiod-test.c +20 -13
@@ -18,7 +18,7 @@static GList *tests; -static void check_kernel (void ) +static gboolean check_kernel (void ) {guint major, minor, release;struct utsname un;
@@ -27,23 +27,29 @@ static void check_kernel(void)g_debug ("checking linux kernel version" ); ret =uname (&un); -if (ret) -g_error ("unable to read the kernel release version: %s" , -g_strerror (errno)); +if (ret) { +g_critical ("unable to read the kernel release version: %s" , +g_strerror (errno)); +return FALSE; + } ret =sscanf (un.release,"%u.%u.%u" , &major, &minor, &release); -if (ret !=3 ) -g_error ("error reading kernel release version" ); - -if (KERNEL_VERSION (major, minor, release) <MIN_KERNEL_VERSION ) -g_error ("linux kernel version must be at least v%u.%u.%u - got v%u.%u.%u" , -MIN_KERNEL_MAJOR ,MIN_KERNEL_MINOR ,MIN_KERNEL_RELEASE , - major, minor, release); +if (ret !=3 ) { +g_critical ("error reading kernel release version" ); +return FALSE; + } + +if (KERNEL_VERSION (major, minor, release) <MIN_KERNEL_VERSION ) { +g_critical ("linux kernel version must be at least v%u.%u.%u - got v%u.%u.%u" , +MIN_KERNEL_MAJOR ,MIN_KERNEL_MINOR ,MIN_KERNEL_RELEASE , + major, minor, release); +return FALSE; + }g_debug ("kernel release is v%u.%u.%u - ok to run tests" , major, minor, release); -return ; +return TRUE; }static void test_func_wrapper (gconstpointer data)
@@ -68,7 +74,8 @@ int main(gint argc, gchar **argv)g_debug ("running libgpiod test suite" );g_debug ("%u tests registered" ,g_list_length (tests)); -check_kernel (); +if (!check_kernel ()) +return 1 ;g_list_foreach (tests, add_test_from_list,NULL );g_list_free (tests);
Signature · 1 line
2.37.2