From: Davin McCall Date: Sun, 29 Dec 2019 06:05:41 +0000 (+1000) Subject: Add a simple logging test X-Git-Tag: v0.8.1~8 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=refs%2Fheads%2Fdevelopment;p=oweals%2Fdinit.git Add a simple logging test --- diff --git a/src/tests/test-includes/dinit.h b/src/tests/test-includes/dinit.h index a689da7..bf6eb68 100644 --- a/src/tests/test-includes/dinit.h +++ b/src/tests/test-includes/dinit.h @@ -18,6 +18,7 @@ namespace bp_sys { extern pid_t last_forked_pid; } +// This is a mock for a Dasynq-based event loop class eventloop_t { time_val current_time {0, 0}; @@ -42,6 +43,14 @@ class eventloop_t } } + void send_fd_event(int fd, int events) + { + auto i = regd_fd_watchers.find(fd); + if (i != regd_fd_watchers.end()) { + i->second->fd_event(*this, fd, events); + } + } + class child_proc_watcher { public: diff --git a/src/tests/tests.cc b/src/tests/tests.cc index e4f9be3..fdc8e00 100644 --- a/src/tests/tests.cc +++ b/src/tests/tests.cc @@ -3,6 +3,7 @@ #include "service.h" #include "test_service.h" +#include "baseproc-sys.h" constexpr static auto REG = dependency_type::REGULAR; constexpr static auto WAITS = dependency_type::WAITS_FOR; @@ -708,6 +709,41 @@ void test15() assert(! tl.got_started); } +static void flush_log(int fd) +{ + while (! is_log_flushed()) { + event_loop.send_fd_event(fd, dasynq::OUT_EVENTS); + event_loop.send_fd_event(STDOUT_FILENO, dasynq::OUT_EVENTS); + } + + std::vector wdata; + bp_sys::extract_written_data(fd, wdata); + bp_sys::extract_written_data(0, wdata); +} + +void test_log1() +{ + service_set sset; + init_log(&sset, true /* syslog format */); + + int logfd = bp_sys::allocfd(); + setup_main_log(logfd); + + flush_log(logfd); + + log(loglevel_t::ERROR, "test one"); + + // flush + //event_loop. + event_loop.send_fd_event(logfd, dasynq::OUT_EVENTS); + + std::vector wdata; + bp_sys::extract_written_data(logfd, wdata); + + std::string wstr {wdata.begin(), wdata.end()}; + + assert(wstr == "<27>dinit: test one\n"); +} #define RUN_TEST(name, spacing) \ std::cout << #name "..." spacing << std::flush; \ @@ -734,4 +770,5 @@ int main(int argc, char **argv) RUN_TEST(test13, " "); RUN_TEST(test14, " "); RUN_TEST(test15, " "); + RUN_TEST(test_log1, " "); }