diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2024-07-26 22:23:17 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2024-07-26 22:23:17 -0400 |
| commit | 47f09225b07c33c7657ded5bbe4c7e4f98eb9e30 (patch) | |
| tree | 3bedbf21474f480af8e4c47b3e0de6e79ab6fb81 /eztester.c | |
| parent | 6f864672834e646aedbdf8c279b58226e8a2a2f3 (diff) | |
FEAT: test can now time out
Tests now have an extra field `max_time_ms` which is the max duration
before it's process gets killed. A time of 0 ms is used to disable
eztester managed timeouts, however a test can still return a timeout
status which will be handled similarily.
Diffstat (limited to 'eztester.c')
| -rw-r--r-- | eztester.c | 39 |
1 files changed, 35 insertions, 4 deletions
@@ -150,14 +150,16 @@ void eztester_run(eztester_list *test_list, eztester_behavior behavior) { mem->work_in_queue = false; mem->behavior = behavior; - pid_t pid = fork(); + pid_t pid, child_pgid; + pid = fork(); if (pid < 0) { perror("fork"); exit(1); - } - if (pid == 0) { + } else if (pid == 0) { + setpgrp(); _ez_worker(mem, test_list); } + child_pgid = pid; eztester_status status; eztester_test test; @@ -180,12 +182,41 @@ void eztester_run(eztester_list *test_list, eztester_behavior behavior) { kill(pid, SIGCONT); + unsigned int elapsed_ms = 0; while (mem->work_in_queue) { - usleep(50e3); + usleep(1e3); + elapsed_ms++; if (_ez_child_premature_exit) { _ez_premature_exit("Worker Process ended prematurely!", pid, mem, results); } + if (test.max_time_ms > 0 && elapsed_ms > test.max_time_ms) { + + int status; + signal(SIGCHLD, SIG_DFL); + + // ask nicely + killpg(child_pgid, SIGTERM); + usleep(10e3); + if (waitpid(pid, &status, WNOHANG) == 0) { + // no longer ask nicely + killpg(child_pgid, SIGKILL); + } + + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(1); + } else if (pid == 0) { + setpgrp(); + _ez_worker(mem, test_list); + } + child_pgid = pid; + signal(SIGCHLD, _ez_chld_handler); + + mem->work_in_queue = false; + mem->status = TEST_TIMEOUT; + } } status = mem->status; |
