Skip to content

Commit

Permalink
feat(tests): rework tests in numerous ways
Browse files Browse the repository at this point in the history
  • Loading branch information
ns6089 committed Aug 22, 2024
1 parent 0a6abb7 commit 86f7bb2
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 445 deletions.
3 changes: 3 additions & 0 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "thread_safe.h"
#include "utility.h"

#include <bitset>

namespace audio {
enum stream_config_e : int {
STEREO, ///< Stereo
Expand Down
90 changes: 51 additions & 39 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ bl::sources::severity_logger<int> info(2); // Should be informed about
bl::sources::severity_logger<int> warning(3); // Strange events
bl::sources::severity_logger<int> error(4); // Recoverable errors
bl::sources::severity_logger<int> fatal(5); // Unrecoverable errors
#ifdef SUNSHINE_TESTS
bl::sources::severity_logger<int> tests(10); // Automatic tests output
#endif

BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)

Expand All @@ -50,6 +53,51 @@ namespace logging {
sink.reset();
}

void
formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os) {
constexpr const char *message = "Message";
constexpr const char *severity = "Severity";

auto log_level = view.attribute_values()[severity].extract<int>().get();

std::string_view log_type;
switch (log_level) {
case 0:
log_type = "Verbose: "sv;
break;
case 1:
log_type = "Debug: "sv;
break;
case 2:
log_type = "Info: "sv;
break;
case 3:
log_type = "Warning: "sv;
break;
case 4:
log_type = "Error: "sv;
break;
case 5:
log_type = "Fatal: "sv;
break;
#ifdef SUNSHINE_TESTS
case 10:
log_type = "Tests: "sv;
break;
#endif
};

auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now - std::chrono::time_point_cast<std::chrono::seconds>(now));

auto t = std::chrono::system_clock::to_time_t(now);
auto lt = *std::localtime(&t);

os << "["sv << std::put_time(&lt, "%Y-%m-%d %H:%M:%S.") << boost::format("%03u") % ms.count() << "]: "sv
<< log_type << view.attribute_values()[message].extract<std::string>();
}

[[nodiscard]] std::unique_ptr<deinit_t>
init(int min_log_level, const std::string &log_file) {
if (sink) {
Expand All @@ -61,49 +109,13 @@ namespace logging {

sink = boost::make_shared<text_sink>();

#ifndef SUNSHINE_TESTS
boost::shared_ptr<std::ostream> stream { &std::cout, boost::null_deleter() };
sink->locked_backend()->add_stream(stream);
#endif
sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(log_file));
sink->set_filter(severity >= min_log_level);

sink->set_formatter([](const bl::record_view &view, bl::formatting_ostream &os) {
constexpr const char *message = "Message";
constexpr const char *severity = "Severity";

auto log_level = view.attribute_values()[severity].extract<int>().get();

std::string_view log_type;
switch (log_level) {
case 0:
log_type = "Verbose: "sv;
break;
case 1:
log_type = "Debug: "sv;
break;
case 2:
log_type = "Info: "sv;
break;
case 3:
log_type = "Warning: "sv;
break;
case 4:
log_type = "Error: "sv;
break;
case 5:
log_type = "Fatal: "sv;
break;
};

auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now - std::chrono::time_point_cast<std::chrono::seconds>(now));

auto t = std::chrono::system_clock::to_time_t(now);
auto lt = *std::localtime(&t);

os << "["sv << std::put_time(&lt, "%Y-%m-%d %H:%M:%S.") << boost::format("%03u") % ms.count() << "]: "sv
<< log_type << view.attribute_values()[message].extract<std::string>();
});
sink->set_formatter(&formatter);

// Flush after each log record to ensure log file contents on disk isn't stale.
// This is particularly important when running from a Windows service.
Expand Down
6 changes: 6 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ extern boost::log::sources::severity_logger<int> info;
extern boost::log::sources::severity_logger<int> warning;
extern boost::log::sources::severity_logger<int> error;
extern boost::log::sources::severity_logger<int> fatal;
#ifdef SUNSHINE_TESTS
extern boost::log::sources::severity_logger<int> tests;
#endif

#include "config.h"
#include "stat_trackers.h"
Expand All @@ -41,6 +44,9 @@ namespace logging {
void
deinit();

void
formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os);

/**
* @brief Initialize the logging system.
* @param min_log_level The minimum log level to output.
Expand Down
14 changes: 7 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ if (NOT (TESTS_SOFTWARE_ENCODER_UNAVAILABLE STREQUAL "fail" OR TESTS_SOFTWARE_EN
endif ()
list(APPEND TEST_DEFINITIONS TESTS_SOFTWARE_ENCODER_UNAVAILABLE="${TESTS_SOFTWARE_ENCODER_UNAVAILABLE}") # fail/skip

file(GLOB_RECURSE TEST_SOURCES
${CMAKE_SOURCE_DIR}/tests/conftest.cpp
${CMAKE_SOURCE_DIR}/tests/utils.cpp
${CMAKE_SOURCE_DIR}/tests/test_*.cpp)
# this indicates we're building tests in case sunshine needs to adjust some code or add private tests
list(APPEND TEST_DEFINITIONS SUNSHINE_TESTS)

file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS
${CMAKE_SOURCE_DIR}/tests/*.h
${CMAKE_SOURCE_DIR}/tests/*.cpp)

set(SUNSHINE_SOURCES
${SUNSHINE_TARGET_FILES})
Expand All @@ -64,16 +66,14 @@ set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME}
${SUNSHINE_EXTERNAL_LIBRARIES}
gtest
gtest_main # if we use this we don't need our own main function
${PLATFORM_LIBRARIES})
target_compile_definitions(${PROJECT_NAME} PUBLIC ${SUNSHINE_DEFINITIONS} ${TEST_DEFINITIONS})
target_compile_options(${PROJECT_NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${SUNSHINE_COMPILE_OPTIONS}>;$<$<COMPILE_LANGUAGE:CUDA>:${SUNSHINE_COMPILE_OPTIONS_CUDA};-std=c++17>) # cmake-lint: disable=C0301
target_link_options(${PROJECT_NAME} PRIVATE)

if (WIN32)
# prefer static libraries since we're linking statically
# this fixes gtest_main and libcurl linking errors, when using non MSYS2 version of CMake
# this fixes libcurl linking errors when using non MSYS2 version of CMake
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_SEARCH_START_STATIC 1)
endif ()

add_test(NAME ${PROJECT_NAME} COMMAND sunshine_test)
202 changes: 0 additions & 202 deletions tests/conftest.cpp

This file was deleted.

Loading

0 comments on commit 86f7bb2

Please sign in to comment.