issue/863/CT300x: add timestamp and check

This commit is contained in:
Tomoki Shirasawa
2018-03-23 13:28:19 +09:00
parent 4c80dca479
commit 535e3f3af6
4 changed files with 113 additions and 11 deletions

View File

@@ -7,10 +7,37 @@
#include <time.h>
#include <sys/time.h>
char *
gettime(char *buf, struct timeval *tv)
{
struct tm *tm;
gettimeofday(tv, NULL);
tm = localtime(&tv->tv_sec);
sprintf(buf, "%02d:%02d:%02d.%06d", tm->tm_hour, tm->tm_min, tm->tm_sec, tv->tv_usec);
return buf;
}
void
tv_sub(struct timeval *tv1, const struct timeval *tv2)
{
tv1->tv_sec -= tv2->tv_sec;
tv1->tv_usec -= tv2->tv_usec;
if (tv1->tv_usec < 0) {
tv1->tv_sec--;
tv1->tv_usec += 1000000;
}
}
struct timeval tv1;
struct timeval tv2;
void
sig(int s)
{
fprintf(stderr, "signal hanlder is called\n");
char buf[16];
fprintf(stderr, "%s signal hanlder is called\n", gettime(buf, &tv2));
}
int
@@ -20,10 +47,12 @@ main(int argc, char **argv)
int fds[2];
char c;
int rc;
char buf[16];
memset(&act, '\0', sizeof act);
act.sa_handler = sig;
sigaction(SIGALRM, &act, NULL);
fprintf(stderr, "%s test start, kill after 3 seconds\n", gettime(buf, &tv1));
alarm(3);
pipe(fds);
rc = read(fds[0], &c, 1);
@@ -35,6 +64,10 @@ main(int argc, char **argv)
fprintf(stderr, "CT3001 NG BAD error errno=%d\n", errno);
exit(1);
}
fprintf(stderr, "CT3001 OK\n");
tv_sub(&tv2, &tv1);
if (tv2.tv_sec != 3)
fprintf(stderr, "CT3001 NG signal delayed (%d.%06d)\n", tv2.tv_sec, tv2.tv_usec);
else
fprintf(stderr, "CT3001 OK\n");
exit(0);
}