Skip to content

Commit 51598b2

Browse files
committed
Reinitialize state in between individual unit tests.
This changes the TestingSetup fixture to be per-unit-test rather than global. Most tests don't need it, so it's only invoked in a few.
1 parent 86eb461 commit 51598b2

File tree

14 files changed

+124
-61
lines changed

14 files changed

+124
-61
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ BITCOIN_TESTS =\
6565
test/sigopcount_tests.cpp \
6666
test/skiplist_tests.cpp \
6767
test/test_bitcoin.cpp \
68+
test/test_bitcoin.h \
6869
test/timedata_tests.cpp \
6970
test/transaction_tests.cpp \
7071
test/uint256_tests.cpp \

src/db.cpp

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,31 @@ void CDBEnv::EnvShutdown()
3939
return;
4040

4141
fDbEnvInit = false;
42-
int ret = dbenv.close(0);
42+
int ret = dbenv->close(0);
4343
if (ret != 0)
4444
LogPrintf("CDBEnv::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
4545
if (!fMockDb)
4646
DbEnv(0).remove(path.string().c_str(), 0);
4747
}
4848

49-
CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
49+
void CDBEnv::Reset()
5050
{
51+
delete dbenv;
52+
dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
5153
fDbEnvInit = false;
5254
fMockDb = false;
5355
}
5456

57+
CDBEnv::CDBEnv() : dbenv(NULL)
58+
{
59+
Reset();
60+
}
61+
5562
CDBEnv::~CDBEnv()
5663
{
5764
EnvShutdown();
65+
delete dbenv;
66+
dbenv = NULL;
5867
}
5968

6069
void CDBEnv::Close()
@@ -79,17 +88,17 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn)
7988
if (GetBoolArg("-privdb", true))
8089
nEnvFlags |= DB_PRIVATE;
8190

82-
dbenv.set_lg_dir(pathLogDir.string().c_str());
83-
dbenv.set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
84-
dbenv.set_lg_bsize(0x10000);
85-
dbenv.set_lg_max(1048576);
86-
dbenv.set_lk_max_locks(40000);
87-
dbenv.set_lk_max_objects(40000);
88-
dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
89-
dbenv.set_flags(DB_AUTO_COMMIT, 1);
90-
dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
91-
dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
92-
int ret = dbenv.open(path.string().c_str(),
91+
dbenv->set_lg_dir(pathLogDir.string().c_str());
92+
dbenv->set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
93+
dbenv->set_lg_bsize(0x10000);
94+
dbenv->set_lg_max(1048576);
95+
dbenv->set_lk_max_locks(40000);
96+
dbenv->set_lk_max_objects(40000);
97+
dbenv->set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
98+
dbenv->set_flags(DB_AUTO_COMMIT, 1);
99+
dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
100+
dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
101+
int ret = dbenv->open(path.string().c_str(),
93102
DB_CREATE |
94103
DB_INIT_LOCK |
95104
DB_INIT_LOG |
@@ -116,14 +125,14 @@ void CDBEnv::MakeMock()
116125

117126
LogPrint("db", "CDBEnv::MakeMock\n");
118127

119-
dbenv.set_cachesize(1, 0, 1);
120-
dbenv.set_lg_bsize(10485760 * 4);
121-
dbenv.set_lg_max(10485760);
122-
dbenv.set_lk_max_locks(10000);
123-
dbenv.set_lk_max_objects(10000);
124-
dbenv.set_flags(DB_AUTO_COMMIT, 1);
125-
dbenv.log_set_config(DB_LOG_IN_MEMORY, 1);
126-
int ret = dbenv.open(NULL,
128+
dbenv->set_cachesize(1, 0, 1);
129+
dbenv->set_lg_bsize(10485760 * 4);
130+
dbenv->set_lg_max(10485760);
131+
dbenv->set_lk_max_locks(10000);
132+
dbenv->set_lk_max_objects(10000);
133+
dbenv->set_flags(DB_AUTO_COMMIT, 1);
134+
dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
135+
int ret = dbenv->open(NULL,
127136
DB_CREATE |
128137
DB_INIT_LOCK |
129138
DB_INIT_LOG |
@@ -144,7 +153,7 @@ CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool (*recoverFunc)(CDB
144153
LOCK(cs_db);
145154
assert(mapFileUseCount.count(strFile) == 0);
146155

147-
Db db(&dbenv, 0);
156+
Db db(dbenv, 0);
148157
int result = db.verify(strFile.c_str(), NULL, NULL, 0);
149158
if (result == 0)
150159
return VERIFY_OK;
@@ -167,7 +176,7 @@ bool CDBEnv::Salvage(std::string strFile, bool fAggressive, std::vector<CDBEnv::
167176

168177
stringstream strDump;
169178

170-
Db db(&dbenv, 0);
179+
Db db(dbenv, 0);
171180
int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
172181
if (result == DB_VERIFY_BAD) {
173182
LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n");
@@ -208,10 +217,10 @@ bool CDBEnv::Salvage(std::string strFile, bool fAggressive, std::vector<CDBEnv::
208217

209218
void CDBEnv::CheckpointLSN(const std::string& strFile)
210219
{
211-
dbenv.txn_checkpoint(0, 0, 0);
220+
dbenv->txn_checkpoint(0, 0, 0);
212221
if (fMockDb)
213222
return;
214-
dbenv.lsn_reset(strFile.c_str(), 0);
223+
dbenv->lsn_reset(strFile.c_str(), 0);
215224
}
216225

217226

@@ -237,7 +246,7 @@ CDB::CDB(const std::string& strFilename, const char* pszMode, bool fFlushOnClose
237246
++bitdb.mapFileUseCount[strFile];
238247
pdb = bitdb.mapDb[strFile];
239248
if (pdb == NULL) {
240-
pdb = new Db(&bitdb.dbenv, 0);
249+
pdb = new Db(bitdb.dbenv, 0);
241250

242251
bool fMockDb = bitdb.IsMock();
243252
if (fMockDb) {
@@ -284,7 +293,7 @@ void CDB::Flush()
284293
if (fReadOnly)
285294
nMinutes = 1;
286295

287-
bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
296+
bitdb.dbenv->txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
288297
}
289298

290299
void CDB::Close()
@@ -324,7 +333,7 @@ bool CDBEnv::RemoveDb(const string& strFile)
324333
this->CloseDb(strFile);
325334

326335
LOCK(cs_db);
327-
int rc = dbenv.dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
336+
int rc = dbenv->dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
328337
return (rc == 0);
329338
}
330339

@@ -344,7 +353,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
344353
string strFileRes = strFile + ".rewrite";
345354
{ // surround usage of db with extra {}
346355
CDB db(strFile.c_str(), "r");
347-
Db* pdbCopy = new Db(&bitdb.dbenv, 0);
356+
Db* pdbCopy = new Db(bitdb.dbenv, 0);
348357

349358
int ret = pdbCopy->open(NULL, // Txn pointer
350359
strFileRes.c_str(), // Filename
@@ -394,10 +403,10 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
394403
}
395404
}
396405
if (fSuccess) {
397-
Db dbA(&bitdb.dbenv, 0);
406+
Db dbA(bitdb.dbenv, 0);
398407
if (dbA.remove(strFile.c_str(), NULL, 0))
399408
fSuccess = false;
400-
Db dbB(&bitdb.dbenv, 0);
409+
Db dbB(bitdb.dbenv, 0);
401410
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
402411
fSuccess = false;
403412
}
@@ -430,10 +439,10 @@ void CDBEnv::Flush(bool fShutdown)
430439
// Move log data to the dat file
431440
CloseDb(strFile);
432441
LogPrint("db", "CDBEnv::Flush: %s checkpoint\n", strFile);
433-
dbenv.txn_checkpoint(0, 0, 0);
442+
dbenv->txn_checkpoint(0, 0, 0);
434443
LogPrint("db", "CDBEnv::Flush: %s detach\n", strFile);
435444
if (!fMockDb)
436-
dbenv.lsn_reset(strFile.c_str(), 0);
445+
dbenv->lsn_reset(strFile.c_str(), 0);
437446
LogPrint("db", "CDBEnv::Flush: %s closed\n", strFile);
438447
mapFileUseCount.erase(mi++);
439448
} else
@@ -443,7 +452,7 @@ void CDBEnv::Flush(bool fShutdown)
443452
if (fShutdown) {
444453
char** listp;
445454
if (mapFileUseCount.empty()) {
446-
dbenv.log_archive(&listp, DB_ARCH_REMOVE);
455+
dbenv->log_archive(&listp, DB_ARCH_REMOVE);
447456
Close();
448457
if (!fMockDb)
449458
boost::filesystem::remove_all(path / "database");

src/db.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ class CDBEnv
3939

4040
public:
4141
mutable CCriticalSection cs_db;
42-
DbEnv dbenv;
42+
DbEnv *dbenv;
4343
std::map<std::string, int> mapFileUseCount;
4444
std::map<std::string, Db*> mapDb;
4545

4646
CDBEnv();
4747
~CDBEnv();
48+
void Reset();
49+
4850
void MakeMock();
4951
bool IsMock() { return fMockDb; }
5052

@@ -79,7 +81,7 @@ class CDBEnv
7981
DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
8082
{
8183
DbTxn* ptxn = NULL;
82-
int ret = dbenv.txn_begin(NULL, &ptxn, flags);
84+
int ret = dbenv->txn_begin(NULL, &ptxn, flags);
8385
if (!ptxn || ret != 0)
8486
return NULL;
8587
return ptxn;

src/main.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ bool fTxIndex = false;
5454
bool fIsBareMultisigStd = true;
5555
unsigned int nCoinCacheSize = 5000;
5656

57-
5857
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
5958
CFeeRate minRelayTxFee = CFeeRate(1000);
6059

@@ -3085,10 +3084,31 @@ bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth
30853084

30863085
void UnloadBlockIndex()
30873086
{
3088-
mapBlockIndex.clear();
3087+
LOCK(cs_main);
30893088
setBlockIndexCandidates.clear();
30903089
chainActive.SetTip(NULL);
30913090
pindexBestInvalid = NULL;
3091+
pindexBestHeader = NULL;
3092+
mempool.clear();
3093+
mapOrphanTransactions.clear();
3094+
mapOrphanTransactionsByPrev.clear();
3095+
nSyncStarted = 0;
3096+
mapBlocksUnlinked.clear();
3097+
vinfoBlockFile.clear();
3098+
nLastBlockFile = 0;
3099+
nBlockSequenceId = 1;
3100+
mapBlockSource.clear();
3101+
mapBlocksInFlight.clear();
3102+
nQueuedValidatedHeaders = 0;
3103+
nPreferredDownload = 0;
3104+
setDirtyBlockIndex.clear();
3105+
setDirtyFileInfo.clear();
3106+
mapNodeState.clear();
3107+
3108+
BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
3109+
delete entry.second;
3110+
}
3111+
mapBlockIndex.clear();
30923112
}
30933113

30943114
bool LoadBlockIndex()

src/test/DoS_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "serialize.h"
1717
#include "util.h"
1818

19+
#include "test/test_bitcoin.h"
20+
1921
#include <stdint.h>
2022

2123
#include <boost/assign/list_of.hpp> // for 'map_list_of()'
@@ -41,7 +43,7 @@ CService ip(uint32_t i)
4143
return CService(CNetAddr(s), Params().GetDefaultPort());
4244
}
4345

44-
BOOST_AUTO_TEST_SUITE(DoS_tests)
46+
BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
4547

4648
BOOST_AUTO_TEST_CASE(DoS_banning)
4749
{

src/test/accounting_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#include "wallet.h"
66
#include "walletdb.h"
77

8+
#include "test/test_bitcoin.h"
9+
810
#include <stdint.h>
911

1012
#include <boost/foreach.hpp>
1113
#include <boost/test/unit_test.hpp>
1214

1315
extern CWallet* pwalletMain;
1416

15-
BOOST_AUTO_TEST_SUITE(accounting_tests)
17+
BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
1618

1719
static void
1820
GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)

src/test/alert_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "util.h"
1616
#include "utilstrencodings.h"
1717

18+
#include "test/test_bitcoin.h"
19+
1820
#include <fstream>
1921

2022
#include <boost/filesystem/operations.hpp>
@@ -78,7 +80,7 @@
7880
}
7981
#endif
8082

81-
struct ReadAlerts
83+
struct ReadAlerts : public TestingSetup
8284
{
8385
ReadAlerts()
8486
{

src/test/miner_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#include "uint256.h"
99
#include "util.h"
1010

11+
#include "test/test_bitcoin.h"
12+
1113
#include <boost/test/unit_test.hpp>
1214

13-
BOOST_AUTO_TEST_SUITE(miner_tests)
15+
BOOST_FIXTURE_TEST_SUITE(miner_tests, TestingSetup)
1416

1517
static
1618
struct {

src/test/rpc_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "base58.h"
99
#include "netbase.h"
1010

11+
#include "test/test_bitcoin.h"
12+
1113
#include <boost/algorithm/string.hpp>
1214
#include <boost/test/unit_test.hpp>
1315

@@ -45,7 +47,7 @@ Value CallRPC(string args)
4547
}
4648

4749

48-
BOOST_AUTO_TEST_SUITE(rpc_tests)
50+
BOOST_FIXTURE_TEST_SUITE(rpc_tests, TestingSetup)
4951

5052
BOOST_AUTO_TEST_CASE(rpc_rawparams)
5153
{

src/test/rpc_wallet_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "base58.h"
99
#include "wallet.h"
1010

11+
#include "test/test_bitcoin.h"
12+
1113
#include <boost/algorithm/string.hpp>
1214
#include <boost/test/unit_test.hpp>
1315

@@ -19,7 +21,7 @@ extern Value CallRPC(string args);
1921

2022
extern CWallet* pwalletMain;
2123

22-
BOOST_AUTO_TEST_SUITE(rpc_wallet_tests)
24+
BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
2325

2426
BOOST_AUTO_TEST_CASE(rpc_addmultisig)
2527
{

0 commit comments

Comments
 (0)