diff --git a/tests/log_test.c b/tests/log_test.c index d73a223..37039ec 100644 --- a/tests/log_test.c +++ b/tests/log_test.c @@ -4,14 +4,24 @@ #include #include #include +#include +#include int out_fd[2]; int err_fd[2]; const char *out_message = "standard message"; const char *err_message = "error message"; +const char *sem_name = "/alog_testing_sem"; int main() { + // RPC sync mechanism. Start locked + sem_t *sem = sem_open(sem_name, O_CREAT, 0x777, 0); + if (sem_unlink(sem_name)) { + perror("couldnt unlink semaphore"); + // might as well proceed since it is a leak anyways + } + // make RPC pipe pipe(out_fd); pipe(err_fd); @@ -23,7 +33,9 @@ int main() { pid_t childpid; if ((childpid = fork()) == -1) { - perror("fork fail"); + perror("cannot fork"); + sem_close(sem); + deinit_alog(); return 1; } @@ -40,14 +52,18 @@ int main() { alog(ERROR, err_message); alog(PRINT, out_message); - exit(0); + if (sem_post(sem)) { + perror("cannot increment semaphore"); + return 1; + } // parent process } else { // we can do checks from here char out_read_buffer[128] = {0}; char err_read_buffer[128] = {0}; - - usleep(10); + if (sem_wait(sem)) { + perror("cannot decrement semaphore"); + } read(err_fd[0], err_read_buffer, 128); // cant check read len because of timestamp @@ -75,8 +91,8 @@ int main() { printf("out and err log printing are successfull.\n"); + sem_close(sem); + deinit_alog(); } - - deinit_alog(); return 0; } diff --git a/tests/tests.mk b/tests/tests.mk index ef5360f..7681cee 100644 --- a/tests/tests.mk +++ b/tests/tests.mk @@ -3,7 +3,7 @@ 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) + $(CC) -o $(TARGET_DIR)/$@ $(BUILD_DIR)/$@.o $(ALOG_LIB) -lpthread chmod +x $(TARGET_DIR)/$@ alog-tests: $(ALOG_TESTS)