add first eventhandler tests

Fri, 16 Aug 2024 16:59:05 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 Aug 2024 16:59:05 +0200
changeset 551
97039494764b
parent 550
77241b3ba544
child 552
4ed0e46aa9dc

add first eventhandler tests

src/server/safs/common.c file | annotate | diff | comparison | revisions
src/server/test/event.c file | annotate | diff | comparison | revisions
src/server/test/event.h file | annotate | diff | comparison | revisions
src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/test/objs.mk file | annotate | diff | comparison | revisions
--- a/src/server/safs/common.c	Thu Aug 15 22:42:35 2024 +0200
+++ b/src/server/safs/common.c	Fri Aug 16 16:59:05 2024 +0200
@@ -165,19 +165,9 @@
         case COMMONSAF_ABORT: return COMMONSAF_REQ_ABORTED;
         case COMMONSAF_NOACTION: return COMMONSAF_RET_NOACTION;
         case COMMONSAF_ERROR: {
-            int len = strlen(value);
-            WSBool isnum = TRUE;
-            int i;
-            for(i=0;i<len;i++) {
-                if(!isdigit(value[i])) {
-                    isnum = FALSE;
-                    break;
-                }
-            }
-            
-            int64_t status;
-            int ret = util_strtoint(value, &status);
-            if(status < 100 || ret > 999 || !ret) {
+            char *end;
+            long status = strtol(value, &end, 10);
+            if(status < 100 || status > 999) {
                 log_ereport(
                         LOG_MISCONFIG,
                         "set-variable: error value must contain a 3-digit http status code");
@@ -185,7 +175,14 @@
                 return COMMONSAF_RET_ERROR;
             }
             
-            const char *msg = isnum ? NULL : cx_strtrim(cx_str(value + i)).ptr;
+            const char *msg = NULL;
+            while(isspace(*end)) {
+                end++;
+            }
+            if(*end != '\0') {
+                msg = end;
+            }
+            
             protocol_status(sn, rq, (int)status, msg);
             
             return COMMONSAF_REQ_ABORTED;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/test/event.c	Fri Aug 16 16:59:05 2024 +0200
@@ -0,0 +1,120 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "event.h"
+
+#include "../daemon/event.h"
+
+typedef struct EVTest {
+    EventHandler *h;
+    pthread_mutex_t mutex;
+    pthread_cond_t cond;
+    void *data1;
+    void *data2;
+    int i1;
+    int i2;
+} EVTest;
+
+UCX_TEST(test_evhandler_create) {
+    EventHandlerConfig cfg1 = { .nthreads = 1};
+    
+    EventHandlerConfig cfg4 = { .nthreads = 4};
+    
+    UCX_TEST_BEGIN;
+    
+    EVHandler *ev1 = evhandler_create(&cfg1);
+    UCX_TEST_ASSERT(ev1, "evhandler_create (1) failed");
+    UCX_TEST_ASSERT(ev1->numins == 1, "ev1 wrong number of instances");
+    
+    EVHandler *ev2 = evhandler_create(&cfg4);
+    UCX_TEST_ASSERT(ev2, "evhandler_create (2) failed");
+    UCX_TEST_ASSERT(ev2->numins == 4, "ev2 wrong number of instances");
+    
+    evhandler_close(ev1);
+    evhandler_close(ev2);
+    
+    UCX_TEST_END;
+}
+
+static int test_event_send_fn(EventHandler *h, Event *event) {
+    EVTest *test = event->cookie;
+    test->i1 = 1;
+    test->i2 = h == test->h;
+    
+    pthread_mutex_lock(&test->mutex);
+    pthread_cond_signal(&test->cond);
+    pthread_mutex_unlock(&test->mutex);
+    
+    return 0;
+}
+
+UCX_TEST(test_event_send) {
+    EventHandlerConfig cfg = { .nthreads = 1};
+    
+    EVHandler *ev = evhandler_create(&cfg);
+    EventHandler *h = ev_instance(ev);
+    
+    EVTest testdata;
+    ZERO(&testdata, sizeof(EVTest));
+    testdata.h = h;
+    pthread_mutex_init(&testdata.mutex, NULL);
+    pthread_cond_init(&testdata.cond, NULL);
+    
+    UCX_TEST_BEGIN;
+    
+    Event evt;
+    ZERO(&evt, sizeof(Event));
+    evt.fn = test_event_send_fn;
+    evt.cookie = &testdata;
+    
+    int ret = event_send(h, &evt);
+    
+    // wait for event finish
+    time_t tstart = time(NULL);
+    while(!testdata.i1) {
+        time_t t = time(NULL);
+        if(t - tstart > 10) {
+            break;
+        }
+        
+        pthread_mutex_lock(&testdata.mutex);
+        pthread_cond_wait(&testdata.cond, &testdata.mutex);
+        pthread_mutex_unlock(&testdata.mutex);
+    }
+    
+    UCX_TEST_ASSERT(!ret, "event_send failed");
+    UCX_TEST_ASSERT(testdata.i1, "event callback not called");
+    UCX_TEST_ASSERT(testdata.i2, "event callback received wrong event handler pointer");
+    
+    UCX_TEST_END;
+    
+    pthread_mutex_destroy(&testdata.mutex);
+    pthread_cond_destroy(&testdata.cond);
+    
+    evhandler_close(ev);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/test/event.h	Fri Aug 16 16:59:05 2024 +0200
@@ -0,0 +1,51 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TEST_EVENT_H
+#define TEST_EVENT_H
+
+#include "../public/nsapi.h"
+
+#include "test.h"
+
+#include "../daemon/event.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+UCX_TEST(test_event_send);
+UCX_TEST(test_evhandler_create);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* EVENT_H */
+
--- a/src/server/test/main.c	Thu Aug 15 22:42:35 2024 +0200
+++ b/src/server/test/main.c	Fri Aug 16 16:59:05 2024 +0200
@@ -49,6 +49,7 @@
 #include "uri.h"
 #include "object.h"
 #include "io.h"
+#include "event.h"
 
 void register_pg_tests(int argc, char **argv, UcxTestSuite *suite);
 
@@ -79,6 +80,10 @@
     ucx_test_register(suite, test_util_uri_escape_latin);
     ucx_test_register(suite, test_util_uri_escape_kanji);
     
+    // event tests
+    ucx_test_register(suite, test_evhandler_create);
+    ucx_test_register(suite, test_event_send);
+    
     // object tests
     ucx_test_register(suite, test_expr_parse_expr_value);
     ucx_test_register(suite, test_expr_parse_expr_neg_value);
--- a/src/server/test/objs.mk	Thu Aug 15 22:42:35 2024 +0200
+++ b/src/server/test/objs.mk	Fri Aug 16 16:59:05 2024 +0200
@@ -40,6 +40,7 @@
 TESTOBJ += uri.o
 TESTOBJ += object.o
 TESTOBJ += io.o
+TESTOBJ += event.o
 
 TESTOBJS = $(TESTOBJ:%=$(TEST_OBJPRE)%)
 TESTSOURCE = $(TESTOBJ:%.o=test/%.c)

mercurial