mirror of
https://salsa.debian.org/ha-team/libqb
synced 2026-01-09 14:11:35 +00:00
test: Fix random number generation in IPC tests
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ý <jpokorny@redhat.com>
This commit is contained in:
parent
f38a877889
commit
d69cc7b3f6
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user