add rudimentary unit tests, and logging timestamps
Signed-off-by: Aidan Hahn <aidan@aidanis.online>
This commit is contained in:
parent
7fafcd344d
commit
1e9b701542
6 changed files with 188 additions and 37 deletions
82
tests/log_test.c
Normal file
82
tests/log_test.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include "../alog.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int out_fd[2];
|
||||
int err_fd[2];
|
||||
|
||||
const char *out_message = "standard message";
|
||||
const char *err_message = "error message";
|
||||
|
||||
int main() {
|
||||
// make RPC pipe
|
||||
pipe(out_fd);
|
||||
pipe(err_fd);
|
||||
|
||||
// remove stdout and stderr
|
||||
_init_alog();
|
||||
alog_remove_target(1);
|
||||
alog_remove_target(2);
|
||||
|
||||
pid_t childpid;
|
||||
if ((childpid = fork()) == -1) {
|
||||
perror("fork fail");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// child process
|
||||
if (childpid == 0) {
|
||||
close(out_fd[0]);
|
||||
close(err_fd[0]);
|
||||
|
||||
// add alog targets in this pipe
|
||||
alog_add_target(out_fd[1], ALOG_OUT);
|
||||
alog_add_target(err_fd[1], ALOG_ERR);
|
||||
|
||||
// write to the things
|
||||
alog(ERROR, err_message);
|
||||
alog(PRINT, out_message);
|
||||
|
||||
exit(0);
|
||||
// parent process
|
||||
} else {
|
||||
// we can do checks from here
|
||||
char out_read_buffer[128] = {0};
|
||||
char err_read_buffer[128] = {0};
|
||||
|
||||
usleep(10);
|
||||
|
||||
read(err_fd[0], err_read_buffer, 128);
|
||||
// cant check read len because of timestamp
|
||||
if (!strstr(err_read_buffer, err_message)) {
|
||||
printf("err log should contain '%s'.\ninstead reads '%s'\n",
|
||||
err_message, err_read_buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int num_out = read(out_fd[0], out_read_buffer, 128);
|
||||
if (num_out != (int) (strlen(out_message) + strlen(err_read_buffer))) {
|
||||
printf("Read %i bytes from out log when %lu were written!\n",
|
||||
num_out, strlen(out_message) + strlen(err_read_buffer) + 1);
|
||||
return 1;
|
||||
}
|
||||
if (strncmp(out_read_buffer, err_read_buffer, strlen(err_read_buffer))) {
|
||||
printf("out log first message should read '%s'.\ninstead reads '%s'\n",
|
||||
err_read_buffer, out_read_buffer);
|
||||
}
|
||||
if (strcmp(&out_read_buffer[strlen(err_read_buffer)], out_message)) {
|
||||
printf("out log second message should read '%s'.\ninstead reads '%s'\n",
|
||||
out_message, &out_read_buffer[strlen(err_read_buffer)]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
printf("out and err log printing are successfull.\n");
|
||||
}
|
||||
|
||||
deinit_alog();
|
||||
return 0;
|
||||
}
|
||||
12
tests/tests.mk
Normal file
12
tests/tests.mk
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
ALOG_TEST_SRCS = $(shell find tests -iname "*.c" -exec basename {} \;)
|
||||
ALOG_TESTS = $(ALOG_TEST_SRCS:.c=)
|
||||
|
||||
$(ALOG_TESTS): $(ALOG_LIB)
|
||||
$(CC) $(CFLAGS) -g -o $(BUILD_DIR)/$@.o -c tests/$@.c
|
||||
$(CC) -o $(TARGET_DIR)/$@ $(BUILD_DIR)/$@.o $(ALOG_LIB)
|
||||
chmod +x $(TARGET_DIR)/$@
|
||||
|
||||
run-tests: $(ALOG_TESTS)
|
||||
for test in $^ ; do \
|
||||
$(TARGET_DIR)/$$test ; \
|
||||
done
|
||||
Loading…
Add table
Add a link
Reference in a new issue