aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt3
-rw-r--r--tests/include/testing.h9
-rw-r--r--tests/test_multiprocessing.c44
3 files changed, 55 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 2b09e9e..08ef833 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -30,6 +30,9 @@ foreach(source_file ${source_files})
elseif (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
target_compile_options(${test_executable} PRIVATE ${win_cflags} ${win_msvc_cflags})
endif()
+ if (TESTS_VERBOSE)
+ target_compile_definitions(${test_executable} PRIVATE STASIS_TEST_VERBOSE=1)
+ endif ()
target_include_directories(${test_executable} PRIVATE
${core_INCLUDE}
${delivery_INCLUDE}
diff --git a/tests/include/testing.h b/tests/include/testing.h
index ab24115..6fa5ca6 100644
--- a/tests/include/testing.h
+++ b/tests/include/testing.h
@@ -9,6 +9,15 @@
#define __FILE_NAME__ __FILE__
#endif
+#ifdef STASIS_TEST_VERBOSE
+#define STASIS_TEST_MSG(MSG, ...) do { \
+fprintf(stderr, "%s:%d:%s(): ", path_basename(__FILE__), __LINE__, __FUNCTION__); \
+fprintf(stderr, MSG LINE_SEP, __VA_ARGS__); \
+} while (0)
+#else
+#define STASIS_TEST_MSG(MSG, ...) do {} while (0)
+#endif
+
typedef void(STASIS_TEST_FUNC)();
struct stasis_test_result_t {
const char *filename;
diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c
index 7c9d695..3a462f1 100644
--- a/tests/test_multiprocessing.c
+++ b/tests/test_multiprocessing.c
@@ -161,7 +161,7 @@ void test_mp_fail_fast() {
if (task->status == 0) result.total_status_success++;
if (task->pid == MP_POOL_PID_UNUSED && task->status == MP_POOL_TASK_STATUS_INITIAL) result.total_unused++;
}
- fprintf(stderr, "total_status_fail = %d\ntotal_status_success = %d\ntotal_signaled = %d\ntotal_unused = %d\n",
+ STASIS_TEST_MSG("\ntotal_status_fail = %d\ntotal_status_success = %d\ntotal_signaled = %d\ntotal_unused = %d",
result.total_status_fail, result.total_status_success, result.total_signaled, result.total_unused);
STASIS_ASSERT(result.total_status_fail, "Should have failures");
STASIS_ASSERT(result.total_status_success, "Should have successes");
@@ -171,6 +171,43 @@ void test_mp_fail_fast() {
mp_pool_free(&p);
}
+static void test_mp_timeout() {
+ struct MultiProcessingPool *p = NULL;
+ p = mp_pool_init("timeout", "timeoutlogs");
+ p->status_interval = 1;
+ struct MultiProcessingTask *task = mp_pool_task(p, "timeout", NULL, "sleep 5");
+ int timeout = 3;
+ task->timeout = timeout;
+ mp_pool_join(p, 1, 0);
+ STASIS_ASSERT((task->time_data.duration >= (double) timeout && task->time_data.duration < (double) timeout + 1), "Timeout occurred out of desired range");
+ mp_pool_show_summary(p);
+ mp_pool_free(&p);
+}
+
+static void test_mp_seconds_to_human_readable() {
+ const struct testcase {
+ int seconds;
+ const char *expected;
+ } tc[] = {
+ {.seconds = -1, "-1s"},
+ {.seconds = 0, "0s"},
+ {.seconds = 10, "10s"},
+ {.seconds = 20, "20s"},
+ {.seconds = 30, "30s"},
+ {.seconds = 60, "1m 0s"},
+ {.seconds = 125, "2m 5s"},
+ {.seconds = 3600, "1h 0m 0s"},
+ {.seconds = 86399, "23h 59m 59s"},
+ {.seconds = 86400, "24h 0m 0s"},
+ };
+ for (size_t i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
+ char result[255] = {0};
+ seconds_to_human_readable(tc[i].seconds, result, sizeof(result));
+ STASIS_TEST_MSG("seconds=%d, expected: %s, got: %s", tc[i].seconds, tc[i].expected, result);
+ STASIS_ASSERT(strcmp(result, tc[i].expected) == 0, "bad output");
+ }
+}
+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void *pool_container(void *data) {
char *commands_sc[] = {
@@ -212,8 +249,13 @@ int main(int argc, char *argv[]) {
test_mp_pool_free,
test_mp_pool_workflow,
test_mp_fail_fast,
+ test_mp_timeout,
+ test_mp_seconds_to_human_readable,
test_mp_stop_continue
};
+
+ globals.task_timeout = 60;
+
STASIS_TEST_RUN(tests);
STASIS_TEST_END_MAIN();
}