src/server/test/httpclient.c

changeset 704
778dcf4ad63c
parent 703
395c62fac7e5
child 705
30de3bfd0412
--- a/src/server/test/httpclient.c	Tue Feb 24 21:28:06 2026 +0100
+++ b/src/server/test/httpclient.c	Wed Feb 25 22:16:20 2026 +0100
@@ -43,6 +43,21 @@
 static pthread_cond_t test_cond;
 static volatile int test_finished;
 
+void http_client_tests_init(void) {
+    pthread_mutex_init(&test_mutex, NULL);
+    pthread_cond_init(&test_cond, NULL);
+    
+    EventHandlerConfig config;
+    config.isdefault = 0;
+    config.name = cx_str(NULL);
+    config.nthreads = 1;
+    test_eventhandler = evhandler_create(&config);
+}
+
+void http_client_tests_cleanup(void) {
+    ev_instance_shutdown(test_eventhandler->instances[0]);
+}
+
 static void test_response_finished(HttpClient *client, void *userdata) {
     pthread_mutex_lock(&test_mutex);
     pthread_cond_signal(&test_cond);
@@ -53,21 +68,10 @@
 
 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
     char *str = buf;
-    return cxBufferWrite(buf, 1, nbytes, userdata);
+    return cxBufferWrite(str, 1, nbytes, userdata);
 }
 
 CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) {
-    pthread_mutex_init(&test_mutex, NULL);
-    pthread_cond_init(&test_cond, NULL);
-    
-    if(!test_eventhandler) {
-        EventHandlerConfig config;
-        config.isdefault = 0;
-        config.name = cx_str(NULL);
-        config.nthreads = 1;
-        test_eventhandler = evhandler_create(&config);
-    }
-    
     HttpClient *client = http_client_new(test_eventhandler->instances[0]);
     client->response_body_write = test_response_body_write;
     client->response_body_write_userdata = out;
@@ -80,7 +84,7 @@
     http_client_set_socket(client, fds[0]);
     http_client_set_method(client, "GET");
     http_client_set_uri(client, "/testx01");
-    
+     
     test_finished = 0;
     int ret = http_client_start(client);
     CX_TEST_ASSERT(ret == 0);
@@ -97,13 +101,13 @@
         }
     }
     
-    close(fds[1]);
-    
     pthread_mutex_lock(&test_mutex);
     if(test_finished == 0) {
         pthread_cond_wait(&test_cond, &test_mutex);
     }
     pthread_mutex_unlock(&test_mutex); 
+    
+    close(fds[1]);
 }
 
 CX_TEST(test_http_client_simple_get1) {

mercurial