diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-06-06 22:58:03 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-06-06 23:01:57 +0200 |
commit | 869881a6cb466ed1b9aa63e7e9de233bcf35a0ef (patch) | |
tree | 5f8bd8bb5066473ad44f0d87de6a74c552224018 | |
parent | shared/bus-util: use the new cleanup functionality to avoid a memleak (diff) | |
download | systemd-869881a6cb466ed1b9aa63e7e9de233bcf35a0ef.tar.xz systemd-869881a6cb466ed1b9aa63e7e9de233bcf35a0ef.zip |
test-bus-util: add a test for destroy callbacks
-rw-r--r-- | src/test/test-bus-util.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/test-bus-util.c b/src/test/test-bus-util.c index 2543fa534f..791b3928fe 100644 --- a/src/test/test-bus-util.c +++ b/src/test/test-bus-util.c @@ -35,6 +35,48 @@ static void test_name_async(unsigned n_messages) { } } +static int callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { + return 1; +} + +static void destroy_callback(void *userdata) { + int *n_called = userdata; + + (*n_called) ++; +} + +static void test_destroy_callback(void) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + sd_bus_slot *slot = NULL; + sd_bus_destroy_t t; + + int r, n_called = 0; + + log_info("/* %s */", __func__); + + r = bus_open_system_watch_bind_with_description(&bus, "test-bus"); + if (r < 0) { + log_error_errno(r, "Failed to connect to bus: %m"); + return; + } + + r = sd_bus_request_name_async(bus, &slot, "org.freedesktop.systemd.test-bus-util", 0, callback, &n_called); + assert(r == 1); + + assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 0); + assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 0); + + assert_se(sd_bus_slot_set_destroy_callback(slot, destroy_callback) == 0); + assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 1); + assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 1); + assert_se(t == destroy_callback); + + /* Force cleanup so we can look at n_called */ + assert(n_called == 0); + sd_bus_slot_unref(slot); + assert(n_called == 1); +} + int main(int argc, char **argv) { log_set_max_level(LOG_DEBUG); log_parse_environment(); @@ -42,6 +84,7 @@ int main(int argc, char **argv) { test_name_async(0); test_name_async(20); + test_destroy_callback(); return 0; } |