From c07e165664dce0fbf2c2a3e0d4123a82c3022a6d Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Wed, 2 Jul 2014 16:36:44 +0100 Subject: [PATCH 7/7] dbus-test-tool spam: add --new-conn https://bugs.freedesktop.org/show_bug.cgi?id=34140 --- tools/dbus-spam.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/tools/dbus-spam.c b/tools/dbus-spam.c index 43b9793..8dda8cb 100644 --- a/tools/dbus-spam.c +++ b/tools/dbus-spam.c @@ -51,6 +51,8 @@ usage (int ecode) " --queue=N queue up N messages at a time (default 1)\n" " --flood send all messages immediately\n" " --no-reply set the NO_REPLY flag (implies --flood)\n" + " --new-conn use a new connection for each message\n" + " (incompatible with --flood or --queue > 1\n" "\n" " --string send payload as a string (default)\n" " --bytes send payload as a byte-array\n" @@ -141,7 +143,7 @@ consume_stdin (char **payload_p, int dbus_test_tool_spam (int argc, char **argv) { - DBusConnection *connection; + DBusConnection *connection = NULL; DBusError error = DBUS_ERROR_INIT; DBusBusType type = DBUS_BUS_SESSION; const char *destination = DBUS_SERVICE_DBUS; @@ -155,7 +157,9 @@ dbus_test_tool_spam (int argc, char **argv) size_t payload_len; int payload_type = DBUS_TYPE_STRING; DBusMessage *template = NULL; + dbus_bool_t flood = FALSE; dbus_bool_t no_reply = FALSE; + dbus_bool_t new_conn = FALSE; unsigned int seed = time (NULL); int n_random_sizes = 0; unsigned int *random_sizes = NULL; @@ -271,6 +275,10 @@ dbus_test_tool_spam (int argc, char **argv) } else if (strcmp (arg, "--flood") == 0) { + if (new_conn) + usage (2); + + flood = TRUE; queue_len = -1; } else if (strcmp (arg, "--no-reply") == 0) @@ -284,11 +292,24 @@ dbus_test_tool_spam (int argc, char **argv) if (queue_len < 1) usage (2); + + if (new_conn && queue_len != 1) + usage (2); } else if (strstr (arg, "--seed=") == arg) { seed = strtoul (arg + strlen ("--seed="), NULL, 10); } + else if (strcmp (arg, "--new-conn") == 0) + { + new_conn = TRUE; + + if (new_conn && flood) + usage (2); + + if (new_conn && queue_len > 1) + usage (2); + } else { usage (2); @@ -306,18 +327,42 @@ dbus_test_tool_spam (int argc, char **argv) VERBOSE (stderr, "Will send up to %d messages, with up to %d queued\n", count, queue_len); - connection = dbus_bus_get_private (type, &error); - - if (connection == NULL) + if (!new_conn) { - fprintf (stderr, "Failed to connect to bus: %s: %s\n", - error.name, error.message); - dbus_error_free (&error); - return 1; + connection = dbus_bus_get_private (type, &error); + + if (connection == NULL) + { + fprintf (stderr, "Failed to connect to bus: %s: %s\n", + error.name, error.message); + dbus_error_free (&error); + return 1; + } } while (no_reply ? sent < count : received < count) { + if (new_conn && sent < count && + (queue_len == -1 || sent < queue_len + received)) + { + if (connection != NULL) + { + dbus_connection_flush (connection); + dbus_connection_close (connection); + dbus_connection_unref (connection); + } + + connection = dbus_bus_get_private (type, &error); + + if (connection == NULL) + { + fprintf (stderr, "Failed to connect to bus: %s: %s\n", + error.name, error.message); + dbus_error_free (&error); + return 1; + } + } + while (sent < count && (queue_len == -1 || sent < queue_len + received)) { @@ -428,6 +473,9 @@ dbus_test_tool_spam (int argc, char **argv) } dbus_message_unref (message); + + if (new_conn) + break; } if (!dbus_connection_read_write_dispatch (connection, -1)) @@ -437,7 +485,8 @@ dbus_test_tool_spam (int argc, char **argv) } } - dbus_connection_flush (connection); + if (!new_conn) + dbus_connection_flush (connection); VERBOSE (stderr, "Done\n"); @@ -446,8 +495,11 @@ dbus_test_tool_spam (int argc, char **argv) if (template != NULL) dbus_message_unref (template); - dbus_connection_close (connection); - dbus_connection_unref (connection); + if (!new_conn) + { + dbus_connection_close (connection); + dbus_connection_unref (connection); + } dbus_shutdown (); return 0; } -- 1.8.5.3