mirror of
https://github.com/troglobit/libite.git
synced 2026-10-24 11:44:37 +00:00
Update pidfile unit test to cover new use-case
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
729cc5307f
commit
7f3215b3e6
1 file changed
+76
-20
+76
-20
@@ -6,26 +6,30 @@
|
|||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
|
|
||||||
extern char *__progname;
|
#define PRINT(args...) if (verbose) printf(args)
|
||||||
|
|
||||||
extern char *__pidfile_name;
|
extern char *__pidfile_name;
|
||||||
extern char *__pidfile_path;
|
extern char *__pidfile_path;
|
||||||
|
|
||||||
|
static char *ident;
|
||||||
|
static char *pidfile_arg;
|
||||||
static char PIDFILE[42];
|
static char PIDFILE[42];
|
||||||
static int verbose = 0;
|
static int verbose = 0;
|
||||||
static struct stat before, after;
|
static struct stat before, after;
|
||||||
|
|
||||||
static void sigterm_handler(int UNUSED(signo))
|
static void sigterm_handler(int UNUSED(signo))
|
||||||
{
|
{
|
||||||
if (verbose)
|
PRINT("Exiting ...\n");
|
||||||
printf("Exiting ...\n");
|
|
||||||
remove(PIDFILE);
|
remove(PIDFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sigalrm_handler(int UNUSED(signo))
|
static void sigalrm_handler(int UNUSED(signo))
|
||||||
{
|
{
|
||||||
pidfile(NULL);
|
PRINT("Calling pidfile(%s)\n", pidfile_arg ?: "NULL");
|
||||||
|
if (pidfile(pidfile_arg))
|
||||||
|
err(1, "failed creating1 %s", pidfile_arg);
|
||||||
if (!fexist(PIDFILE))
|
if (!fexist(PIDFILE))
|
||||||
err(1, "failed creating %s", PIDFILE);
|
err(1, "failed creating2 %s, created %s", PIDFILE, __pidfile_name);
|
||||||
stat(PIDFILE, &before);
|
stat(PIDFILE, &before);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,31 +41,40 @@ static int mtime()
|
|||||||
/* Must sleep a while here otherwise we execute too fast => no mtime change :-( */
|
/* Must sleep a while here otherwise we execute too fast => no mtime change :-( */
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
|
|
||||||
if (verbose)
|
PRINT("Calling pidfile() again to update mtime ...\n");
|
||||||
printf("Calling pidfile() again to update mtime ...\n");
|
if (pidfile(pidfile_arg))
|
||||||
pidfile(NULL);
|
err(1, "failed creating3 %s", pidfile_arg);
|
||||||
stat(PIDFILE, &after);
|
stat(PIDFILE, &after);
|
||||||
|
|
||||||
ret = timespec_newer(&after.st_mtim, &before.st_mtim);
|
ret = timespec_newer(&after.st_mtim, &before.st_mtim);
|
||||||
if (verbose) {
|
PRINT("Before: %s\n", timespec2str(&before.st_mtim, buf, sizeof(buf)));
|
||||||
printf("Before: %s\n", timespec2str(&before.st_mtim, buf, sizeof(buf)));
|
PRINT("After : %s\n", timespec2str(&after.st_mtim, buf, sizeof(buf)));
|
||||||
printf("After : %s\n", timespec2str(&after.st_mtim, buf, sizeof(buf)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return !ret;
|
return !ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int test_pidfile(char *arg0)
|
||||||
{
|
{
|
||||||
if (argc > 1 && !strcmp(argv[1], "-v"))
|
/* pidfile() argument */
|
||||||
verbose = 1;
|
pidfile_arg = arg0;
|
||||||
|
|
||||||
/* Override default _PATH_VARRUN in pidfile() */
|
/* Expected PID file name */
|
||||||
__pidfile_path = _PATH_TMP;
|
if (!arg0)
|
||||||
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", __pidfile_path, __progname);
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", __pidfile_path, ident);
|
||||||
|
else if (arg0[0] != '/')
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", __pidfile_path, arg0);
|
||||||
|
else
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s", arg0);
|
||||||
|
|
||||||
|
PRINT("Verifying pidfile(%s) ... PID: %d\n", PIDFILE, getpid());
|
||||||
|
|
||||||
|
/* Remove any lingering files from previous test runs */
|
||||||
|
remove(PIDFILE);
|
||||||
|
|
||||||
signal(SIGTERM, sigterm_handler);
|
signal(SIGTERM, sigterm_handler);
|
||||||
signal(SIGALRM, sigalrm_handler);
|
signal(SIGALRM, sigalrm_handler);
|
||||||
|
|
||||||
|
/* For signalling test */
|
||||||
alarm(1);
|
alarm(1);
|
||||||
|
|
||||||
return test(pidfile_poll(PIDFILE) != getpid(), "Testing pidfile_poll()")
|
return test(pidfile_poll(PIDFILE) != getpid(), "Testing pidfile_poll()")
|
||||||
@@ -69,7 +82,50 @@ int main(int argc, char *argv[])
|
|||||||
|| test(mtime(), "Testing mtime update of followup pidfile() call")
|
|| test(mtime(), "Testing mtime update of followup pidfile() call")
|
||||||
|| test(pidfile_signal(PIDFILE, SIGTERM), "Testing signalling ourselves")
|
|| test(pidfile_signal(PIDFILE, SIGTERM), "Testing signalling ourselves")
|
||||||
;
|
;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *progname(char *arg0)
|
||||||
|
{
|
||||||
|
char *nm;
|
||||||
|
|
||||||
|
nm = strrchr(arg0, '/');
|
||||||
|
if (nm)
|
||||||
|
nm++;
|
||||||
|
else
|
||||||
|
nm = arg0;
|
||||||
|
|
||||||
|
return nm;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
size_t i;
|
||||||
|
char *pidfiles[] = {
|
||||||
|
NULL,
|
||||||
|
"pidfile_test1",
|
||||||
|
"/var/tmp/pidfile_test2.pid",
|
||||||
|
};
|
||||||
|
|
||||||
|
if (argc > 1 && !strcmp(argv[1], "-v"))
|
||||||
|
verbose = 1;
|
||||||
|
|
||||||
|
/* Override default _PATH_VARRUN in pidfile() */
|
||||||
|
__pidfile_path = _PATH_TMP;
|
||||||
|
|
||||||
|
/* Find __progname, not all C libraries support this */
|
||||||
|
ident = progname(argv[0]);
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(pidfiles); i++)
|
||||||
|
ret += test_pidfile(pidfiles[i]);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* compile-command: "make pidfile && ./pidfile -v"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user