Skip to content

Commit c52776e

Browse files
committed
Merge #16252: test: Log to debug.log in all unit tests
fabc57e test: Log to debug.log in all tests (MarcoFalke) fa4a04a test: use common setup in gui tests (MarcoFalke) fad3d2a test: Create data dir in BasicTestingSetup (MarcoFalke) Pull request description: This makes it easier to debug a frozen test or a test that failed. To debug a failed test, remove the line `fs::remove_all(m_path_root);`. The pull is done in three commits: * Create a datadir for every unit test once (and only once). This requires the `SetDataDir` function to go away. * Use the common setup in the gui unit tests. Some of those tests are testing the init sequence, so we'd have to undo some of what the testing setup did. * Log to the debug.log in all tests ACKs for top commit: laanwj: ACK fabc57e Tree-SHA512: 73444210b88172669e2cd22c2703a1e30e105185d2d5f03decbdedcfd09c64ed208d3716c59c8bebb0e44214cee5c8095e3e995d049e1572ee98f1017e413665
2 parents bc948d4 + fabc57e commit c52776e

16 files changed

+52
-54
lines changed

src/logging.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ bool BCLog::Logger::StartLogging()
7575
return true;
7676
}
7777

78+
void BCLog::Logger::DisconnectTestLogger()
79+
{
80+
std::lock_guard<std::mutex> scoped_lock(m_cs);
81+
m_buffering = true;
82+
if (m_fileout != nullptr) fclose(m_fileout);
83+
m_fileout = nullptr;
84+
}
85+
7886
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
7987
{
8088
m_categories |= flag;

src/logging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ namespace BCLog {
100100

101101
/** Start logging (and flush all buffered messages) */
102102
bool StartLogging();
103+
/** Only for testing */
104+
void DisconnectTestLogger();
103105

104106
void ShrinkDebugFile();
105107

src/qt/test/apptests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qt/test/apptests.h>
66

77
#include <chainparams.h>
8+
#include <key.h>
89
#include <qt/bitcoin.h>
910
#include <qt/bitcoingui.h>
1011
#include <qt/networkstyle.h>
@@ -62,6 +63,9 @@ void AppTests::appTests()
6263
}
6364
#endif
6465

66+
ECC_Stop(); // Already started by the common test setup, so stop it to avoid interference
67+
LogInstance().DisconnectTestLogger();
68+
6569
m_app.parameterSetup();
6670
m_app.createOptionsModel(true /* reset settings */);
6771
QScopedPointer<const NetworkStyle> style(

src/qt/test/rpcnestedtests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void RPCNestedTests::rpcNestedTests()
3535
tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
3636
//mempool.setSanityCheck(1.0);
3737

38+
LogInstance().DisconnectTestLogger(); // Already started by the common test setup, so stop it to avoid interference
3839
TestingSetup test;
3940

4041
if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();

src/qt/test/test_main.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include <qt/bitcoin.h>
1212
#include <qt/test/apptests.h>
1313
#include <qt/test/rpcnestedtests.h>
14-
#include <util/system.h>
1514
#include <qt/test/uritests.h>
1615
#include <qt/test/compattests.h>
16+
#include <test/setup_common.h>
1717

1818
#ifdef ENABLE_WALLET
1919
#include <qt/test/addressbooktests.h>
@@ -48,14 +48,8 @@ extern void noui_connect();
4848
// This is all you need to run all the tests
4949
int main(int argc, char *argv[])
5050
{
51-
SetupEnvironment();
52-
SetupNetworking();
53-
SelectParams(CBaseChainParams::REGTEST);
54-
noui_connect();
55-
ClearDatadirCache();
56-
fs::path pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin-qt_%lu_%i", (unsigned long)GetTime(), (int)GetRand(100000));
57-
fs::create_directories(pathTemp);
58-
gArgs.ForceSetArg("-datadir", pathTemp.string());
51+
BasicTestingSetup test{CBaseChainParams::REGTEST};
52+
5953
auto node = interfaces::MakeNode();
6054

6155
bool fInvalid = false;
@@ -109,7 +103,5 @@ int main(int argc, char *argv[])
109103
}
110104
#endif
111105

112-
fs::remove_all(pathTemp);
113-
114106
return fInvalid;
115107
}

src/test/blockfilter_index_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
267267

268268
BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
269269
{
270-
SetDataDir("tempdir");
271-
272270
BlockFilterIndex* filter_index;
273271

274272
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);

src/test/dbwrapper_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
2727
{
2828
// Perform tests both obfuscated and non-obfuscated.
2929
for (const bool obfuscate : {false, true}) {
30-
fs::path ph = SetDataDir(std::string("dbwrapper").append(obfuscate ? "_true" : "_false"));
30+
fs::path ph = GetDataDir() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false");
3131
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
3232
char key = 'k';
3333
uint256 in = InsecureRand256();
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
4747
{
4848
// Perform tests both obfuscated and non-obfuscated.
4949
for (const bool obfuscate : {false, true}) {
50-
fs::path ph = SetDataDir(std::string("dbwrapper_batch").append(obfuscate ? "_true" : "_false"));
50+
fs::path ph = GetDataDir() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
5151
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
5252

5353
char key = 'i';
@@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
8383
{
8484
// Perform tests both obfuscated and non-obfuscated.
8585
for (const bool obfuscate : {false, true}) {
86-
fs::path ph = SetDataDir(std::string("dbwrapper_iterator").append(obfuscate ? "_true" : "_false"));
86+
fs::path ph = GetDataDir() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
8787
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
8888

8989
// The two keys are intentionally chosen for ordering
@@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
123123
BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
124124
{
125125
// We're going to share this fs::path between two wrappers
126-
fs::path ph = SetDataDir("existing_data_no_obfuscate");
126+
fs::path ph = GetDataDir() / "existing_data_no_obfuscate";
127127
create_directories(ph);
128128

129129
// Set up a non-obfuscated wrapper to write some initial data.
@@ -164,7 +164,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
164164
BOOST_AUTO_TEST_CASE(existing_data_reindex)
165165
{
166166
// We're going to share this fs::path between two wrappers
167-
fs::path ph = SetDataDir("existing_data_reindex");
167+
fs::path ph = GetDataDir() / "existing_data_reindex";
168168
create_directories(ph);
169169

170170
// Set up a non-obfuscated wrapper to write some initial data.
@@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
199199

200200
BOOST_AUTO_TEST_CASE(iterator_ordering)
201201
{
202-
fs::path ph = SetDataDir("iterator_ordering");
202+
fs::path ph = GetDataDir() / "iterator_ordering";
203203
CDBWrapper dbw(ph, (1 << 20), true, false, false);
204204
for (int x=0x00; x<256; ++x) {
205205
uint8_t key = x;
@@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
277277
{
278278
char buf[10];
279279

280-
fs::path ph = SetDataDir("iterator_string_ordering");
280+
fs::path ph = GetDataDir() / "iterator_string_ordering";
281281
CDBWrapper dbw(ph, (1 << 20), true, false, false);
282282
for (int x=0x00; x<10; ++x) {
283283
for (int y = 0; y < 10; y++) {

src/test/flatfile_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ BOOST_FIXTURE_TEST_SUITE(flatfile_tests, BasicTestingSetup)
1111

1212
BOOST_AUTO_TEST_CASE(flatfile_filename)
1313
{
14-
auto data_dir = SetDataDir("flatfile_test");
14+
const auto data_dir = GetDataDir();
1515

1616
FlatFilePos pos(456, 789);
1717

@@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(flatfile_filename)
2424

2525
BOOST_AUTO_TEST_CASE(flatfile_open)
2626
{
27-
auto data_dir = SetDataDir("flatfile_test");
27+
const auto data_dir = GetDataDir();
2828
FlatFileSeq seq(data_dir, "a", 16 * 1024);
2929

3030
std::string line1("A purely peer-to-peer version of electronic cash would allow online "
@@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
8585

8686
BOOST_AUTO_TEST_CASE(flatfile_allocate)
8787
{
88-
auto data_dir = SetDataDir("flatfile_test");
88+
const auto data_dir = GetDataDir();
8989
FlatFileSeq seq(data_dir, "a", 100);
9090

9191
bool out_of_space;
@@ -105,7 +105,7 @@ BOOST_AUTO_TEST_CASE(flatfile_allocate)
105105

106106
BOOST_AUTO_TEST_CASE(flatfile_flush)
107107
{
108-
auto data_dir = SetDataDir("flatfile_test");
108+
const auto data_dir = GetDataDir();
109109
FlatFileSeq seq(data_dir, "a", 100);
110110

111111
bool out_of_space;

src/test/fs_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
1111

1212
BOOST_AUTO_TEST_CASE(fsbridge_fstream)
1313
{
14-
fs::path tmpfolder = SetDataDir("fsbridge_fstream");
14+
fs::path tmpfolder = GetDataDir();
1515
// tmpfile1 should be the same as tmpfile2
1616
fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
1717
fs::path tmpfile2 = tmpfolder / L"fs_tests_₿_🏃";

src/test/net_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ BOOST_AUTO_TEST_CASE(cnode_listen_port)
8989

9090
BOOST_AUTO_TEST_CASE(caddrdb_read)
9191
{
92-
SetDataDir("caddrdb_read");
9392
CAddrManUncorrupted addrmanUncorrupted;
9493
addrmanUncorrupted.MakeDeterministic();
9594

@@ -135,7 +134,6 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
135134

136135
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
137136
{
138-
SetDataDir("caddrdb_read_corrupted");
139137
CAddrManCorrupted addrmanCorrupted;
140138
addrmanCorrupted.MakeDeterministic();
141139

0 commit comments

Comments
 (0)