From d69cc7b3f6fbe595af51977894d41e0980518b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= Date: Fri, 28 Apr 2017 14:31:33 +0200 Subject: [PATCH] test: Fix random number generation in IPC tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sockets are named using a random() suffix in at attempt to isolate concurrent test. However random() always returns the same random number by design ... unless pre-seeded with some value being unique enough for the particular execution. Borrowing the most of the above message from original "srandom" fix by Chrissie who also discovered this issue (nice!), I thought it would be more viable if we encoded such "unique enough" variables directly to IPC name being generated, not relying on pseudorandom generators in any way. Hence this other fix. Signed-off-by: Jan Pokorný --- tests/check_ipc.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/check_ipc.c b/tests/check_ipc.c index 920b4f1..7623acc 100644 --- a/tests/check_ipc.c +++ b/tests/check_ipc.c @@ -122,13 +122,26 @@ exit_handler(int32_t rsignal, void *data) static void set_ipc_name(const char *prefix) { - /* We have to give the server name a random postfix because - * some build systems attempt to generate packages for libqb - * in parallel. These unit tests are run during the package - * build process. Two builds executing on the same machine - * can stomp on each other's unit tests if the ipc server - * names aren't unique... This was very confusing to debug */ - snprintf(ipc_name, 256, "%s-%d", prefix, (int32_t)random()); + /* We have to make the server name as unique as possible given + * the build- (seconds part of preprocessor's timestamp) and + * run-time (pid + lower 16 bits of the current timestamp) + * circumstances, because some build systems attempt to generate + * packages for libqb in parallel. These unit tests are run + * during the package build process. 2+ builds executing on + * the same machine (whether containerized or not because of + * abstract unix sockets namespace sharing) can stomp on each + * other's unit tests if the ipc server names aren't unique... */ + + /* single-shot grab of seconds part of preprocessor's timestamp */ + static char t_sec[3] = ""; + if (t_sec[0] == '\0') { + const char const *found = strrchr(__TIME__, ':'); + strncpy(t_sec, found ? found + 1 : "-", sizeof(t_sec) - 1); + t_sec[sizeof(t_sec) - 1] = '\0'; + } + + snprintf(ipc_name, sizeof(ipc_name), "%s%s%lX%.4x", prefix, t_sec, + (long)getpid(), (int) ((long) time(NULL) % (0x10000))); } static int32_t