/** * * \section COPYRIGHT * * Copyright 2013-2020 Software Radio Systems Limited * * By using this file, you agree to the terms and conditions set * forth in the LICENSE file which can be found at the top level of * the distribution. * */ #include "file_test_utils.h" #include "src/srslog/sinks/file_sink.h" #include "test_dummies.h" #include "testing_helpers.h" using namespace srslog; static constexpr char log_filename[] = "file_sink_test.log"; static bool when_data_is_written_to_file_then_contents_are_valid() { file_test_utils::scoped_file_deleter deleter(log_filename); file_sink file( log_filename, 0, std::unique_ptr(new test_dummies::log_formatter_dummy)); std::vector entries; for (unsigned i = 0; i != 10; ++i) { std::string entry = "Test log entry - " + std::to_string(i) + '\n'; file.write(detail::memory_buffer(entry)); entries.push_back(entry); } file.flush(); ASSERT_EQ(file_test_utils::file_exists(log_filename), true); ASSERT_EQ(file_test_utils::compare_file_contents(log_filename, entries), true); return true; } /// A Test-Specific Subclass of file_sink. This subclass provides public access /// to the data members of the parent class. class file_sink_subclass : public file_sink { public: file_sink_subclass(std::string name, size_t max_size) : file_sink( std::move(name), max_size, std::unique_ptr(new test_dummies::log_formatter_dummy)) {} uint32_t get_num_of_files() const { return get_file_index(); } }; static bool when_data_written_exceeds_size_threshold_then_new_file_is_created() { std::string filename0 = file_utils::build_filename_with_index(log_filename, 0); std::string filename1 = file_utils::build_filename_with_index(log_filename, 1); std::string filename2 = file_utils::build_filename_with_index(log_filename, 2); file_test_utils::scoped_file_deleter deleter = { filename0, filename1, filename2}; file_sink_subclass file(log_filename, 5001); // Build a 1000 byte entry. std::string entry(1000, 'a'); // Fill in the file with 5000 bytes, one byte less than the threshold. for (unsigned i = 0; i != 5; ++i) { file.write(detail::memory_buffer(entry)); } file.flush(); // Only one file should exist. ASSERT_EQ(file.get_num_of_files(), 1); // Trigger a file rotation. file.write(detail::memory_buffer(entry)); file.flush(); // A second file should be created. ASSERT_EQ(file.get_num_of_files(), 2); // Fill in the second file with 4000 bytes, one byte less than the threshold. for (unsigned i = 0; i != 4; ++i) { file.write(detail::memory_buffer(entry)); } file.flush(); // Two files should exist, third should not be created yet. ASSERT_EQ(file.get_num_of_files(), 2); // Trigger a file rotation. file.write(detail::memory_buffer(entry)); file.flush(); // Three files should exist. ASSERT_EQ(file.get_num_of_files(), 3); return true; } int main() { TEST_FUNCTION(when_data_is_written_to_file_then_contents_are_valid); TEST_FUNCTION( when_data_written_exceeds_size_threshold_then_new_file_is_created); return 0; }