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 "gio.h"
22 : :
23 : : #include "gmemorymonitorbase.h"
24 : : #include "gmemorymonitorpoll.h"
25 : :
26 : : typedef struct
27 : : {
28 : : double simulated_mem_free_ratio;
29 : : int expected_warning_level;
30 : : } TestData;
31 : :
32 : : static void
33 : 3 : memory_monitor_signal_cb (GMemoryMonitor *m,
34 : : GMemoryMonitorWarningLevel level,
35 : : void *user_data)
36 : : {
37 : 3 : int *out_warning_level = user_data;
38 : :
39 : 3 : g_assert_cmpint (*out_warning_level, ==, -1);
40 : 3 : *out_warning_level = level;
41 : :
42 : 3 : g_main_context_wakeup (NULL);
43 : 3 : }
44 : :
45 : : static void
46 : 1 : test_dup_default (void)
47 : : {
48 : : GMemoryMonitor *monitor;
49 : :
50 : 1 : monitor = g_memory_monitor_dup_default ();
51 : 1 : g_assert_nonnull (monitor);
52 : 1 : g_object_unref (monitor);
53 : 1 : }
54 : :
55 : : static void
56 : 3 : test_event (const void *data)
57 : : {
58 : 3 : const TestData *test_data = data;
59 : : GMemoryMonitorPoll *monitor;
60 : 3 : GError *local_error = NULL;
61 : 3 : int warning_level = -1;
62 : : unsigned long warning_id;
63 : :
64 : 3 : monitor = g_object_new (G_TYPE_MEMORY_MONITOR_POLL,
65 : : "poll-interval-ms", 50,
66 : 3 : "mem-free-ratio", test_data->simulated_mem_free_ratio,
67 : : NULL);
68 : 3 : warning_id = g_signal_connect (monitor, "low-memory-warning",
69 : : G_CALLBACK (memory_monitor_signal_cb), &warning_level);
70 : 3 : g_initable_init (G_INITABLE (monitor), NULL, &local_error);
71 : 3 : g_assert_no_error (local_error);
72 : :
73 : 8 : while (warning_level == -1)
74 : 5 : g_main_context_iteration (NULL, TRUE);
75 : :
76 : 3 : g_assert_cmpint (warning_level, ==, test_data->expected_warning_level);
77 : :
78 : 3 : g_signal_handler_disconnect (monitor, warning_id);
79 : 3 : g_object_unref (monitor);
80 : 3 : }
81 : :
82 : : static void
83 : 0 : warning_cb (GMemoryMonitor *m,
84 : : GMemoryMonitorWarningLevel level,
85 : : GMainLoop *test_loop)
86 : : {
87 : 0 : char *str = g_enum_to_string (G_TYPE_MEMORY_MONITOR_WARNING_LEVEL, level);
88 : 0 : g_free (str);
89 : 0 : g_main_loop_quit (test_loop);
90 : 0 : }
91 : :
92 : : static void
93 : 0 : do_watch_memory (void)
94 : : {
95 : : GMemoryMonitor *m;
96 : : GMainLoop *loop;
97 : :
98 : 0 : m = g_memory_monitor_dup_default ();
99 : 0 : loop = g_main_loop_new (NULL, TRUE);
100 : 0 : g_signal_connect (G_OBJECT (m), "low-memory-warning",
101 : : G_CALLBACK (warning_cb), loop);
102 : :
103 : 0 : g_main_loop_run (loop);
104 : :
105 : 0 : g_signal_handlers_disconnect_by_func (G_OBJECT (m),
106 : : G_CALLBACK (warning_cb), loop);
107 : :
108 : 0 : g_main_loop_unref (loop);
109 : 0 : g_clear_object (&m);
110 : 0 : }
111 : :
112 : : int
113 : 1 : main (int argc, char **argv)
114 : : {
115 : 1 : if (argc == 2 && strcmp (argv[1], "--watch") == 0)
116 : : {
117 : 0 : do_watch_memory ();
118 : 0 : return 0;
119 : : }
120 : :
121 : 1 : g_setenv ("GIO_USE_MEMORY_MONITOR", "poll", TRUE);
122 : 1 : g_test_init (&argc, &argv, NULL);
123 : :
124 : 1 : g_test_add_func ("/memory-monitor-poll/dup-default", test_dup_default);
125 : :
126 : 1 : g_test_add_data_func ("/memory-monitor-poll/critical-event",
127 : 1 : &((const TestData) {
128 : : .simulated_mem_free_ratio = 0.19,
129 : : .expected_warning_level = 255,
130 : : }), test_event);
131 : 1 : g_test_add_data_func ("/memory-monitor-poll/medium-event",
132 : 1 : &((const TestData) {
133 : : .simulated_mem_free_ratio = 0.29,
134 : : .expected_warning_level = 100,
135 : : }), test_event);
136 : 1 : g_test_add_data_func ("/memory-monitor-poll/low-event",
137 : 1 : &((const TestData) {
138 : : .simulated_mem_free_ratio = 0.39,
139 : : .expected_warning_level = 50,
140 : : }), test_event);
141 : :
142 : 1 : return g_test_run ();
143 : : }
|