]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
test1541: threaded connection sharing
authorDaniel Stenberg <daniel@haxx.se>
Sun, 17 Mar 2019 22:37:35 +0000 (23:37 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 18 Mar 2019 12:58:46 +0000 (13:58 +0100)
The threaded-shared-conn.c example turned into test case. Only works if
pthread was detected.

An attempt to detect future regressions such as e3a53e3efb942a5

Closes #3687

tests/data/Makefile.inc
tests/data/test1541 [new file with mode: 0644]
tests/libtest/Makefile.inc
tests/libtest/lib1541.c [new file with mode: 0644]

index 359b2deb26e58fe257092b2009050254a52f21bc..2eca9c6709fb8a18bef4e5ae893722543ab375bd 100644 (file)
@@ -174,7 +174,7 @@ test1516 test1517 test1518 test1519 test1520 test1521 test1522 \
 \
 test1525 test1526 test1527 test1528 test1529 test1530 test1531 test1532 \
 test1533 test1534 test1535 test1536 test1537 test1538 \
-test1540 \
+test1540 test1541 \
 test1550 test1551 test1552 test1553 test1554 test1555 test1556 test1557 \
 test1558          test1560 test1561 test1562 \
 \
diff --git a/tests/data/test1541 b/tests/data/test1541
new file mode 100644 (file)
index 0000000..28b91c7
--- /dev/null
@@ -0,0 +1,32 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+HTTP GET
+multi-threaded
+connection-sharing
+</keywords>
+</info>
+
+# Server-side
+</reply>
+# Client-side
+<client>
+<server>
+http
+</server>
+<tool>
+lib1541
+</tool>
+ <name>
+connection sharing using 67 parallel threads for 7 seconds
+ </name>
+ <command>
+http://%HOSTIP:%HTTPPORT/1
+</command>
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+</verify>
+</testcase>
index 1817dc34839757a8acef0d8328930b87618facee..e38f48102c0915ceac3c7c42e31a12a4975edd86 100644 (file)
@@ -29,7 +29,7 @@ noinst_PROGRAMS = chkhostname libauthretry libntlmconnect                \
  lib1518         lib1520 lib1521 lib1522 \
  lib1525 lib1526 lib1527 lib1528 lib1529 lib1530 lib1531 lib1532 lib1533 \
  lib1534 lib1535 lib1536 lib1537 lib1538 \
- lib1540 \
+ lib1540 lib1541 \
  lib1550 lib1551 lib1552 lib1553 lib1554 lib1555 lib1556 lib1557 \
  lib1558 \
  lib1560 \
@@ -493,6 +493,10 @@ lib1540_SOURCES = lib1540.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
 lib1540_LDADD = $(TESTUTIL_LIBS)
 lib1540_CPPFLAGS = $(AM_CPPFLAGS)
 
+lib1541_SOURCES = lib1541.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
+lib1541_LDADD = $(TESTUTIL_LIBS)
+lib1541_CPPFLAGS = $(AM_CPPFLAGS)
+
 lib1550_SOURCES = lib1550.c $(SUPPORTFILES)
 lib1550_CPPFLAGS = $(AM_CPPFLAGS) -DLIB1517
 
diff --git a/tests/libtest/lib1541.c b/tests/libtest/lib1541.c
new file mode 100644 (file)
index 0000000..983a47e
--- /dev/null
@@ -0,0 +1,151 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at https://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "test.h"
+
+#include "testutil.h"
+#include "warnless.h"
+#include "memdebug.h"
+
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#include <time.h>
+
+/* number of threads to fire up in parallel */
+#define NUM_THREADS 67
+
+/* for how many seconds each thread will loop */
+#define RUN_FOR_SECONDS 7
+
+static pthread_mutex_t connlock;
+
+static size_t write_db(void *ptr, size_t size, size_t nmemb, void *data)
+{
+  /* not interested in the downloaded bytes, return the size */
+  (void)ptr;  /* unused */
+  (void)data; /* unused */
+  return (size_t)(size * nmemb);
+}
+
+static void lock_cb(CURL *handle, curl_lock_data data,
+                    curl_lock_access access, void *userptr)
+{
+  (void)access; /* unused */
+  (void)userptr; /* unused */
+  (void)handle; /* unused */
+  (void)data; /* unused */
+  pthread_mutex_lock(&connlock);
+}
+
+static void unlock_cb(CURL *handle, curl_lock_data data,
+                      void *userptr)
+{
+  (void)userptr; /* unused */
+  (void)handle;  /* unused */
+  (void)data;    /* unused */
+  pthread_mutex_unlock(&connlock);
+}
+
+static void init_locks(void)
+{
+  pthread_mutex_init(&connlock, NULL);
+}
+
+static void kill_locks(void)
+{
+  pthread_mutex_destroy(&connlock);
+}
+
+struct initurl {
+  const char *url;
+  CURLSH *share;
+  int threadno;
+};
+
+static void *run_thread(void *ptr)
+{
+  struct initurl *u = (struct initurl *)ptr;
+  int i;
+  time_t end = time(NULL) + RUN_FOR_SECONDS;
+
+  for(i = 0; time(NULL) < end; i++) {
+    CURL *curl = curl_easy_init();
+    curl_easy_setopt(curl, CURLOPT_URL, u->url);
+    curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
+    curl_easy_setopt(curl, CURLOPT_SHARE, u->share);
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_db);
+    curl_easy_perform(curl); /* ignores error */
+    curl_easy_cleanup(curl);
+    fprintf(stderr, "Thread %d transfer %d\n", u->threadno, i);
+  }
+
+  return NULL;
+}
+
+int test(char *URL)
+{
+  pthread_t tid[NUM_THREADS];
+  int i;
+  int error;
+  CURLSH *share;
+  struct initurl url[NUM_THREADS];
+
+  /* Must initialize libcurl before any threads are started */
+  curl_global_init(CURL_GLOBAL_ALL);
+
+  share = curl_share_init();
+  curl_share_setopt(share, CURLSHOPT_LOCKFUNC, lock_cb);
+  curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, unlock_cb);
+  curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
+
+  init_locks();
+
+  for(i = 0; i< NUM_THREADS; i++) {
+    url[i].url = URL;
+    url[i].share = share;
+    url[i].threadno = i;
+    error = pthread_create(&tid[i], NULL, run_thread, &url[i]);
+    if(0 != error)
+      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
+    else
+      fprintf(stderr, "Thread %d, gets %s\n", i, URL);
+  }
+
+  /* now wait for all threads to terminate */
+  for(i = 0; i< NUM_THREADS; i++) {
+    error = pthread_join(tid[i], NULL);
+    fprintf(stderr, "Thread %d terminated\n", i);
+  }
+
+  kill_locks();
+
+  curl_share_cleanup(share);
+  curl_global_cleanup();
+  return 0;
+}
+
+#else /* without pthread, this test doesn't work */
+int test(char *URL)
+{
+  (void)URL;
+  return 0;
+}
+#endif