Time handling: improve time handling in unittests

- make sure before each unittest is run the time is reset
- add functions to set the time to current time and increment the time
- convert alert-unified* Rotate tests to use them
- convert time based counters to use them
- use GetTime instead of gettimeofday for creating the unified* filenames
remotes/origin/master-1.0.x
Victor Julien 16 years ago
parent a64af4adca
commit 5c3bbb8d61

@ -23,6 +23,7 @@
#include "tm-modules.h"
#include "util-unittest.h"
#include "util-time.h"
#define DEFAULT_LOG_FILENAME "unified.alert"
@ -290,13 +291,10 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
* Load the default configuration.
*/
/* get the time so we can have a filename with seconds since epoch
* in it. XXX review if we can take this info from somewhere else.
* This is used both during init and runtime, so it must be thread
* safe. */
/* get the time so we can have a filename with seconds since epoch */
struct timeval ts;
memset (&ts, 0, sizeof(struct timeval));
gettimeofday(&ts, NULL);
TimeGet(&ts);
/* create the filename to use */
char *log_dir;
@ -346,7 +344,8 @@ static int AlertUnifiedAlertTestRotate01(void)
return 0;
}
sleep(1);
TimeSetIncrementTime(1);
ret = AlertUnifiedAlertRotateFile(&tv, data);
if (ret == -1)
goto error;

@ -23,6 +23,7 @@
#include "tm-modules.h"
#include "util-unittest.h"
#include "util-time.h"
#define DEFAULT_LOG_FILENAME "unified.log"
@ -308,13 +309,10 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
* Load the default configuration.
*/
/* get the time so we can have a filename with seconds since epoch
* in it. XXX review if we can take this info from somewhere else.
* This is used both during init and runtime, so it must be thread
* safe. */
/* get the time so we can have a filename with seconds since epoch */
struct timeval ts;
memset (&ts, 0, sizeof(struct timeval));
gettimeofday(&ts, NULL);
TimeGet(&ts);
/* create the filename to use */
char *log_dir;
@ -364,7 +362,8 @@ static int AlertUnifiedLogTestRotate01(void)
return 0;
}
sleep(1);
TimeSetIncrementTime(1);
ret = AlertUnifiedLogRotateFile(&tv, data);
if (ret == -1)
goto error;

@ -19,6 +19,7 @@
#include "decode-ipv4.h"
#include "util-debug.h"
#include "util-time.h"
/*prototypes*/
TmEcode Unified2Alert (ThreadVars *, Packet *, void *, PacketQueue *);
@ -574,13 +575,10 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
* Load the default configuration.
*/
/** get the time so we can have a filename with seconds since epoch
* in it. XXX review if we can take this info from somewhere else.
* This is used both during init and runtime, so it must be thread
* safe. */
/** get the time so we can have a filename with seconds since epoch */
struct timeval ts;
memset(&ts, 0, sizeof(struct timeval));
gettimeofday(&ts, NULL);
memset(&ts, 0x00, sizeof(struct timeval));
TimeGet(&ts);
/* create the filename to use */
char *log_dir;
@ -959,13 +957,16 @@ static int Unified2TestRotate01(void)
return 0;
}
sleep(1);
TimeSetIncrementTime(1);
ret = Unified2AlertRotateFile(&tv, data);
if (ret == -1)
goto error;
if (strcmp(filename, lf->filename) == 0)
if (strcmp(filename, lf->filename) == 0) {
printf("filename \"%s\" == \"%s\": ", filename, lf->filename);
goto error;
}
r = 1;

@ -2079,12 +2079,6 @@ static int SCPerfTestIntervalQual16()
memset(&tv, 0, sizeof(ThreadVars));
TimeModeSetOffline();
struct timeval timev;
memset(&timev, 0x00, sizeof(timev));
gettimeofday(&timev, NULL);
TimeSet(&timev);
id1 = SCPerfRegisterIntervalCounter("t1", "c1", SC_PERF_TYPE_DOUBLE, NULL,
&tv.sc_perf_pctx, "3s");
@ -2100,10 +2094,7 @@ static int SCPerfTestIntervalQual16()
SCPerfUpdateCounterArray(pca, &tv.sc_perf_pctx, 0);
/* forward the time 6 seconds */
memset(&timev, 0x00, sizeof(timev));
TimeGet(&timev);
timev.tv_sec += 6;
TimeSet(&timev);
TimeSetIncrementTime(6);
SCPerfOutputCalculateCounterValue(tv.sc_perf_pctx.head, &d_temp);
@ -2122,12 +2113,6 @@ static int SCPerfTestIntervalQual17()
memset(&tv, 0, sizeof(ThreadVars));
TimeModeSetOffline();
struct timeval timev;
memset(&timev, 0x00, sizeof(timev));
gettimeofday(&timev, NULL);
TimeSet(&timev);
id1 = SCPerfRegisterIntervalCounter("t1", "c1", SC_PERF_TYPE_DOUBLE, NULL,
&tv.sc_perf_pctx, "2m30s");
@ -2143,10 +2128,7 @@ static int SCPerfTestIntervalQual17()
SCPerfUpdateCounterArray(pca, &tv.sc_perf_pctx, 0);
/* forward the time 3 seconds */
memset(&timev, 0x00, sizeof(timev));
TimeGet(&timev);
timev.tv_sec += 3;
TimeSet(&timev);
TimeSetIncrementTime(3);
SCPerfOutputCalculateCounterValue(tv.sc_perf_pctx.head, &d_temp);

@ -39,6 +39,16 @@ void TimeSet(struct timeval *tv)
mutex_unlock(&current_time_mutex);
}
/** \brief set the time to "gettimeofday" meant for testing */
void TimeSetToCurrentTime(void) {
struct timeval tv;
memset(&tv, 0x00, sizeof(tv));
gettimeofday(&tv, NULL);
TimeSet(&tv);
}
void TimeGet(struct timeval *tv)
{
if (tv == NULL)
@ -56,3 +66,16 @@ void TimeGet(struct timeval *tv)
SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec",
(uintmax_t)tv->tv_sec, (uintmax_t)tv->tv_usec);
}
/** \brief increment the time in the engine
* \param tv_sec seconds to increment the time with */
void TimeSetIncrementTime(uint32_t tv_sec) {
struct timeval tv;
memset(&tv, 0x00, sizeof(tv));
TimeGet(&tv);
tv.tv_sec += tv_sec;
TimeSet(&tv);
}

@ -3,6 +3,10 @@
void TimeSet(struct timeval *);
void TimeGet(struct timeval *);
void TimeSetToCurrentTime(void);
void TimeSetIncrementTime(uint32_t);
void TimeModeSetLive(void);
void TimeModeSetOffline (void);

@ -8,6 +8,7 @@
#include "eidps-common.h"
#include "util-unittest.h"
#include "util-debug.h"
#include "util-time.h"
static pcre *parse_regex;
static pcre_extra *parse_regex_study;
@ -166,6 +167,11 @@ uint32_t UtRunTests(char *regex_arg) {
printf("Test %-60.60s : ", ut->name);
matchcnt++;
fflush(stdout); /* flush so in case of a segv we see the testname */
/* reset the time */
TimeModeSetOffline();
TimeSetToCurrentTime();
ret = ut->TestFn();
printf("%s\n", (ret == ut->evalue) ? "pass" : "FAILED");
if (ret != ut->evalue) {

Loading…
Cancel
Save