Skip to content

Commit

Permalink
tests/libtest/lib3026.c: try to implement test for Windows
Browse files Browse the repository at this point in the history
Work in Progress - untested - do not merge - PR for testing
  • Loading branch information
mback2k committed Jun 14, 2022
1 parent 4572489 commit c750e3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
1 change: 0 additions & 1 deletion tests/data/test3026
Expand Up @@ -19,7 +19,6 @@ slow
# be used for it
<features>
threaded-resolver
!win32
</features>
<server>
none
Expand Down
57 changes: 52 additions & 5 deletions tests/libtest/lib3026.c
Expand Up @@ -26,10 +26,6 @@
#include "testutil.h"
#include "warnless.h"

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#include <unistd.h>

#define NUM_THREADS 1000

static void *run_thread(void *ptr)
Expand All @@ -43,6 +39,56 @@ static void *run_thread(void *ptr)
return NULL;
}

#ifdef WIN32
int test(char *URL)
{
CURLcode results[NUM_THREADS];
HANDLE tids[NUM_THREADS];
unsigned tid_count = NUM_THREADS, i;
int test_failure = 0;
curl_version_info_data *ver;
(void) URL;

ver = curl_version_info(CURLVERSION_NOW);
if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
fprintf(stderr, "%s:%d On Windows but the "
"CURL_VERSION_THREADSAFE feature flag is not set\n",
__FILE__, __LINE__);
return -1;
}

for(i = 0; i < tid_count; i++) {
results[i] = CURL_LAST;
HANDLE tid = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
if(tid == NULL) {
fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
__FILE__, __LINE__, res);
tid_count = i;
test_failure = -1;
goto cleanup;
}
tids[i] = tid;
}

cleanup:
for(i = 0; i < tid_count; i++) {
WaitForSingleObject(tids[i], INFINITE);
CloseHandle(tids[i]);
if(results[i] != CURLE_OK) {
fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
"with code %d (%s)\n", __FILE__, __LINE__,
i, (int) results[i], curl_easy_strerror(results[i]));
test_failure = -1;
}
}

return test_failure;
}

#elif defined(HAVE_PTHREAD_H)
#include <pthread.h>
#include <unistd.h>

int test(char *URL)
{
CURLcode results[NUM_THREADS];
Expand All @@ -61,6 +107,7 @@ int test(char *URL)
}

for(i = 0; i < tid_count; i++) {
results[i] = CURL_LAST;
int res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
if(res) {
fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
Expand All @@ -85,7 +132,7 @@ int test(char *URL)
return test_failure;
}

#else /* without pthread, this test doesn't work */
#else /* without pthread or Windows, this test doesn't work */
int test(char *URL)
{
curl_version_info_data *ver;
Expand Down

0 comments on commit c750e3f

Please sign in to comment.