summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoss Burton <ross@linux.intel.com>2009-05-29 16:57:36 +0100
committerRoss Burton <ross@linux.intel.com>2009-05-29 16:57:36 +0100
commit0266c22844c7f770863440d8e8b4215c3b7467f5 (patch)
treef4f41ed49fd4cae4168bc5ad888899e9000e8251
parent3ee0889aaf55c24aa8be7e14ccfc8bd6923bb10c (diff)
downloadlibrest-0266c22844c7f770863440d8e8b4215c3b7467f5.tar.gz
Add basic test case for threaded ops
-rw-r--r--.gitignore1
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/threaded.c109
3 files changed, 112 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index a21bb95..3622e8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,4 +40,5 @@ examples/test-facebook
tests/oauth
tests/proxy
tests/flickr
+tests/threaded
rest/test-runner
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7a930d9..5e48c8a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-TESTS = proxy oauth flickr
+TESTS = proxy threaded oauth flickr
AM_CPPFLAGS = $(SOUP_CFLAGS) -I$(top_srcdir)
AM_LDFLAGS = $(SOUP_LIBS) ../rest/librest.la
@@ -6,5 +6,6 @@ AM_LDFLAGS = $(SOUP_LIBS) ../rest/librest.la
check_PROGRAMS = $(TESTS)
proxy_SOURCES = proxy.c
+threaded_SOURCES = threaded.c
oauth_SOURCES = oauth.c
flickr_SOURCES = flickr.c
diff --git a/tests/threaded.c b/tests/threaded.c
new file mode 100644
index 0000000..2ecfe49
--- /dev/null
+++ b/tests/threaded.c
@@ -0,0 +1,109 @@
+/*
+ * librest - RESTful web services access
+ * Copyright (c) 2009 Intel Corporation.
+ *
+ * Authors: Rob Bradford <rob@linux.intel.com>
+ * Ross Burton <ross@linux.intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <libsoup/soup.h>
+#include <rest/rest-proxy.h>
+
+static volatile int errors = 0;
+
+static void
+server_callback (SoupServer *server, SoupMessage *msg,
+ const char *path, GHashTable *query,
+ SoupClientContext *client, gpointer user_data)
+{
+ if (g_str_equal (path, "/ping")) {
+ soup_message_set_status (msg, SOUP_STATUS_OK);
+ } else {
+ soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
+ }
+}
+
+static gpointer
+func (gpointer data)
+{
+ RestProxy *proxy;
+ RestProxyCall *call;
+ const char *url = data;
+ GError *error = NULL;
+
+ proxy = rest_proxy_new (url, FALSE);
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "ping");
+
+ if (!rest_proxy_call_sync (call, &error)) {
+ g_printerr ("Call failed: %s\n", error->message);
+ g_error_free (error);
+ g_atomic_int_add (&errors, 1);
+ goto done;
+ }
+
+ if (rest_proxy_call_get_status_code (call) != SOUP_STATUS_OK) {
+ g_printerr ("Wrong response code, got %d\n", rest_proxy_call_get_status_code (call));
+ g_atomic_int_add (&errors, 1);
+ goto done;
+ }
+
+ g_print ("Thread %p done\n", g_thread_self ());
+
+ done:
+ g_object_unref (call);
+ g_object_unref (proxy);
+ return NULL;
+}
+
+int
+main (int argc, char **argv)
+{
+ SoupSession *session;
+ SoupServer *server;
+ GThread *threads[10];
+ char *url;
+ int i;
+
+ g_thread_init (NULL);
+ g_type_init ();
+
+ session = soup_session_sync_new ();
+
+ server = soup_server_new (NULL);
+ soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
+ g_thread_create (soup_server_run, server, FALSE, NULL);
+
+ url = g_strdup_printf ("http://localhost:%d/", soup_server_get_port (server));
+
+ for (i = 0; i < G_N_ELEMENTS (threads); i++) {
+ threads[i] = g_thread_create (func, url, TRUE, NULL);
+ g_print ("Starting thread %p\n", threads[i]);
+ }
+
+ for (i = 0; i < G_N_ELEMENTS (threads); i++) {
+ g_thread_join (threads[i]);
+ }
+
+ g_free (url);
+
+ return errors != 0;
+}