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