diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-12-07 15:45:15 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-12-07 15:45:15 -0800 |
| commit | 45ad63d265261203155c882ec93b80a604e9ab3b (patch) | |
| tree | eb6fda615bb65e888a0bada3ad1730daabb72129 /src/testing/marry.c | |
| parent | 32c78f7b7b4fcfacea6b45e8601c82771f1e40b3 (diff) | |
| download | nng-45ad63d265261203155c882ec93b80a604e9ab3b.tar.gz nng-45ad63d265261203155c882ec93b80a604e9ab3b.tar.bz2 nng-45ad63d265261203155c882ec93b80a604e9ab3b.zip | |
nuts: try to avoid address in use for most tests
We get test failures somewhat frequently due to port conflicts.
This attempts to make more of the tests use the trick of binding
to port 0, and letting us use the random port instead.
Diffstat (limited to 'src/testing/marry.c')
| -rw-r--r-- | src/testing/marry.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/testing/marry.c b/src/testing/marry.c index 8cc94473..36b879c7 100644 --- a/src/testing/marry.c +++ b/src/testing/marry.c @@ -92,6 +92,62 @@ nuts_scratch_addr(const char *scheme, size_t sz, char *addr) abort(); } +void +nuts_scratch_addr_zero(const char *scheme, size_t sz, char *addr) +{ + if ((strcmp(scheme, "inproc") == 0) || + (strcmp(scheme, "abstract") == 0)) { + (void) snprintf(addr, sz, "%s://nuts%04x%04x%04x%04x", scheme, + nng_random(), nng_random(), nng_random(), nng_random()); + return; + } + + if ((strncmp(scheme, "tcp", 3) == 0) || + (strncmp(scheme, "tls", 3) == 0) || + (strncmp(scheme, "udp", 3) == 0)) { + const char *ip = + strchr(scheme, '6') != NULL ? "[::1]" : "127.0.0.1"; + (void) snprintf(addr, sz, "%s://%s:%u", scheme, ip, 0); + return; + } + + if (strncmp(scheme, "ws", 2) == 0) { + const char *ip = + strchr(scheme, '6') != NULL ? "[::1]" : "127.0.0.1"; + (void) snprintf(addr, sz, "%s://%s:%u/nuts%04x%04x%04x%04x", + scheme, ip, 0, nng_random(), nng_random(), nng_random(), + nng_random()); + return; + } + + if ((strncmp(scheme, "ipc", 3) == 0) || + (strncmp(scheme, "unix", 4) == 0)) { +#ifdef _WIN32 + // Windows doesn't place IPC names in the filesystem. + (void) snprintf(addr, sz, "%s://nuts%04x%04x%04x%04x", scheme, + nng_random(), nng_random(), nng_random(), nng_random()); + return; +#else + char *tmpdir; + + if (((tmpdir = getenv("TMPDIR")) == NULL) && + ((tmpdir = getenv("TEMP")) == NULL) && + ((tmpdir = getenv("TMP")) == NULL)) { + tmpdir = "/tmp"; + } + + (void) snprintf(addr, sz, "%s://%s/nuts%04x%04x%04x%04x", + scheme, tmpdir, nng_random(), nng_random(), nng_random(), + nng_random()); + return; +#endif + } + + // We should not be here. + nng_log_err("NUTS", "Unknown scheme"); + abort(); +} + // nuts_next_port returns a "next" allocation port. // Ports are chosen by starting from a random point within a // range (normally 38000-40000, but other good places to choose |
