Bug 626748 – Use async methods for writing and handle EAGAIN
[platform/upstream/glib.git] / gio / tests / gdbus-connection.c
index 6d3c483..fc02746 100644 (file)
@@ -917,6 +917,85 @@ test_connection_basic (void)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+/* Message size > 20MiB ... should be enough to make sure the message
+ * is fragmented when shoved across any transport
+ */
+#define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
+
+static void
+large_message_on_name_appeared (GDBusConnection *connection,
+                                const gchar *name,
+                                const gchar *name_owner,
+                                gpointer user_data)
+{
+  GError *error;
+  gchar *request;
+  const gchar *reply;
+  GVariant *result;
+  guint n;
+
+  request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
+  for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
+    request[n] = '0' + (n%10);
+  request[n] = '\0';
+
+  error = NULL;
+  result = g_dbus_connection_call_sync (connection,
+                                        "com.example.TestService",      /* bus name */
+                                        "/com/example/TestObject",      /* object path */
+                                        "com.example.Frob",             /* interface name */
+                                        "HelloWorld",                   /* method name */
+                                        g_variant_new ("(s)", request), /* parameters */
+                                        G_VARIANT_TYPE ("(s)"),         /* return type */
+                                        G_DBUS_CALL_FLAGS_NONE,
+                                        -1,
+                                        NULL,
+                                        &error);
+  g_assert_no_error (error);
+  g_assert (result != NULL);
+  g_variant_get (result, "(&s)", &reply);
+  g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
+  g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
+  g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
+  g_variant_unref (result);
+
+  g_free (request);
+
+  g_main_loop_quit (loop);
+}
+
+static void
+large_message_on_name_vanished (GDBusConnection *connection,
+                                const gchar *name,
+                                gpointer user_data)
+{
+}
+
+static void
+test_connection_large_message (void)
+{
+  guint watcher_id;
+
+  session_bus_up ();
+
+  /* this is safe; testserver will exit once the bus goes away */
+  g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
+
+  watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
+                                 "com.example.TestService",
+                                 G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                 large_message_on_name_appeared,
+                                 large_message_on_name_vanished,
+                                 NULL,  /* user_data */
+                                 NULL); /* GDestroyNotify */
+  g_main_loop_run (loop);
+  g_bus_unwatch_name (watcher_id);
+
+  session_bus_down ();
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 int
 main (int   argc,
       char *argv[])
@@ -939,5 +1018,6 @@ main (int   argc,
   g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
   g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
+  g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
   return g_test_run();
 }