Skip to content

Commit

Permalink
test: Fix random number generation in IPC tests
Browse files Browse the repository at this point in the history
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ý <[email protected]>
  • Loading branch information
jnpkrn committed Apr 28, 2017
1 parent f38a877 commit d69cc7b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tests/check_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d69cc7b

Please sign in to comment.