]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test case for restarting device monitor 33032/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 1 Aug 2024 06:46:03 +0000 (15:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Aug 2024 02:22:24 +0000 (11:22 +0900)
src/libsystemd/sd-device/test-sd-device-monitor.c

index 4beab76b4d6bd1826863213af95bce0a9e2159ab..d6a6e5d364bb5417aa2cf36226857c69c853d926 100644 (file)
@@ -114,6 +114,43 @@ TEST(sd_device_monitor_is_running) {
         ASSERT_EQ(sd_device_monitor_is_running(m), 0);
 }
 
+TEST(sd_device_monitor_start_stop) {
+        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
+        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
+        union sockaddr_union sa;
+        const char *syspath;
+
+        prepare_loopback(&device);
+
+        ASSERT_OK(sd_device_get_syspath(device, &syspath));
+
+        prepare_monitor(&monitor_server, &monitor_client, &sa);
+
+        /* Sending devices before starting client. */
+        ASSERT_OK(sd_device_enumerator_new(&e));
+        send_by_enumerator(monitor_server, &sa, e, 5, syspath);
+
+        /* sd_device_monitor_start() can be called multiple times. */
+        ASSERT_OK(sd_device_monitor_start(monitor_client, NULL, NULL));
+        ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
+
+        /* Sending devices after client being started. */
+        send_by_enumerator(monitor_server, &sa, e, 5, syspath);
+
+        /* sd_device_monitor_stop() can be called multiple times. */
+        ASSERT_OK(sd_device_monitor_stop(monitor_client));
+        ASSERT_OK(sd_device_monitor_stop(monitor_client));
+
+        /* Sending devices before restarting client. */
+        send_by_enumerator(monitor_server, &sa, e, 5, syspath);
+
+        /* Restart monitor, and check if the previously sent devices are ignored. */
+        ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
+        ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
+        ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
+}
+
 TEST(refuse_invalid_device) {
         _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
         _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;