Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright 2025 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include <glib/glib.h>
22 : : #include <glib/gstdio.h>
23 : : #include <sys/stat.h>
24 : : #include <sys/types.h>
25 : :
26 : : #include "gio.h"
27 : : #include "gmemorymonitorpsi.h"
28 : :
29 : : typedef struct
30 : : {
31 : : char *mock_psi_path;
32 : : char *mock_proc_path;
33 : : } SetupData;
34 : :
35 : : static void
36 : 1 : tests_setup (SetupData *setup_data,
37 : : gconstpointer data)
38 : : {
39 : 1 : char *proc_contents = NULL;
40 : 1 : GError *local_error = NULL;
41 : :
42 : 1 : setup_data->mock_psi_path = g_build_filename (g_get_tmp_dir (), "cgroup", NULL);
43 : 1 : g_assert_no_errno (mkfifo (setup_data->mock_psi_path, S_IRUSR | S_IWUSR));
44 : :
45 : 1 : setup_data->mock_proc_path = g_build_filename (g_get_tmp_dir (), "psi-proc", NULL);
46 : 1 : proc_contents = g_strdup_printf ("0::%s", setup_data->mock_psi_path);
47 : 1 : g_file_set_contents_full (setup_data->mock_proc_path, proc_contents,
48 : : -1, G_FILE_SET_CONTENTS_NONE,
49 : : S_IRUSR | S_IWUSR, &local_error);
50 : 1 : g_assert_no_error (local_error);
51 : 1 : g_free (proc_contents);
52 : 1 : }
53 : :
54 : : static void
55 : 1 : tests_teardown (SetupData *setup_data,
56 : : gconstpointer data)
57 : : {
58 : 1 : g_unlink (setup_data->mock_proc_path);
59 : 1 : g_free (setup_data->mock_proc_path);
60 : 1 : g_unlink (setup_data->mock_psi_path);
61 : 1 : g_free (setup_data->mock_psi_path);
62 : 1 : }
63 : :
64 : : static void
65 : 3 : memory_monitor_psi_signal_cb (GMemoryMonitor *m,
66 : : GMemoryMonitorWarningLevel level,
67 : : gpointer user_data)
68 : : {
69 : 3 : int *out_warning_level = user_data;
70 : :
71 : 3 : *out_warning_level = level;
72 : :
73 : 3 : g_main_context_wakeup (NULL);
74 : 3 : }
75 : :
76 : : static void
77 : 1 : test_receive_signals (SetupData *setup, gconstpointer data)
78 : : {
79 : : GMemoryMonitorPsi *monitor;
80 : : unsigned long warning_id;
81 : 1 : int warning_level = -1;
82 : 1 : GError *local_error = NULL;
83 : :
84 : 1 : monitor = g_object_new (G_TYPE_MEMORY_MONITOR_PSI,
85 : : "proc-path", setup->mock_proc_path,
86 : : NULL);
87 : 1 : warning_id = g_signal_connect (monitor, "low-memory-warning",
88 : : G_CALLBACK (memory_monitor_psi_signal_cb), &warning_level);
89 : 1 : g_initable_init (G_INITABLE (monitor), NULL, &local_error);
90 : 1 : g_assert_no_error (local_error);
91 : :
92 : 1 : g_file_set_contents (setup->mock_psi_path,
93 : : "test",
94 : : -1,
95 : : &local_error);
96 : 1 : g_assert_no_error (local_error);
97 : :
98 : 2 : while (warning_level == -1)
99 : 1 : g_main_context_iteration (NULL, TRUE);
100 : :
101 : 1 : g_assert_true (warning_level == 50 ||
102 : : warning_level == 100 ||
103 : : warning_level == 255);
104 : :
105 : 1 : g_signal_handler_disconnect (monitor, warning_id);
106 : 1 : g_object_unref (monitor);
107 : 1 : }
108 : :
109 : : int
110 : 1 : main (int argc, char **argv)
111 : : {
112 : 1 : g_setenv ("GIO_USE_MEMORY_MONITOR", "psi", TRUE);
113 : :
114 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
115 : 1 : g_test_add ("/memory-monitor-psi/receive-signal", SetupData, NULL,
116 : : tests_setup, test_receive_signals, tests_teardown);
117 : :
118 : 1 : return g_test_run ();
119 : : }
|