test: Add ASSERT_DEBUG_LOG to unit test framework

This commit is contained in:
MarcoFalke 2019-08-02 17:06:37 -04:00
parent fa1936f57b
commit fa2c44c3cc
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
7 changed files with 97 additions and 25 deletions

View file

@ -58,6 +58,8 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.r
BITCOIN_TEST_SUITE = \
test/lib/blockfilter.cpp \
test/lib/blockfilter.h \
test/lib/logging.cpp \
test/lib/logging.h \
test/lib/transaction_utils.cpp \
test/lib/transaction_utils.h \
test/main.cpp \

View file

@ -66,28 +66,31 @@ void noui_connect()
noui_InitMessageConn = uiInterface.InitMessage_connect(noui_InitMessage);
}
bool noui_ThreadSafeMessageBoxSuppressed(const std::string& message, const std::string& caption, unsigned int style)
bool noui_ThreadSafeMessageBoxRedirect(const std::string& message, const std::string& caption, unsigned int style)
{
LogPrintf("%s: %s\n", caption, message);
return false;
}
bool noui_ThreadSafeQuestionSuppressed(const std::string& /* ignored interactive message */, const std::string& message, const std::string& caption, unsigned int style)
bool noui_ThreadSafeQuestionRedirect(const std::string& /* ignored interactive message */, const std::string& message, const std::string& caption, unsigned int style)
{
LogPrintf("%s: %s\n", caption, message);
return false;
}
void noui_InitMessageSuppressed(const std::string& message)
void noui_InitMessageRedirect(const std::string& message)
{
LogPrintf("init message: %s\n", message);
}
void noui_suppress()
void noui_test_redirect()
{
noui_ThreadSafeMessageBoxConn.disconnect();
noui_ThreadSafeQuestionConn.disconnect();
noui_InitMessageConn.disconnect();
noui_ThreadSafeMessageBoxConn = uiInterface.ThreadSafeMessageBox_connect(noui_ThreadSafeMessageBoxSuppressed);
noui_ThreadSafeQuestionConn = uiInterface.ThreadSafeQuestion_connect(noui_ThreadSafeQuestionSuppressed);
noui_InitMessageConn = uiInterface.InitMessage_connect(noui_InitMessageSuppressed);
noui_ThreadSafeMessageBoxConn = uiInterface.ThreadSafeMessageBox_connect(noui_ThreadSafeMessageBoxRedirect);
noui_ThreadSafeQuestionConn = uiInterface.ThreadSafeQuestion_connect(noui_ThreadSafeQuestionRedirect);
noui_InitMessageConn = uiInterface.InitMessage_connect(noui_InitMessageRedirect);
}
void noui_reconnect()

View file

@ -17,10 +17,10 @@ void noui_InitMessage(const std::string& message);
/** Connect all bitcoind signal handlers */
void noui_connect();
/** Suppress all bitcoind signal handlers. Used to suppress output during test runs that produce expected errors */
void noui_suppress();
/** Redirect all bitcoind signal handlers to LogPrintf. Used to check or suppress output during test runs that produce expected errors */
void noui_test_redirect();
/** Reconnects the regular Non-GUI handlers after having used noui_suppress */
/** Reconnects the regular Non-GUI handlers after having used noui_test_redirect */
void noui_reconnect();
#endif // BITCOIN_NOUI_H

32
src/test/lib/logging.cpp Normal file
View file

@ -0,0 +1,32 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/lib/logging.h>
#include <logging.h>
#include <noui.h>
#include <tinyformat.h>
#include <util/memory.h>
#include <stdexcept>
DebugLogHelper::DebugLogHelper(std::string message)
: m_message{std::move(message)}
{
m_print_connection = LogInstance().PushBackCallback(
[this](const std::string& s) {
if (m_found) return;
m_found = s.find(m_message) != std::string::npos;
});
noui_test_redirect();
}
void DebugLogHelper::check_found()
{
noui_reconnect();
LogInstance().DeleteCallback(m_print_connection);
if (!m_found) {
throw std::runtime_error(strprintf("'%s' not found in debug log\n", m_message));
}
}

29
src/test/lib/logging.h Normal file
View file

@ -0,0 +1,29 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_LIB_LOGGING_H
#define BITCOIN_TEST_LIB_LOGGING_H
#include <util/macros.h>
#include <functional>
#include <list>
#include <string>
class DebugLogHelper
{
const std::string m_message;
bool m_found{false};
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
void check_found();
public:
DebugLogHelper(std::string message);
~DebugLogHelper() { check_found(); }
};
#define ASSERT_DEBUG_LOG(message) DebugLogHelper PASTE2(debugloghelper, __COUNTER__)(message)
#endif // BITCOIN_TEST_LIB_LOGGING_H

View file

@ -5,6 +5,7 @@
#include <netaddress.h>
#include <noui.h>
#include <test/lib/logging.h>
#include <test/setup_common.h>
#include <timedata.h>
#include <warnings.h>
@ -59,9 +60,10 @@ BOOST_AUTO_TEST_CASE(addtimedata)
MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1);
// Filter size is 1 + 3 = 4: It is always initialized with a single element (offset 0)
noui_suppress();
MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
noui_reconnect();
{
ASSERT_DEBUG_LOG("Please check that your computer's date and time are correct!");
MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
}
BOOST_CHECK(GetWarnings("gui").find("clock is wrong") != std::string::npos);

View file

@ -5,6 +5,7 @@
#include <boost/test/unit_test.hpp>
#include <noui.h>
#include <test/lib/logging.h>
#include <test/setup_common.h>
#include <util/system.h>
#include <wallet/test/init_test_fixture.h>
@ -34,28 +35,31 @@ BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_custom)
BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_does_not_exist)
{
SetWalletDir(m_walletdir_path_cases["nonexistent"]);
noui_suppress();
bool result = m_chain_client->verify();
noui_reconnect();
BOOST_CHECK(result == false);
{
ASSERT_DEBUG_LOG("does not exist");
bool result = m_chain_client->verify();
BOOST_CHECK(result == false);
}
}
BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_is_not_directory)
{
SetWalletDir(m_walletdir_path_cases["file"]);
noui_suppress();
bool result = m_chain_client->verify();
noui_reconnect();
BOOST_CHECK(result == false);
{
ASSERT_DEBUG_LOG("is not a directory");
bool result = m_chain_client->verify();
BOOST_CHECK(result == false);
}
}
BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_is_not_relative)
{
SetWalletDir(m_walletdir_path_cases["relative"]);
noui_suppress();
bool result = m_chain_client->verify();
noui_reconnect();
BOOST_CHECK(result == false);
{
ASSERT_DEBUG_LOG("is a relative path");
bool result = m_chain_client->verify();
BOOST_CHECK(result == false);
}
}
BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing)