From: Yu Watanabe Date: Mon, 4 Apr 2022 09:39:38 +0000 (+0900) Subject: test-sd-device: allow several devices removed during running test X-Git-Tag: v251-rc2~197^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b1d723228e327e80df190ad89be9c4abc2d49bf8;p=thirdparty%2Fsystemd.git test-sd-device: allow several devices removed during running test To make the test stabler. --- diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index 222c5b9dbc9..560a73ad260 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -173,39 +173,54 @@ TEST(sd_device_enumerator_subsystems) { test_sd_device_one(d); } -static unsigned test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) { +static void test_sd_device_enumerator_filter_subsystem_one( + const char *subsystem, + Hashmap *h, + unsigned *ret_n_new_dev, + unsigned *ret_n_removed_dev) { + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - sd_device *d, *t; - unsigned n_new_dev = 0; + unsigned n_new_dev = 0, n_removed_dev = 0; + sd_device *d; assert_se(sd_device_enumerator_new(&e) >= 0); assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0); FOREACH_DEVICE(e, d) { const char *syspath; + sd_device *t; assert_se(sd_device_get_syspath(d, &syspath) >= 0); t = hashmap_remove(h, syspath); - assert_se(!sd_device_unref(t)); - if (t) - log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath); - else { + if (!t) { log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath); n_new_dev++; } + + assert_se(!sd_device_unref(t)); } - /* Assume no device is unplugged. */ - assert_se(hashmap_isempty(h)); + HASHMAP_FOREACH(d, h) { + const char *syspath; - return n_new_dev; + assert_se(sd_device_get_syspath(d, &syspath) >= 0); + log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath); + n_removed_dev++; + + assert_se(!sd_device_unref(d)); + } + + hashmap_free(h); + + *ret_n_new_dev = n_new_dev; + *ret_n_removed_dev = n_removed_dev; } TEST(sd_device_enumerator_filter_subsystem) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; _cleanup_(hashmap_freep) Hashmap *subsystems; - unsigned n_new_dev = 0; + unsigned n_new_dev = 0, n_removed_dev = 0; sd_device *d; Hashmap *h; char *s; @@ -239,16 +254,22 @@ TEST(sd_device_enumerator_filter_subsystem) { } while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) { - n_new_dev += test_sd_device_enumerator_filter_subsystem_one(s, h); - hashmap_free(h); + unsigned n, m; + + test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m); free(s); + + n_new_dev += n; + n_removed_dev += m; } if (n_new_dev > 0) - log_warning("%u new device is found in re-scan", n_new_dev); + log_warning("%u new devices are found in re-scan", n_new_dev); + if (n_removed_dev > 0) + log_warning("%u devices removed in re-scan", n_removed_dev); - /* Assume that not so many devices are plugged. */ - assert_se(n_new_dev <= 10); + /* Assume that not so many devices are plugged or unplugged. */ + assert_se(n_new_dev + n_removed_dev <= 10); } TEST(sd_device_new_from_nulstr) {