From f007ad4dcfee0037cd86bf31804795e5f60cb2d9 Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Wed, 28 Oct 2020 00:01:46 +0300 Subject: Added threading to the libunit test app. --- src/test/nxt_unit_app_test.c | 130 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 108 insertions(+), 22 deletions(-) (limited to 'src/test/nxt_unit_app_test.c') diff --git a/src/test/nxt_unit_app_test.c b/src/test/nxt_unit_app_test.c index 4ecb8d6b..5b2521e8 100644 --- a/src/test/nxt_unit_app_test.c +++ b/src/test/nxt_unit_app_test.c @@ -3,9 +3,14 @@ * Copyright (C) NGINX, Inc. */ +#include +#include #include #include #include +#include +#include +#include #define CONTENT_TYPE "Content-Type" @@ -28,12 +33,106 @@ #define BODY " Body:\n" -static inline char * -copy(char *p, const void *src, uint32_t len) +static int ready_handler(nxt_unit_ctx_t *ctx); +static void *worker(void *main_ctx); +static void greeting_app_request_handler(nxt_unit_request_info_t *req); +static inline char *copy(char *p, const void *src, uint32_t len); + + +static int thread_count; +static pthread_t *threads; + + +int +main(int argc, char **argv) { - memcpy(p, src, len); + int i, err; + nxt_unit_ctx_t *ctx; + nxt_unit_init_t init; - return p + len; + if (argc == 3 && strcmp(argv[1], "-t") == 0) { + thread_count = atoi(argv[2]); + } + + memset(&init, 0, sizeof(nxt_unit_init_t)); + + init.callbacks.request_handler = greeting_app_request_handler; + init.callbacks.ready_handler = ready_handler; + + ctx = nxt_unit_init(&init); + if (ctx == NULL) { + return 1; + } + + err = nxt_unit_run(ctx); + + nxt_unit_debug(ctx, "main worker finished with %d code", err); + + if (thread_count > 1) { + for (i = 0; i < thread_count - 1; i++) { + err = pthread_join(threads[i], NULL); + + nxt_unit_debug(ctx, "join thread #%d: %d", i, err); + } + + nxt_unit_free(ctx, threads); + } + + nxt_unit_done(ctx); + + nxt_unit_debug(NULL, "main worker done"); + + return 0; +} + + +static int +ready_handler(nxt_unit_ctx_t *ctx) +{ + int i, err; + + nxt_unit_debug(ctx, "ready"); + + if (!nxt_unit_is_main_ctx(ctx) || thread_count <= 1) { + return NXT_UNIT_OK; + } + + threads = nxt_unit_malloc(ctx, sizeof(pthread_t) * (thread_count - 1)); + if (threads == NULL) { + return NXT_UNIT_ERROR; + } + + for (i = 0; i < thread_count - 1; i++) { + err = pthread_create(&threads[i], NULL, worker, ctx); + if (err != 0) { + return NXT_UNIT_ERROR; + } + } + + return NXT_UNIT_OK; +} + + +static void * +worker(void *main_ctx) +{ + int rc; + nxt_unit_ctx_t *ctx; + + ctx = nxt_unit_ctx_alloc(main_ctx, NULL); + if (ctx == NULL) { + return NULL; + } + + nxt_unit_debug(ctx, "start worker"); + + rc = nxt_unit_run(ctx); + + nxt_unit_debug(ctx, "worker finished with %d code", rc); + + nxt_unit_done(ctx); + + return NULL; } @@ -168,24 +267,11 @@ fail: nxt_unit_request_done(req, rc); } -int -main() -{ - nxt_unit_ctx_t *ctx; - nxt_unit_init_t init; - - memset(&init, 0, sizeof(nxt_unit_init_t)); - - init.callbacks.request_handler = greeting_app_request_handler; - ctx = nxt_unit_init(&init); - if (ctx == NULL) { - return 1; - } - - nxt_unit_run(ctx); - - nxt_unit_done(ctx); +static inline char * +copy(char *p, const void *src, uint32_t len) +{ + memcpy(p, src, len); - return 0; + return p + len; } -- cgit From d03b217f33db21d9af28d58d92ba02c2a2e48f5e Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Sun, 1 Nov 2020 13:22:11 +0300 Subject: Fixed building test app without debug. Compilers complained about unused variables after 37e2a3ea1bf1. --- src/test/nxt_unit_app_test.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/test/nxt_unit_app_test.c') diff --git a/src/test/nxt_unit_app_test.c b/src/test/nxt_unit_app_test.c index 5b2521e8..b6dd13d2 100644 --- a/src/test/nxt_unit_app_test.c +++ b/src/test/nxt_unit_app_test.c @@ -72,7 +72,13 @@ main(int argc, char **argv) for (i = 0; i < thread_count - 1; i++) { err = pthread_join(threads[i], NULL); - nxt_unit_debug(ctx, "join thread #%d: %d", i, err); + if (nxt_fast_path(err == 0)) { + nxt_unit_debug(ctx, "join thread #%d", i); + + } else { + nxt_unit_alert(ctx, "pthread_join(#%d) failed: %s (%d)", + i, strerror(err), err); + } } nxt_unit_free(ctx, threads); @@ -132,7 +138,7 @@ worker(void *main_ctx) nxt_unit_done(ctx); - return NULL; + return (void *) (intptr_t) rc; } -- cgit From 8340ca0b9c7ad4109033ccb028f87cc1b73396bc Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Wed, 18 Nov 2020 22:33:53 +0300 Subject: Libunit: improving logging consistency. Debug logging depends on macros defined in nxt_auto_config.h. --- src/test/nxt_unit_app_test.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/test/nxt_unit_app_test.c') diff --git a/src/test/nxt_unit_app_test.c b/src/test/nxt_unit_app_test.c index b6dd13d2..a5f3728c 100644 --- a/src/test/nxt_unit_app_test.c +++ b/src/test/nxt_unit_app_test.c @@ -3,8 +3,6 @@ * Copyright (C) NGINX, Inc. */ -#include -#include #include #include #include -- cgit