From 713b80f440cb414cd0b856bde0ea1b31f939777f Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 9 Nov 2024 23:45:21 -0800 Subject: refactor initialization/finalization Applications must now call nng_init(), but they can supply a set of parameters optionally. The code is now safe for multiple libraries to do this concurrently, meaning nng_fini no longer can race against another instance starting up. The nni_init checks on all public APIs are removed now. --- tests/convey.c | 57 +++++++++------- tests/cplusplus_pair.cc | 2 + tests/httpserver.c | 176 ++++++++++++++++++++++++------------------------ tests/tcp6.c | 2 - 4 files changed, 121 insertions(+), 116 deletions(-) (limited to 'tests') diff --git a/tests/convey.c b/tests/convey.c index ebe8f4ff..9626de9b 100644 --- a/tests/convey.c +++ b/tests/convey.c @@ -59,6 +59,9 @@ #include "convey.h" +extern int nng_init(void *); +extern void nng_fini(void); + /* * About symbol naming. We use Go-like conventions to help set expectations, * even though we cannot necessarily count on the linker to prevent @@ -111,28 +114,28 @@ struct convey_timer { }; struct convey_log { - char * log_buf; + char *log_buf; size_t log_size; size_t log_length; }; struct convey_ctx { char ctx_name[256]; - struct convey_ctx * ctx_parent; - struct convey_ctx * ctx_root; /* the root node on the list */ - struct convey_ctx * ctx_next; /* root list only, cleanup */ + struct convey_ctx *ctx_parent; + struct convey_ctx *ctx_root; /* the root node on the list */ + struct convey_ctx *ctx_next; /* root list only, cleanup */ int ctx_level; int ctx_done; int ctx_started; - jmp_buf * ctx_jmp; + jmp_buf *ctx_jmp; int ctx_fatal; int ctx_fail; int ctx_skip; int ctx_printed; struct convey_timer ctx_timer; - struct convey_log * ctx_errlog; - struct convey_log * ctx_faillog; - struct convey_log * ctx_dbglog; + struct convey_log *ctx_errlog; + struct convey_log *ctx_faillog; + struct convey_log *ctx_dbglog; }; static void convey_print_result(struct convey_ctx *); @@ -150,7 +153,7 @@ static void convey_logf(struct convey_log *, const char *, ...); static void convey_log_emit(struct convey_log *, const char *, const char *); static void convey_log_free(struct convey_log *); static struct convey_log *convey_log_alloc(void); -static char * convey_nextline(char **); +static char *convey_nextline(char **); static void convey_emit_color(const char *); /* @@ -212,10 +215,10 @@ convey_print_result(struct convey_ctx *t) convey_read_timer(&t->ctx_timer, &secs, &usecs); (void) convey_logf(t->ctx_dbglog, "Test %s: %s (%d.%02ds)\n", - t->ctx_fatal ? "FATAL" - : t->ctx_fail - ? "FAIL" - : t->ctx_skip ? "PASS (with SKIPs)" : "PASS", + t->ctx_fatal ? "FATAL" + : t->ctx_fail ? "FAIL" + : t->ctx_skip ? "PASS (with SKIPs)" + : "PASS", t->ctx_name, secs, usecs / 10000); if (convey_verbose) { @@ -242,8 +245,9 @@ convey_print_result(struct convey_ctx *t) convey_emit_color(convey_nocolor); } (void) printf("\n\n--- %s: %s (%d.%02ds)\n", - t->ctx_fatal ? "FATAL" - : t->ctx_fail ? "FAIL" : "PASS", + t->ctx_fatal ? "FATAL" + : t->ctx_fail ? "FAIL" + : "PASS", t->ctx_name, secs, usecs / 10000); } @@ -687,7 +691,7 @@ convey_vlogf(struct convey_log *log, const char *fmt, va_list va, int addnl) /* Grow the log buffer if we need to */ while ((log->log_size - log->log_length) < 256) { size_t newsz = log->log_size + 2000; - char * ptr = malloc(newsz); + char *ptr = malloc(newsz); if (ptr == NULL) { return; } @@ -893,9 +897,9 @@ convey_init_term(void) // Values probably don't matter, just need to be // different! convey_nocolor = "\033[0m"; - convey_green = "\033[32m"; - convey_yellow = "\033[33m"; - convey_red = "\033[31m"; + convey_green = "\033[32m"; + convey_yellow = "\033[33m"; + convey_red = "\033[31m"; } term = getenv("TERM"); #endif @@ -950,9 +954,9 @@ convey_nextline(char **next) static struct convey_env { struct convey_env *next; - const char * name; - char * value; -} * convey_environment; + const char *name; + char *value; +} *convey_environment; static struct convey_env * conveyFindEnv(const char *name) @@ -999,11 +1003,14 @@ int conveyMain(int argc, char **argv) { int i; - const char * status; - const char * prog = ""; + const char *status; + const char *prog = ""; struct convey_timer pc; int secs, usecs; - struct convey_env * env; + struct convey_env *env; + + nng_init(NULL); + atexit(nng_fini); if ((argc > 0) && (argv[0] != NULL)) { prog = argv[0]; diff --git a/tests/cplusplus_pair.cc b/tests/cplusplus_pair.cc index 002c6f58..d011c4cd 100644 --- a/tests/cplusplus_pair.cc +++ b/tests/cplusplus_pair.cc @@ -29,6 +29,7 @@ main(int argc, char **argv) (void) argc; (void) argv; + nng_init(NULL); if ((rv = nng_pair1_open(&s1)) != 0) { throw nng_strerror(rv); } @@ -69,6 +70,7 @@ main(int argc, char **argv) } printf("Pass.\n"); + nng_fini(); #else (void) argc; (void) argv; diff --git a/tests/httpserver.c b/tests/httpserver.c index 9c798326..f572bc2c 100644 --- a/tests/httpserver.c +++ b/tests/httpserver.c @@ -35,12 +35,12 @@ httpdo(nng_url *url, nng_http_req *req, nng_http_res *res, void **datap, size_t *sizep) { int rv; - nng_aio * aio = NULL; + nng_aio *aio = NULL; nng_http_client *cli = NULL; - nng_http_conn * h = NULL; + nng_http_conn *h = NULL; size_t clen = 0; - void * data = NULL; - const char * ptr; + void *data = NULL; + const char *ptr; if (((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) || ((rv = nng_http_client_alloc(&cli, url)) != 0)) { @@ -107,11 +107,11 @@ httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp, int rv; nng_http_req *req = NULL; nng_http_res *res = NULL; - nng_url * url = NULL; + nng_url *url = NULL; size_t clen = 0; - void * data = NULL; - char * ctype = NULL; - const char * ptr; + void *data = NULL; + char *ctype = NULL; + const char *ptr; if (((rv = nng_url_parse(&url, addr)) != 0) || ((rv = nng_http_req_alloc(&req, url)) != 0) || @@ -161,7 +161,7 @@ httpecho(nng_aio *aio) nng_http_req *req = nng_aio_get_input(aio, 0); nng_http_res *res; int rv; - void * body; + void *body; size_t len; nng_http_req_get_data(req, &body, &len); @@ -180,11 +180,9 @@ httpecho(nng_aio *aio) } TestMain("HTTP Server", { - nng_http_server * s; + nng_http_server *s; nng_http_handler *h; - nni_init(); - Convey("We can start an HTTP server", { nng_aio *aio; char portbuf[16]; @@ -214,9 +212,9 @@ TestMain("HTTP Server", { Convey("We can connect a client to it", { nng_http_client *cli; - nng_http_conn * h; - nng_http_req * req; - nng_http_res * res; + nng_http_conn *h; + nng_http_req *req; + nng_http_res *res; So(nng_http_client_alloc(&cli, url) == 0); nng_http_client_connect(cli, aio); @@ -287,13 +285,13 @@ TestMain("HTTP Server", { Convey("Directory serving works (root)", { char urlstr[32]; nng_url *url; - char * tmpdir; - char * workdir; - char * file1; - char * file2; - char * file3; - char * subdir1; - char * subdir2; + char *tmpdir; + char *workdir; + char *file1; + char *file2; + char *file3; + char *subdir1; + char *subdir2; trantest_next_address(urlstr, "http://127.0.0.1:"); So(nng_url_parse(&url, urlstr) == 0); @@ -335,10 +333,10 @@ TestMain("HTTP Server", { Convey("Index.html works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/subdir1/index.html", urlstr); @@ -353,10 +351,10 @@ TestMain("HTTP Server", { Convey("Index.htm works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf( fullurl, sizeof(fullurl), "%s/subdir2", urlstr); @@ -371,10 +369,10 @@ TestMain("HTTP Server", { Convey("Named file works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf( fullurl, sizeof(fullurl), "%s/file.txt", urlstr); @@ -389,13 +387,13 @@ TestMain("HTTP Server", { Convey("Named file with URI parameters works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; - snprintf( - fullurl, sizeof(fullurl), "%s/file.txt?param=123456", urlstr); + snprintf(fullurl, sizeof(fullurl), + "%s/file.txt?param=123456", urlstr); So(httpget(fullurl, &data, &size, &stat, &ctype) == 0); So(stat == NNG_HTTP_STATUS_OK); So(size == strlen(doc2)); @@ -407,10 +405,10 @@ TestMain("HTTP Server", { Convey("Missing index gives 404", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/", urlstr); So(httpget(fullurl, &data, &size, &stat, &ctype) == 0); @@ -421,10 +419,10 @@ TestMain("HTTP Server", { Convey("Custom error page works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; So(nng_http_server_set_error_page(s, 404, doc4) == 0); snprintf(fullurl, sizeof(fullurl), "%s/", urlstr); @@ -438,11 +436,11 @@ TestMain("HTTP Server", { Convey("Bad method gives 405", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/", urlstr); @@ -461,11 +459,11 @@ TestMain("HTTP Server", { Convey("Version 0.9 gives 505", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/", urlstr); @@ -484,11 +482,11 @@ TestMain("HTTP Server", { Convey("Missing Host gives 400", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/", urlstr); @@ -509,13 +507,13 @@ TestMain("HTTP Server", { Convey("Directory serving works", { char urlstr[32]; nng_url *url; - char * tmpdir; - char * workdir; - char * file1; - char * file2; - char * file3; - char * subdir1; - char * subdir2; + char *tmpdir; + char *workdir; + char *file1; + char *file2; + char *file3; + char *subdir1; + char *subdir2; trantest_next_address(urlstr, "http://127.0.0.1:"); So(nng_url_parse(&url, urlstr) == 0); @@ -558,10 +556,10 @@ TestMain("HTTP Server", { Convey("Index.html works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/docs/subdir1/index.html", urlstr); @@ -576,10 +574,10 @@ TestMain("HTTP Server", { Convey("Index.htm works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/docs/subdir2", urlstr); @@ -594,10 +592,10 @@ TestMain("HTTP Server", { Convey("Named file works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/docs/file.txt", urlstr); @@ -612,10 +610,10 @@ TestMain("HTTP Server", { Convey("Missing index gives 404", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr); So(httpget(fullurl, &data, &size, &stat, &ctype) == 0); @@ -626,10 +624,10 @@ TestMain("HTTP Server", { Convey("Custom error page works", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; So(nng_http_server_set_error_page(s, 404, doc4) == 0); snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr); @@ -643,11 +641,11 @@ TestMain("HTTP Server", { Convey("Bad method gives 405", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr); @@ -666,11 +664,11 @@ TestMain("HTTP Server", { Convey("Version 0.9 gives 505", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr); @@ -689,11 +687,11 @@ TestMain("HTTP Server", { Convey("Missing Host gives 400", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr); @@ -714,11 +712,11 @@ TestMain("HTTP Server", { Convey("Multiple tree handlers works", { char urlstr[32]; nng_url *url; - char * tmpdir; - char * workdir; - char * workdir2; - char * file1; - char * file2; + char *tmpdir; + char *workdir; + char *workdir2; + char *file1; + char *file2; trantest_next_address(urlstr, "http://127.0.0.1:"); So(nng_url_parse(&url, urlstr) == 0); @@ -771,10 +769,10 @@ TestMain("HTTP Server", { Convey("Named file works (1)", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf( fullurl, sizeof(fullurl), "%s/file1.txt", urlstr); @@ -789,10 +787,10 @@ TestMain("HTTP Server", { Convey("Named file works (2)", { char fullurl[256]; - void * data; + void *data; size_t size; uint16_t stat; - char * ctype; + char *ctype; snprintf(fullurl, sizeof(fullurl), "%s/subdir/file2.txt", urlstr); @@ -831,9 +829,9 @@ TestMain("HTTP Server", { size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; char txdata[5]; - char * rxdata; + char *rxdata; snprintf(txdata, sizeof(txdata), "1234"); So(nng_http_res_alloc(&res) == 0); @@ -855,11 +853,11 @@ TestMain("HTTP Server", { Convey("Get method gives 405", { char fullurl[256]; - void * data; + void *data; size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; So(nng_http_res_alloc(&res) == 0); snprintf(fullurl, sizeof(fullurl), "%s/post", urlstr); @@ -894,9 +892,9 @@ TestMain("HTTP Server", { char fullurl[256]; nng_http_req *req; nng_http_res *res; - nng_url * curl; - const char * dest; - void * data; + nng_url *curl; + const char *dest; + void *data; size_t size; So(nng_http_handler_alloc_redirect(&h, "/here", 301, @@ -928,9 +926,9 @@ TestMain("HTTP Server", { char fullurl[256]; nng_http_req *req; nng_http_res *res; - nng_url * curl; - const char * dest; - void * data; + nng_url *curl; + const char *dest; + void *data; size_t size; // We'll use a 303 to ensure codes carry thru @@ -965,10 +963,10 @@ TestMain("HTTP Server", { size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; char txdata[5]; - const char * dest; - void * data; + const char *dest; + void *data; So(nng_http_handler_alloc_redirect(&h, "/here", 301, "http://127.0.0.1/there") == 0); @@ -1020,9 +1018,9 @@ TestMain("HTTP Server", { size_t size; nng_http_req *req; nng_http_res *res; - nng_url * curl; + nng_url *curl; char txdata[5]; - char * rxdata; + char *rxdata; snprintf(txdata, sizeof(txdata), "1234"); So(nng_http_res_alloc(&res) == 0); diff --git a/tests/tcp6.c b/tests/tcp6.c index 79e6fa47..f6fd5a7c 100644 --- a/tests/tcp6.c +++ b/tests/tcp6.c @@ -70,8 +70,6 @@ check_props_v6(nng_msg *msg) } TestMain("TCP (IPv6) Transport", { - nni_init(); - if (has_v6()) { trantest_test_extended("tcp://[::1]:", check_props_v6); } else { -- cgit v1.2.3-70-g09d2