[libsoup/autobahn-integration-new: 1/3] autobahn-test.c: Implement timeout in main loop




commit d794ab343d823b21395aeb51a93d5371f7839694
Author: Diego Pino Garcia <dpino igalia com>
Date:   Thu Mar 11 07:30:18 2021 +0000

    autobahn-test.c: Implement timeout in main loop

 tests/autobahn/autobahn-test.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/tests/autobahn/autobahn-test.c b/tests/autobahn/autobahn-test.c
index e9c6fd88..a691e2e2 100644
--- a/tests/autobahn/autobahn-test.c
+++ b/tests/autobahn/autobahn-test.c
@@ -27,6 +27,8 @@
 static char *address = "ws://localhost:9001";
 static char *agent = "libsoup";
 
+static unsigned long int AUTOBAHN_TEST_TIMEOUT = 20;
+
 typedef void (*ConnectionFunc) (SoupWebsocketConnection *socket_connection,
                                 gint type,
                                 GBytes *message,
@@ -58,7 +60,7 @@ on_message_received (SoupWebsocketConnection *socket_connection,
 {
         ConnectionContext *ctx = (ConnectionContext *)data;
 
-        g_test_message ("Message recieved");
+        g_test_message ("Message received");
 
         if (ctx && ctx->method)
                 ctx->method (socket_connection, type, message, ctx->data);
@@ -116,8 +118,26 @@ connect_and_run (SoupSession *session, char *path, ConnectionFunc method, gpoint
         g_test_message ("Connecting to %s", uri);
         soup_session_websocket_connect_async (session, message, NULL, NULL, G_PRIORITY_DEFAULT, NULL, 
on_connect, ctx);
 
-        while (!ctx->done)
-               g_main_context_iteration (async_context, TRUE);
+        time_t now = time(NULL);
+        const time_t threshold = now + AUTOBAHN_TEST_TIMEOUT;
+
+        char timestr[32];
+        strftime(timestr, 32, "%Y-%m-%d %H:%M:%S", localtime(&now));
+        fprintf(stderr, "### Run %s (%s)\n", uri, timestr);
+        while (!ctx->done) {
+                g_main_context_iteration (async_context, TRUE);
+                now = time(NULL);
+                if (now > threshold) {
+                        debug_printf (1, "Test timeout: %s\n", uri);
+                        g_test_message ("Test timeout: %s\n", uri);
+
+                        // FIXME: On a timeout, if ctx is freed no more test are executed.
+                        g_object_unref (message);
+                        g_free (uri);
+                        g_main_context_unref (async_context);
+                        return;
+                }
+        }
 
         g_object_unref (message);
         g_free (uri);
@@ -247,6 +267,9 @@ int main (int argc, char *argv[])
                 num_cases = num_case;
         }
 
+        if (getenv ("AUTOBAHN_TEST_TIMEOUT"))
+                AUTOBAHN_TEST_TIMEOUT = atol (getenv ("AUTOBAHN_TEST_TIMEOUT"));
+
         session = soup_session_new ();
         soup_session_add_feature_by_type (session, SOUP_TYPE_WEBSOCKET_EXTENSION_MANAGER);
 


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