Skip to content

Commit 1c9c0ac

Browse files
committed
Add tests.py
1 parent afbeca8 commit 1c9c0ac

File tree

7 files changed

+68
-7
lines changed

7 files changed

+68
-7
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ printer.o: printer.c printer.h cputracker.h
1414
cputracker.o: cputracker.c reader.h cputracker.h
1515

1616
format:
17-
clang-format --style=file -i *.c *.h
17+
clang-format --style=file -i *.c *.h
18+
19+
clean:
20+
rm -f *.o
21+
22+
.PHONY: clean

analyzer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "analyzer.h"
2-
#include "cputracker.h"
32

43
static unsigned long average_cpu_usage(struct proc_stat prev,
54
struct proc_stat current) {

cputracker.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void watchdog(void) {
6767
for (int i = 0; i < THREADS; i++) {
6868
if (working[i] == 0) {
6969
pthread_mutex_unlock(&watchdogMutex);
70-
perror("Error: Thread not responding... Terminating\n");
70+
printf("Error: Thread not responding... Terminating\n");
7171
handler(-1);
7272
return;
7373
}
@@ -77,6 +77,12 @@ static void watchdog(void) {
7777
}
7878
}
7979

80+
void main_test(void){
81+
int nproc;
82+
if (get_nproc(&nproc) == 0) {
83+
assert(nproc >= 1);
84+
}
85+
}
8086

8187
int main(void) {
8288
struct sigaction action;

cputracker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ extern sem_t g_printLeftSpaceSemaphore;
5151
unsigned long *get_item_from_print_buffer(void);
5252
struct proc_stat *get_item_from_data_buffer(void);
5353
void notify_watchdog(int id);
54+
void main_test(void);
5455
#endif

reader.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "reader.h"
2-
#include "cputracker.h"
32

4-
static int get_proc_stats(struct proc_stat *stats) {
3+
static int get_proc_stats(struct proc_stat *stats, int threads) {
54
FILE *file = fopen("/proc/stat", "r");
65
char line[1024];
7-
for (int thread = 0; thread < g_nproc; thread++) {
6+
for (int thread = 0; thread < threads; thread++) {
87
if (fgets(line, sizeof(line), file) == NULL ||
98
strncmp(line, "cpu", 3) != 0) {
109
fclose(file);
@@ -20,14 +19,21 @@ static int get_proc_stats(struct proc_stat *stats) {
2019
return 0;
2120
}
2221

22+
void reader_test(void){
23+
struct proc_stat stats;
24+
if (get_proc_stats(&stats,1) == 0) {
25+
assert(strncmp(stats.name, "cpu", 3) == 0);
26+
}
27+
}
28+
2329
void *reader(void *arg) {
2430
(void)arg;
2531
while (1) {
2632
notify_watchdog(Reader);
2733
sem_wait(&g_dataLeftSpaceSemaphore);
2834
pthread_mutex_lock(&g_dataBufferMutex);
2935

30-
if (get_proc_stats(get_item_from_data_buffer()) == -1) {
36+
if (get_proc_stats(get_item_from_data_buffer(), g_nproc) == -1) {
3137
pthread_mutex_unlock(&g_dataBufferMutex);
3238
sem_post(&g_dataLeftSpaceSemaphore);
3339
continue;

reader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
#define READ_DELAY 50000
66

77
void *reader(void *arg);
8+
void reader_test(void);
89
#endif

tests.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
import signal
5+
import time
6+
7+
def sigterm_case():
8+
process = subprocess.Popen(["./cputracker"], stdout=subprocess.PIPE)
9+
time.sleep(2)
10+
# Wysłanie sygnału SIGTERM do procesu
11+
process.send_signal(signal.SIGTERM)
12+
13+
# Oczekiwanie na zakończenie procesu
14+
process.wait()
15+
16+
# Odczytywanie wyjścia programu
17+
output = process.stdout.read()
18+
19+
if b"Error: Thread not responding... Terminating" in output:
20+
print("SIGTERM TEST: [OK]")
21+
else:
22+
print("SIGTERM TEST: [NO]")
23+
24+
def valgrind_case():
25+
process = subprocess.Popen(["valgrind --leak-check=full ./cputracker -s"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
26+
time.sleep(6)
27+
# Wysłanie sygnału SIGTERM do procesu
28+
process.send_signal(signal.SIGTERM)
29+
30+
# Oczekiwanie na zakończenie procesu
31+
process.wait()
32+
33+
# Odczytywanie wyjścia programu
34+
output = process.stderr.read()
35+
36+
if b"All heap blocks were freed -- no leaks are possible" in output:
37+
print("VALGRIND TEST: [OK]")
38+
else:
39+
print("VALGRIND TEST: [NO]")
40+
print(output)
41+
42+
sigterm_case()
43+
valgrind_case()

0 commit comments

Comments
 (0)