[gnio] Add cancellation testing code to server.c



commit 739e14802ae243f443934c13f085060437b4c79c
Author: Alexander Larsson <alexl redhat com>
Date:   Wed May 6 12:25:59 2009 +0200

    Add cancellation testing code to server.c
---
 test/server.c |   36 +++++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/test/server.c b/test/server.c
index d3030fb..904bb30 100644
--- a/test/server.c
+++ b/test/server.c
@@ -12,10 +12,13 @@ gboolean dont_reuse_address = FALSE;
 gboolean non_blocking = FALSE;
 gboolean use_udp = FALSE;
 gboolean use_source = FALSE;
+int cancel_timeout = 0;
 
 static GOptionEntry cmd_entries[] = {
   {"port", 'p', 0, G_OPTION_ARG_INT, &port,
    "Local port to bind to", NULL},
+  {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
+   "Cancel any op after the specified amount of seconds", NULL},
   {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
    "Use udp instead of tcp", NULL},
   {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
@@ -55,6 +58,7 @@ source_ready (gpointer data,
 static void
 ensure_condition (GSocket *socket,
 		  const char *where,
+		  GCancellable *cancellable,
 		  GIOCondition condition)
 {
   GError *error = NULL;
@@ -67,7 +71,7 @@ ensure_condition (GSocket *socket,
     {
       source = g_socket_create_source (socket,
                                        condition,
-                                       NULL);
+                                       cancellable);
       g_source_set_callback (source,
                              (GSourceFunc) source_ready,
 			     NULL, NULL);
@@ -77,7 +81,7 @@ ensure_condition (GSocket *socket,
     }
   else
     {
-      if (!g_socket_condition_wait (socket, condition, NULL, &error))
+      if (!g_socket_condition_wait (socket, condition, cancellable, &error))
 	{
 	  g_printerr ("condition wait error for %s: %s\n",
 		      where,
@@ -87,6 +91,17 @@ ensure_condition (GSocket *socket,
     }
 }
 
+static gpointer
+cancel_thread (gpointer data)
+{
+  GCancellable *cancellable = data;
+
+  g_usleep (1000*1000*cancel_timeout);
+  g_print ("Cancelling\n");
+  g_cancellable_cancel (cancellable);
+  return NULL;
+}
+
 int
 main (int argc,
       char *argv[])
@@ -97,6 +112,7 @@ main (int argc,
   GSocketType socket_type;
   GError *error = NULL;
   GOptionContext *context;
+  GCancellable *cancellable;
 
   g_thread_init (NULL);
 
@@ -110,6 +126,16 @@ main (int argc,
       return 1;
     }
 
+  if (cancel_timeout)
+    {
+      cancellable = g_cancellable_new ();
+      g_thread_create (cancel_thread, cancellable, FALSE, NULL);
+    }
+  else
+    {
+      cancellable = NULL;
+    }
+
   loop = g_main_loop_new (NULL, FALSE);
 
   if (use_udp)
@@ -148,7 +174,7 @@ main (int argc,
 
       g_print ("listening on port %d...\n", port);
 
-      ensure_condition (socket, "accept", G_IO_IN);
+      ensure_condition (socket, "accept", cancellable, G_IO_IN);
       new_socket = g_socket_accept (socket, &error);
       if (!new_socket)
 	{
@@ -187,7 +213,7 @@ main (int argc,
       gssize size;
       gsize to_send;
 
-      ensure_condition (recv_socket, "receive", G_IO_IN);
+      ensure_condition (recv_socket, "receive", cancellable, G_IO_IN);
       if (use_udp)
 	size = g_socket_receive_from (recv_socket, &address,
 				      buffer, sizeof buffer, &error);
@@ -219,7 +245,7 @@ main (int argc,
 
       while (to_send > 0)
 	{
-	  ensure_condition (recv_socket, "send", G_IO_OUT);
+	  ensure_condition (recv_socket, "send", cancellable, G_IO_OUT);
 	  if (use_udp)
 	    size = g_socket_send_to (recv_socket, address,
 				     buffer, to_send, &error);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]