bitcoin/src/test/util/logging.h
Russell Yanofsky 7918c1b019 test: Add CreateWalletFromFile test
Add unit test calling CreateWalletFromFile, which isn't currently called from
other unit tests, with some basic checks to make sure it rescans and registers
for notifications correctly.

Motivation for this change was to try to write a test that would fail without
the early `handleNotifications` call in ef8c6ca60767cac589d98ca57ee33179608ccda8
from https://github.com/bitcoin/bitcoin/pull/16426, but succeed with it:

ef8c6ca607/src/wallet/wallet.cpp (L3978-L3986)

However, writing a full test for the race condition that call prevents isn't
possible without the locking changes from #16426. So this PR just adds as much
test coverage as is possible now.

This new test is also useful for https://github.com/bitcoin/bitcoin/pull/15719,
since it detects the stale notifications.transactionAddedToMempool notifications
that PR eliminates.
2020-04-26 20:23:05 -04:00

41 lines
1.3 KiB
C++

// 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_UTIL_LOGGING_H
#define BITCOIN_TEST_UTIL_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;
//! Custom match checking function.
//!
//! Invoked with pointers to lines containing matching strings, and with
//! null if check_found() is called without any successful match.
//!
//! Can return true to enable default DebugLogHelper behavior of:
//! (1) ending search after first successful match, and
//! (2) raising an error in check_found if no match was found
//! Can return false to do the opposite in either case.
using MatchFn = std::function<bool(const std::string* line)>;
MatchFn m_match;
void check_found();
public:
DebugLogHelper(std::string message, MatchFn match = [](const std::string*){ return true; });
~DebugLogHelper() { check_found(); }
};
#define ASSERT_DEBUG_LOG(message) DebugLogHelper PASTE2(debugloghelper, __COUNTER__)(message)
#endif // BITCOIN_TEST_UTIL_LOGGING_H