Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ functions:
set_cmake_var realm_vars CMAKE_TOOLCHAIN_FILE PATH "${cmake_toolchain_file}"
fi

set_cmake_var realm_vars REALM_TEST_LOGGING BOOL On
set_cmake_var realm_vars REALM_TEST_LOGGING_LEVEL STRING "${test_logging_level|debug}"

if [[ -n "${test_timeout_extra|}" ]]; then
Expand Down
15 changes: 15 additions & 0 deletions src/realm/util/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <iostream>
#include <mutex>
#include <sstream>

namespace realm::util {

Expand Down Expand Up @@ -110,6 +111,20 @@ const std::string_view Logger::level_to_string(Level level) noexcept
return "";
}

bool Logger::get_env_log_level_if_set(Level& level) noexcept
{
const char* str = getenv("UNITTEST_LOG_LEVEL");
if (!str || strlen(str) == 0)
return true;

std::istringstream in(str);
in.imbue(std::locale::classic());
in.flags(in.flags() & ~std::ios_base::skipws); // Do not accept white space
in >> level;

return in && in.get() == std::char_traits<char>::eof();
}

Comment on lines +114 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend moving this to the test directory (e.g. test/util/) instead of the Core base.

void StderrLogger::do_log(Level level, const std::string& message)
{
static Mutex mutex;
Expand Down
1 change: 1 addition & 0 deletions src/realm/util/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Logger {
static void set_default_level_threshold(Level level) noexcept;
static Level get_default_level_threshold() noexcept;
static const std::string_view level_to_string(Level level) noexcept;
static bool get_env_log_level_if_set(Level& level) noexcept;

protected:
// Used by subclasses that link to a base logger
Expand Down
5 changes: 3 additions & 2 deletions test/combined_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ int main(int argc, const char* argv[])
return 1;
int status = test_all();
if (status) {
std::cerr << "core and sync tests failed: " << status << std::endl;
std::cerr << "(!) Core/Sync tests failed: " << status << std::endl;
return status;
}
std::cout << "core and sync tests passed\n";
std::cout << "--------------------------\n"
<< "(+) Core/Sync tests passed\n\n";
return run_object_store_tests(argc, argv);
}
18 changes: 5 additions & 13 deletions test/object-store/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,12 @@ if(REALM_ENABLE_SYNC)
endif()
endif()

if(REALM_TEST_LOGGING)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend keeping the TEST_ENABLE_LOGGING value so it works like before (based on the logic in test_file.hpp) and update the logic to something like this:

set(logging_level "off")
if(DEFINED REALM_TEST_LOGGING_LEVEL)
    if (NOT "${REALM_TEST_LOGGING_LEVEL}" STREQUAL "")
        set(logging_level ${REALM_TEST_LOGGING_LEVEL})
    endif()
elseif(TEST_ENABLE_LOGGING)
    set(logging_level "all")
endif()
set(REALM_TEST_LOGGING_LEVEL ${logging_level} CACHE STRING "Object-store tests logging level" FORCE)
message(STATUS "Test logging level: ${REALM_TEST_LOGGING_LEVEL}")
target_compile_definitions(ObjectStoreTestLib PRIVATE TEST_LOGGING_LEVEL=${REALM_TEST_LOGGING_LEVEL})

target_compile_definitions(ObjectStoreTestLib PRIVATE
TEST_ENABLE_LOGGING=1
)

if(REALM_TEST_LOGGING_LEVEL)
message(STATUS "Test logging level: ${REALM_TEST_LOGGING_LEVEL}")
target_compile_definitions(ObjectStoreTestLib PRIVATE
TEST_LOGGING_LEVEL=${REALM_TEST_LOGGING_LEVEL}
)
else()
message(STATUS "Test logging enabled")
endif()
set(REALM_TEST_LOGGING_LEVEL "off" CACHE STRING "Object-store tests logging level")
if ("${REALM_TEST_LOGGING_LEVEL}" STREQUAL "")
set(REALM_TEST_LOGGING_LEVEL "off" CACHE STRING "Object-store tests logging level" FORCE)
endif()
message(STATUS "Test logging level: ${REALM_TEST_LOGGING_LEVEL}")
target_compile_definitions(ObjectStoreTestLib PRIVATE TEST_LOGGING_LEVEL=${REALM_TEST_LOGGING_LEVEL})

# Optional extra time to add to test timeout values
if(REALM_TEST_TIMEOUT_EXTRA)
Expand Down
5 changes: 2 additions & 3 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ TEST_CASE("app: verify app error codes", "[sync][app][local]") {
return false;
}
}
catch (nlohmann::json::exception ex) {
catch (const nlohmann::json::exception&) {
// It's also a failure if parsing the json body throws an exception
return false;
}
Expand Down Expand Up @@ -3929,7 +3929,6 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {
std::string base_file_path = util::make_temp_dir() + random_string(10);
auto redir_transport = std::make_shared<BaseUrlTransport>();
AutoVerifiedEmailCredentials creds;
util::Logger::set_default_level_threshold(realm::util::Logger::Level::TEST_LOGGING_LEVEL);
auto logger = util::Logger::get_default_logger();

App::Config app_config = {"fake-app-id"};
Expand Down Expand Up @@ -4148,7 +4147,7 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {
SECTION("Verify new sync session updates location") {
bool use_ssl = GENERATE(true, false);
std::string expected_host = "redirect.someurl.fake";
int expected_port = 8081;
sync::port_type expected_port = 8081;
std::string init_url = util::format("http%1://alternate.someurl.fake", use_ssl ? "s" : "");
std::string init_wsurl = util::format("ws%1://alternate.someurl.fake", use_ssl ? "s" : "");
std::string redir_url = util::format("http%1://%2:%3", use_ssl ? "s" : "", expected_host, expected_port);
Expand Down
19 changes: 17 additions & 2 deletions test/object-store/util/test_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ TestFile::TestFile()
disable_sync_to_disk();
m_temp_dir = util::make_temp_dir();
path = (fs::path(m_temp_dir) / "realm.XXXXXX").string();
util::Logger::set_default_level_threshold(realm::util::Logger::Level::TEST_LOGGING_LEVEL);
util::Logger::set_default_level_threshold(TestSyncManager::default_log_level());
if (const char* crypt_key = test_util::crypt_key()) {
encryption_key = std::vector<char>(crypt_key, crypt_key + 64);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to address line 200 of this file, since it also uses the TEST_ENABLE_LOGGING variable, which is no longer being set.

#if TEST_ENABLE_LOGGING
                   m_logger = util::Logger::get_default_logger();
#else
                   // Logging is disabled, use a NullLogger to prevent printing anything
                   m_logger.reset(new util::NullLogger());
#endif

Expand Down Expand Up @@ -340,7 +340,7 @@ TestAppSession::TestAppSession(AppSession session,
if (!m_transport)
m_transport = instance_of<SynchronousTestTransport>;
auto app_config = get_config(m_transport, *m_app_session);
util::Logger::set_default_level_threshold(realm::util::Logger::Level::TEST_LOGGING_LEVEL);
util::Logger::set_default_level_threshold(TestSyncManager::default_log_level());
set_app_config_defaults(app_config, m_transport);

util::try_make_dir(m_base_file_path);
Expand Down Expand Up @@ -423,6 +423,21 @@ std::vector<bson::BsonDocument> TestAppSession::get_documents(SyncUser& user, co

// MARK: - TestSyncManager

TestSyncManager::LoggerLevel TestSyncManager::default_log_level()
{
#ifndef TEST_LOGGING_LEVEL
#define TEST_LOGGING_LEVEL off
#endif

static LoggerLevel log_level = []() {
LoggerLevel level = LoggerLevel::TEST_LOGGING_LEVEL;
realm::util::Logger::get_env_log_level_if_set(level);
return level;
}();

return log_level;
}

TestSyncManager::TestSyncManager(const Config& config, const SyncServer::Config& sync_server_config)
: transport(config.transport ? config.transport : std::make_shared<Transport>(network_callback))
, m_sync_server(sync_server_config)
Expand Down
17 changes: 4 additions & 13 deletions test/object-store/util/test_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,6 @@ struct InMemoryTestFile : realm::Realm::Config {
void advance_and_notify(realm::Realm& realm);
void on_change_but_no_notify(realm::Realm& realm);

#ifndef TEST_ENABLE_LOGGING
#define TEST_ENABLE_LOGGING 0 // change to 1 to enable trace-level logging
#endif

#ifndef TEST_LOGGING_LEVEL
#if TEST_ENABLE_LOGGING
#define TEST_LOGGING_LEVEL all
#else
#define TEST_LOGGING_LEVEL off
#endif // TEST_ENABLE_LOGGING
#endif // TEST_LOGGING_LEVEL

#if REALM_ENABLE_SYNC

using StartImmediately = realm::util::TaggedBool<class StartImmediatelyTag>;
Expand Down Expand Up @@ -221,13 +209,16 @@ class TestAppSession {

class TestSyncManager {
public:
using LoggerLevel = realm::util::Logger::Level;
static LoggerLevel default_log_level();

struct Config {
Config() {}
realm::app::App::Config app_config;
std::string base_path;
realm::SyncManager::MetadataMode metadata_mode = realm::SyncManager::MetadataMode::NoMetadata;
bool should_teardown_test_directory = true;
realm::util::Logger::Level log_level = realm::util::Logger::Level::TEST_LOGGING_LEVEL;
LoggerLevel log_level = TestSyncManager::default_log_level();
bool override_sync_route = true;
std::shared_ptr<realm::app::GenericNetworkTransport> transport;
bool start_sync_client = true;
Expand Down
28 changes: 11 additions & 17 deletions test/test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,19 +443,22 @@ bool run_tests(const std::shared_ptr<realm::util::Logger>& logger = nullptr)
reporters.push_back(std::make_unique<CustomReporter>(report_progress));
}
if (const char* str = getenv("UNITTEST_XML"); str && strlen(str) != 0) {
std::cout << "Configuring jUnit reporter to store test results in " << str << std::endl;
if (!running_spawned_process)
std::cout << "Configuring jUnit reporter to store test results in " << str << std::endl;
junit_file.open(str, util::File::mode_Write);
const char* test_suite_name = getenv("UNITTEST_SUITE_NAME");
if (!test_suite_name || !strlen(test_suite_name))
test_suite_name = "realm-core-tests";
reporters.push_back(create_junit_reporter(junit_out, test_suite_name));
}
else if (const char* str = getenv("UNITTEST_EVERGREEN_TEST_RESULTS"); str && strlen(str) != 0) {
std::cout << "Configuring evergreen reporter to store test results in " << str << std::endl;
if (!running_spawned_process)
std::cout << "Configuring evergreen reporter to store test results in " << str << std::endl;
reporters.push_back(create_evergreen_reporter(str));
}
auto reporter = create_combined_reporter(reporters);
config.reporter = reporter.get();
if (!running_spawned_process)
config.reporter = reporter.get();

// Set up filter
const char* filter_str = getenv("UNITTEST_FILTER");
Expand All @@ -468,18 +471,8 @@ bool run_tests(const std::shared_ptr<realm::util::Logger>& logger = nullptr)
config.filter = filter.get();

// Set intra test log level threshold
{
const char* str = getenv("UNITTEST_LOG_LEVEL");
if (str && strlen(str) != 0) {
std::istringstream in(str);
in.imbue(std::locale::classic());
in.flags(in.flags() & ~std::ios_base::skipws); // Do not accept white space
in >> config.intra_test_log_level;
bool bad = !in || in.get() != std::char_traits<char>::eof();
if (bad)
throw std::runtime_error("Bad intra test log level");
}
}
if (!util::Logger::get_env_log_level_if_set(config.intra_test_log_level))
throw std::runtime_error("Bad intra test log level");

// Set up per-thread file logging
{
Expand Down Expand Up @@ -510,10 +503,11 @@ bool run_tests(const std::shared_ptr<realm::util::Logger>& logger = nullptr)
list.sort(PatternBasedFileOrder(file_order));
bool success = list.run(config);

if (test_only)
if (test_only && !running_spawned_process)
std::cout << "\n*** BE AWARE THAT MOST TESTS WERE EXCLUDED DUE TO USING 'ONLY' MACRO ***\n";

std::cout << "\n";
if (!running_spawned_process)
std::cout << "\n";

// The iOS Simulator has a separate set of kernel file caches from the parent
// OS, and if the simulator is deleted immediately after running the tests
Expand Down
12 changes: 3 additions & 9 deletions test/util/test_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,11 @@ std::string TestDirNameGenerator::next()

std::shared_ptr<DB> get_test_db(const std::string& path, const char* crypt_key)
{
const char* str = getenv("UNITTEST_LOG_LEVEL");
realm::util::Logger::Level core_log_level = realm::util::Logger::Level::off;
if (str && strlen(str) != 0) {
std::istringstream in(str);
in.imbue(std::locale::classic());
in.flags(in.flags() & ~std::ios_base::skipws); // Do not accept white space
in >> core_log_level;
}
auto log_level = Logger::Level::off;
Logger::get_env_log_level_if_set(log_level);

DBOptions options;
options.logger = std::make_shared<util::StderrLogger>(core_log_level);
options.logger = std::make_shared<util::StderrLogger>(log_level);
options.encryption_key = crypt_key;
return DB::create(make_in_realm_history(), path, options);
}
Expand Down