aboutsummaryrefslogtreecommitdiff
path: root/tests/test_multiprocessing.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-09-15 16:12:26 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-09-18 23:07:14 -0400
commit754bb98ed952b589d41affdf677f8284926f368a (patch)
tree8e486047dd820d6e008bb79e8a8996bb06338022 /tests/test_multiprocessing.c
parentead0756a0df1de9d2011359393234a605b6f0313 (diff)
downloadstasis-754bb98ed952b589d41affdf677f8284926f368a.tar.gz
Add test_multiprocessing.c
Diffstat (limited to 'tests/test_multiprocessing.c')
-rw-r--r--tests/test_multiprocessing.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c
new file mode 100644
index 0000000..3d1d972
--- /dev/null
+++ b/tests/test_multiprocessing.c
@@ -0,0 +1,73 @@
+#include "testing.h"
+
+static struct MultiProcessingPool *pool;
+char *commands[] = {
+ "true",
+ "uname -a",
+ "/bin/echo hello world",
+};
+
+void test_mp_pool_init() {
+ STASIS_ASSERT((pool = mp_pool_init("mypool", "mplogs")) != NULL, "Pool initialization failed");
+ STASIS_ASSERT_FATAL(pool != NULL, "Should not be NULL");
+ STASIS_ASSERT(pool->num_alloc == MP_POOL_TASK_MAX, "Wrong number of default records");
+ STASIS_ASSERT(pool->num_used == 0, "Wrong number of used records");
+ STASIS_ASSERT(strcmp(pool->log_root, "mplogs") == 0, "Wrong log root directory");
+ STASIS_ASSERT(strcmp(pool->ident, "mypool") == 0, "Wrong identity");
+ int data_bad = 0;
+ char *data = calloc(pool->num_alloc, sizeof(*pool->task));
+ for (size_t i = 0; i < pool->num_alloc * sizeof(*pool->task); i++) {
+ if (data[i] != 0) {
+ data_bad = 1;
+ break;
+ }
+ }
+ free(data);
+ STASIS_ASSERT(data_bad == 0, "Task array should be zeroed");
+ mp_pool_free(&pool);
+}
+
+void test_mp_task() {
+ pool = mp_pool_init("mypool", "mplogs");
+
+ if (pool) {
+ for (size_t i = 0; i < sizeof(commands) / sizeof(*commands); i++) {
+ struct MultiProcessingTask *task;
+ char task_name[100] = {0};
+ sprintf(task_name, "mytask%zu", i);
+ STASIS_ASSERT_FATAL((task = mp_task(pool, task_name, commands[i])) != NULL, "Task should not be NULL");
+ STASIS_ASSERT(task->pid != 0, "PID should be non-zero at this point");
+ STASIS_ASSERT(task->parent_pid != MP_POOL_PID_UNUSED, "Parent PID should be non-zero");
+ STASIS_ASSERT(task->status == 0, "Status should be zero");
+ STASIS_ASSERT(strcmp(task->ident, task_name) == 0, "Wrong task identity");
+ STASIS_ASSERT(strstr(task->log_file, pool->log_root) != NULL, "Log file path must be in log_root");
+ STASIS_ASSERT(task->gate != NULL, "Semaphore should be initialized");
+ }
+ }
+}
+
+void test_mp_pool_join() {
+ STASIS_ASSERT(mp_pool_join(pool, get_cpu_count(), 0) == 0, "Pool tasks should have not have failed");
+ for (size_t i = 0; i < pool->num_used; i++) {
+ struct MultiProcessingTask *task = &pool->task[i];
+ STASIS_ASSERT(task->pid == MP_POOL_PID_UNUSED, "Task should be marked as unused");
+ STASIS_ASSERT(task->status == 0, "Task should have succeeded");
+ }
+}
+
+void test_mp_pool_free() {
+ mp_pool_free(&pool);
+ STASIS_ASSERT(pool == NULL, "Should be NULL");
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ STASIS_TEST_FUNC *tests[] = {
+ test_mp_pool_init,
+ test_mp_task,
+ test_mp_pool_join,
+ test_mp_pool_free,
+ };
+ STASIS_TEST_RUN(tests);
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file