diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 33 | ||||
| -rw-r--r-- | tests/run-xvfb.sh | 2 | ||||
| -rw-r--r-- | tests/test-css-provider-leak.c | 128 | ||||
| -rw-r--r-- | tests/test-indicator-ng.c | 4 |
4 files changed, 159 insertions, 8 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cf84c55..b2a3ab4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,7 +13,7 @@ function(add_test_library_by_name name) add_library("${TEST_LIBRARY_NAME}" SHARED ${TEST_LIBRARY_NAME}.c) target_include_directories("${TEST_LIBRARY_NAME}" PUBLIC ${PROJECT_DEPS_INCLUDE_DIRS}) target_include_directories("${TEST_LIBRARY_NAME}" PUBLIC "${CMAKE_SOURCE_DIR}/src") - target_link_libraries("${TEST_LIBRARY_NAME}" ${PROJECT_DEPS_LIBRARIES} -l${ayatana_indicator_gtkver}) + target_link_libraries("${TEST_LIBRARY_NAME}" ${PROJECT_DEPS_LINK_LIBRARIES} -l${ayatana_indicator_gtkver}) target_link_directories("${TEST_LIBRARY_NAME}" PUBLIC "${CMAKE_BINARY_DIR}/src") add_dependencies("${TEST_LIBRARY_NAME}" "${ayatana_indicator_gtkver}") endfunction() @@ -25,7 +25,7 @@ function(add_test_executable_by_name name) target_compile_definitions("${TEST_EXECUTABLE_NAME}" PUBLIC SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}") target_include_directories("${TEST_EXECUTABLE_NAME}" PUBLIC ${PROJECT_DEPS_INCLUDE_DIRS}) target_include_directories("${TEST_EXECUTABLE_NAME}" PUBLIC "${CMAKE_SOURCE_DIR}/src") - target_link_libraries("${TEST_EXECUTABLE_NAME}" ${PROJECT_DEPS_LIBRARIES} -l${ayatana_indicator_gtkver}) + target_link_libraries("${TEST_EXECUTABLE_NAME}" ${PROJECT_DEPS_LINK_LIBRARIES} -l${ayatana_indicator_gtkver}) target_link_directories("${TEST_EXECUTABLE_NAME}" PUBLIC "${CMAKE_BINARY_DIR}/src") add_dependencies("${TEST_EXECUTABLE_NAME}" "${ayatana_indicator_gtkver}") endfunction() @@ -185,7 +185,7 @@ add_custom_command( WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} VERBATIM COMMAND - echo "#!/bin/bash" > "${CMAKE_CURRENT_BINARY_DIR}/test-desktop-shortcuts-tester" + echo "#!/bin/sh" > "${CMAKE_CURRENT_BINARY_DIR}/test-desktop-shortcuts-tester" COMMAND echo ". ${CMAKE_CURRENT_SOURCE_DIR}/run-xvfb.sh" >> "${CMAKE_CURRENT_BINARY_DIR}/test-desktop-shortcuts-tester" COMMAND @@ -195,6 +195,26 @@ add_custom_command( ) add_test("test-desktop-shortcuts-tester" "test-desktop-shortcuts-tester") +# test-css-provider-leak +add_test_executable_by_name(test-css-provider-leak) + +# test-css-provider-leak-tester +add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak-tester" + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + VERBATIM + COMMAND + echo "#!/bin/sh" > "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak-tester" + COMMAND + echo ". ${CMAKE_CURRENT_SOURCE_DIR}/run-xvfb.sh" >> "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak-tester" + COMMAND + echo "gtester -k --verbose -o=${CMAKE_CURRENT_BINARY_DIR}/loader-check-results.xml ${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak" >> "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak-tester" + COMMAND + chmod +x "${CMAKE_CURRENT_BINARY_DIR}/test-css-provider-leak-tester" +) +add_test("test-css-provider-leak-tester" "test-css-provider-leak-tester") + if (FLAVOUR_GTK3 AND ENABLE_IDO) # test-indicator-ng add_test_executable_by_name(test-indicator-ng) @@ -223,7 +243,7 @@ if (FLAVOUR_GTK3 AND ENABLE_IDO) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} VERBATIM COMMAND - echo "#!/bin/bash" > "${CMAKE_CURRENT_BINARY_DIR}/test-indicator-ng-tester" + echo "#!/bin/sh" > "${CMAKE_CURRENT_BINARY_DIR}/test-indicator-ng-tester" COMMAND echo ". ${CMAKE_CURRENT_SOURCE_DIR}/run-xvfb.sh" >> "${CMAKE_CURRENT_BINARY_DIR}/test-indicator-ng-tester" COMMAND @@ -244,7 +264,7 @@ add_custom_command( WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} VERBATIM COMMAND - echo "#!/bin/bash" > "${CMAKE_CURRENT_BINARY_DIR}/loader-tester" + echo "#!/bin/sh" > "${CMAKE_CURRENT_BINARY_DIR}/loader-tester" COMMAND echo ". ${CMAKE_CURRENT_SOURCE_DIR}/run-xvfb.sh" >> "${CMAKE_CURRENT_BINARY_DIR}/loader-tester" COMMAND @@ -257,7 +277,7 @@ add_test("loader-tester" "loader-tester") # indicator-test-service add_executable("indicator-test-service" indicator-test-service.c) target_include_directories("indicator-test-service" PUBLIC ${PROJECT_DEPS_INCLUDE_DIRS}) -target_link_libraries("indicator-test-service" ${PROJECT_DEPS_LIBRARIES}) +target_link_libraries("indicator-test-service" ${PROJECT_DEPS_LINK_LIBRARIES}) set (ALL_TESTERS "service-manager-connect-tester" @@ -267,6 +287,7 @@ set (ALL_TESTERS "service-version-tester" "service-version-multiwatch-tester" "test-desktop-shortcuts-tester" + "test-css-provider-leak-tester" "loader-tester" ) diff --git a/tests/run-xvfb.sh b/tests/run-xvfb.sh index 63b6f0d..16c5f65 100644 --- a/tests/run-xvfb.sh +++ b/tests/run-xvfb.sh @@ -1,4 +1,4 @@ -if [ "$DISPLAY" == "" ]; then +if [ "$DISPLAY" = "" ]; then Xvfb -ac -noreset -screen 0 800x600x16 -help 2>/dev/null 1>&2 XID=`for id in 101 102 103 104 105 106 107 197 199 211 223 227 293 307 308 309 310 311 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 4703 4721 4723 4729 4733 4751 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 ; do test -e /tmp/.X$id-lock || { echo $id; exit 0; }; done; exit 1` { Xvfb -ac -noreset -screen 0 800x600x16 :$XID -screen 0 800x600x16 -nolisten tcp -auth /dev/null >/dev/null 2>&1 & trap "kill -15 $! " 0 HUP INT QUIT TRAP USR1 PIPE TERM ; } || { echo "Gtk+Tests:ERROR: Failed to start Xvfb environment for X11 target tests."; exit 1; } diff --git a/tests/test-css-provider-leak.c b/tests/test-css-provider-leak.c new file mode 100644 index 0000000..aeb7d49 --- /dev/null +++ b/tests/test-css-provider-leak.c @@ -0,0 +1,128 @@ +/* + * Test that demonstrates the GtkCssProvider memory leak in + * indicator_ng_set_label (before fix) and validates the fix. + * + * Before the fix, every call to indicator_ng_set_label() created a new + * GtkCssProvider and added it to the label's GtkStyleContext without + * removing the old one. Since the datetime indicator updates the label + * once per second, this leaked ~2 KB/sec (~170 MB/day). + * + * This test calls set_label in a loop and measures heap growth via + * /proc/self/statm to show the leak is real and the fix works. + */ + +#include <gtk/gtk.h> +#include <stdio.h> + +static long +get_rss_kb (void) +{ + long rss = 0; + FILE *f = fopen ("/proc/self/statm", "r"); + if (f) { + long size, resident; + if (fscanf (f, "%ld %ld", &size, &resident) == 2) + rss = resident * (sysconf (_SC_PAGESIZE) / 1024); + fclose (f); + } + return rss; +} + +/* + * Simulate what indicator_ng_set_label does using the UNFIXED code: + * creates and adds a new GtkCssProvider every call. + */ +static void +set_label_leaky (GtkLabel *label, guint nPadding) +{ + GtkCssProvider *pCssProvider = gtk_css_provider_new (); + GtkStyleContext *pStyleContext = gtk_widget_get_style_context (GTK_WIDGET (label)); + gtk_style_context_add_provider (pStyleContext, GTK_STYLE_PROVIDER (pCssProvider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + gchar *sCss = g_strdup_printf ("label{padding-left: %ipx;}", nPadding); + gtk_css_provider_load_from_data (pCssProvider, sCss, -1, NULL); + g_free (sCss); + g_object_unref (pCssProvider); +} + +/* + * Simulate what indicator_ng_set_label does AFTER the fix: + * reuse a single provider, only reload its CSS data. + */ +static void +set_label_fixed (GtkLabel *label, guint nPadding, GtkCssProvider **cached) +{ + if (*cached == NULL) { + *cached = gtk_css_provider_new (); + GtkStyleContext *pStyleContext = gtk_widget_get_style_context (GTK_WIDGET (label)); + gtk_style_context_add_provider (pStyleContext, GTK_STYLE_PROVIDER (*cached), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + } + gchar *sCss = g_strdup_printf ("label{padding-left: %ipx;}", nPadding); + gtk_css_provider_load_from_data (*cached, sCss, -1, NULL); + g_free (sCss); +} + +#define ITERATIONS 100000 + +static void +test_leaky_set_label (void) +{ + GtkWidget *label = gtk_label_new ("12:00:00"); + g_object_ref_sink (label); + + long rss_before = get_rss_kb (); + + for (int i = 0; i < ITERATIONS; i++) { + set_label_leaky (GTK_LABEL (label), 6); + } + + long rss_after = get_rss_kb (); + long growth_kb = rss_after - rss_before; + + g_test_message ("leaky: RSS before=%ld KB, after=%ld KB, growth=%ld KB over %d iterations", + rss_before, rss_after, growth_kb, ITERATIONS); + + /* 100k leaked providers should use at least 50 MB. + If we see significant growth, the leak is confirmed. */ + g_assert_cmpint (growth_kb, >, 50000); + + g_object_unref (label); +} + +static void +test_fixed_set_label (void) +{ + GtkWidget *label = gtk_label_new ("12:00:00"); + g_object_ref_sink (label); + + long rss_before = get_rss_kb (); + + GtkCssProvider *cached = NULL; + for (int i = 0; i < ITERATIONS; i++) { + set_label_fixed (GTK_LABEL (label), 6, &cached); + } + + long rss_after = get_rss_kb (); + long growth_kb = rss_after - rss_before; + + g_test_message ("fixed: RSS before=%ld KB, after=%ld KB, growth=%ld KB over %d iterations", + rss_before, rss_after, growth_kb, ITERATIONS); + + /* With the fix, memory growth should be negligible (< 5 MB). */ + g_assert_cmpint (growth_kb, <, 5000); + + g_object_unref (cached); + g_object_unref (label); +} + +int +main (int argc, char **argv) +{ + gtk_test_init (&argc, &argv); + + g_test_add_func ("/indicator-ng/css-provider-leak/leaky", test_leaky_set_label); + g_test_add_func ("/indicator-ng/css-provider-leak/fixed", test_fixed_set_label); + + return g_test_run (); +} diff --git a/tests/test-indicator-ng.c b/tests/test-indicator-ng.c index 774b000..2b93236 100644 --- a/tests/test-indicator-ng.c +++ b/tests/test-indicator-ng.c @@ -174,11 +174,13 @@ test_menu (void) int main (int argc, char **argv) { - /* gvfs, dconf, and appmenu-gtk leak GDbusConnections, which confuses + /* gvfs, dconf, appmenu-gtk, atk-bridge and gdk-wayland leak GDbusConnections, which confuses * g_test_dbus_down. Make sure we're not using any of those. */ g_setenv ("GIO_USE_VFS", "local", TRUE); g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); + g_setenv ("NO_AT_BRIDGE", "1", TRUE); + g_setenv ("GDK_BACKEND", "x11", TRUE); g_unsetenv ("UBUNTU_MENUPROXY"); g_test_init (&argc, &argv, NULL); |
