aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAngelo Marletta <angelo.marletta@gmail.com>2012-06-21 02:06:58 +0100
committerAngelo Marletta <angelo.marletta@gmail.com>2012-06-21 02:06:58 +0100
commitce079fc58d6d30c3fe400a777934b4a4cce85761 (patch)
treeb5e9db6655a79921ed89370c541da7472700b30f /tests
parentc08039365f62abe5f180c9c3c6c3f8fac6feb378 (diff)
downloadcpulimit-ce079fc58d6d30c3fe400a777934b4a4cce85761.tar.gz
multiplatform refactoring. added more tests. works but there is a fd leak.
Diffstat (limited to 'tests')
-rw-r--r--tests/process_iterator_test.c86
1 files changed, 69 insertions, 17 deletions
diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c
index dcd4ebe..566648d 100644
--- a/tests/process_iterator_test.c
+++ b/tests/process_iterator_test.c
@@ -4,29 +4,36 @@
#include <unistd.h>
#include <assert.h>
#include <time.h>
+#include <signal.h>
#include <process_iterator.h>
#include <process_group.h>
+volatile sig_atomic_t child;
+
+void kill_child(int sig)
+{
+ kill(child, SIGINT);
+}
+
void test_single_process()
{
struct process_iterator it;
struct process process;
struct process_filter filter;
int count;
- time_t now;
//don't iterate children
filter.pid = getpid();
filter.include_children = 0;
count = 0;
- now = time(NULL);
+// time_t now = time(NULL);
init_process_iterator(&it, &filter);
while (get_next_process(&it, &process) == 0)
{
assert(process.pid == getpid());
assert(process.ppid == getppid());
assert(process.cputime < 100);
- assert(process.starttime == now || process.starttime == now - 1);
+// assert(process.starttime == now || process.starttime == now - 1);
count++;
}
assert(count == 1);
@@ -35,14 +42,14 @@ void test_single_process()
filter.pid = getpid();
filter.include_children = 0;
count = 0;
- now = time(NULL);
+// now = time(NULL);
init_process_iterator(&it, &filter);
while (get_next_process(&it, &process) == 0)
{
assert(process.pid == getpid());
assert(process.ppid == getppid());
assert(process.cputime < 100);
- assert(process.starttime == now || process.starttime == now - 1);
+// assert(process.starttime == now || process.starttime == now - 1);
count++;
}
assert(count == 1);
@@ -54,29 +61,30 @@ void test_multiple_process()
struct process_iterator it;
struct process process;
struct process_filter filter;
- int child = fork();
+ pid_t child = fork();
if (child == 0)
{
- //child code
+ //child is supposed to be killed by the parent :/
sleep(1);
- exit(0);
+ exit(1);
}
filter.pid = getpid();
filter.include_children = 1;
init_process_iterator(&it, &filter);
int count = 0;
- time_t now = time(NULL);
+// time_t now = time(NULL);
while (get_next_process(&it, &process) == 0)
{
if (process.pid == getpid()) assert(process.ppid == getppid());
else if (process.pid == child) assert(process.ppid == getpid());
else assert(0);
assert(process.cputime < 100);
- assert(process.starttime == now || process.starttime == now - 1);
+// assert(process.starttime == now || process.starttime == now - 1);
count++;
}
assert(count == 2);
close_process_iterator(&it);
+ kill(child, SIGINT);
}
void test_all_processes()
@@ -87,14 +95,14 @@ void test_all_processes()
filter.pid = 0;
init_process_iterator(&it, &filter);
int count = 0;
- time_t now = time(NULL);
+// time_t now = time(NULL);
while (get_next_process(&it, &process) == 0)
{
if (process.pid == getpid())
{
assert(process.ppid == getppid());
assert(process.cputime < 100);
- assert(process.starttime == now || process.starttime == now - 1);
+// assert(process.starttime == now || process.starttime == now - 1);
}
count++;
}
@@ -102,21 +110,65 @@ void test_all_processes()
close_process_iterator(&it);
}
-void test_process_list()
+void test_process_group_all()
{
struct process_group pgroup;
- pid_t current_pid = getpid();
- assert(init_process_group(&pgroup, current_pid, 0) == 0);
+ assert(init_process_group(&pgroup, 0, 0) == 0);
+ update_process_group(&pgroup);
+ struct list_node *node = NULL;
+ int count = 0;
+ for (node=pgroup.proclist->first; node!= NULL; node=node->next) {
+ count++;
+ }
+ assert(count > 10);
update_process_group(&pgroup);
assert(close_process_group(&pgroup) == 0);
}
+void test_process_group_single()
+{
+ struct process_group pgroup;
+ child = fork();
+ if (child == 0)
+ {
+ //child is supposed to be killed by the parent :/
+ while(1);
+ exit(1);
+ }
+ signal(SIGABRT, &kill_child);
+ signal(SIGTERM, &kill_child);
+ assert(init_process_group(&pgroup, child, 0) == 0);
+ int i;
+ for (i=0; i<10000; i++)
+ {
+ update_process_group(&pgroup);
+ struct list_node *node = NULL;
+ int count = 0;
+ for (node=pgroup.proclist->first; node!= NULL; node=node->next) {
+ struct process *p = (struct process*)(node->data);
+ assert(p->pid == child);
+ assert(p->ppid == getpid());
+ assert(p->cpu_usage <= 1.2);
+printf("%f\n", p->cpu_usage);
+ count++;
+ }
+ assert(count == 1);
+ struct timespec interval;
+ interval.tv_sec = 0;
+ interval.tv_nsec = 100000000;
+ nanosleep(&interval, NULL);
+ }
+ assert(close_process_group(&pgroup) == 0);
+ kill(child, SIGINT);
+}
+
int main()
{
- printf("Pid %d\n", getpid());
+// printf("Pid %d\n", getpid());
test_single_process();
test_multiple_process();
test_all_processes();
- test_process_list();
+ test_process_group_all();
+ test_process_group_single();
return 0;
}
Un proyecto texto-plano.xyz