Tests for wait()

This commit is contained in:
Pithikos 2015-01-18 00:26:04 +00:00
parent bd4991e11b
commit b38f0f35c4
3 changed files with 108 additions and 0 deletions

View File

@ -25,6 +25,12 @@ function extract_num { #needle with number #haystack
}
function time_exec { #command ..
realsecs=$(/usr/bin/time -f '%e' "$@" 2>&1 > /dev/null)
echo "$realsecs"
}
function err { #string #log
echo "------------------- ERROR ------------------------"
echo "$1"

46
tests/src/wait.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "../../thpool.h"
/*
* This program takes 3 arguments: number of jobs to add,
* number of threads,
* wait for each thread (1) or all threads at once (0)?
*
* Each job is to simply sleep for one second.
*
* */
void sleep_1() {
sleep(1);
}
int main(int argc, char *argv[]){
char* p;
if (argc != 4){
puts("This testfile needs excactly three arguments");
exit(1);
}
int num_jobs = strtol(argv[1], &p, 10);
int num_threads = strtol(argv[2], &p, 10);
int wait_each_job = strtol(argv[3], &p, 10);
threadpool thpool = thpool_init(num_threads);
int n;
for (n=0; n<num_jobs; n++){
thpool_add_work(thpool, (void*)sleep_1, NULL);
if (wait_each_job)
thpool_wait(thpool);
}
if (!wait_each_job)
thpool_wait(thpool);
return 0;
}

56
tests/wait Executable file
View File

@ -0,0 +1,56 @@
#! /bin/bash
#
# This file has several tests to check for memory leaks.
# valgrind is used so make sure you have it installed
#
. funcs
# ---------------------------- Tests -----------------------------------
function test_wait_each_job { #threads #jobs
echo "Will test waiting for each job ($1 threads, $2 jobs)"
gcc src/wait.c ../thpool.c -pthread -o test
realsecs=$(time_exec ./test $2 $1 1)
threshold=1.00 # in secs
ret=$(python -c "print((abs($realsecs-$2))<=$threshold)")
if [ "$ret" == "True" ]; then
return
fi
err "Elapsed $realsecs which is more than than allowed"
exit 1
}
function test_wait_pool { #threads #jobs
echo "Will test waiting for whole threadpool ($1 threads, $2 jobs)"
gcc src/wait.c ../thpool.c -pthread -o test
realsecs=$(time_exec ./test $2 $1 0)
threshold=1.00 # in secs
expected_time=$(python -c "import math; print(math.ceil($2/$1.0))")
ret=$(python -c "print((abs($realsecs-$expected_time))<=$threshold)")
if [ "$ret" == "True" ]; then
return
fi
err "Elapsed $realsecs which is too different from what expected ($expected_time)"
exit 1
}
# Run tests
test_wait_each_job 1 4
test_wait_each_job 4 4
test_wait_each_job 4 10
test_wait_pool 1 4
test_wait_pool 8 2
test_wait_pool 4 4
test_wait_pool 4 20
echo "No errors"