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.
This commit is contained in:
Pieter Wuille 2015-03-03 07:49:12 -08:00
parent 86eb461c5b
commit 51598b2631
14 changed files with 124 additions and 61 deletions

View file

@ -65,6 +65,7 @@ BITCOIN_TESTS =\
test/sigopcount_tests.cpp \ test/sigopcount_tests.cpp \
test/skiplist_tests.cpp \ test/skiplist_tests.cpp \
test/test_bitcoin.cpp \ test/test_bitcoin.cpp \
test/test_bitcoin.h \
test/timedata_tests.cpp \ test/timedata_tests.cpp \
test/transaction_tests.cpp \ test/transaction_tests.cpp \
test/uint256_tests.cpp \ test/uint256_tests.cpp \

View file

@ -39,22 +39,31 @@ void CDBEnv::EnvShutdown()
return; return;
fDbEnvInit = false; fDbEnvInit = false;
int ret = dbenv.close(0); int ret = dbenv->close(0);
if (ret != 0) if (ret != 0)
LogPrintf("CDBEnv::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret)); LogPrintf("CDBEnv::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
if (!fMockDb) if (!fMockDb)
DbEnv(0).remove(path.string().c_str(), 0); DbEnv(0).remove(path.string().c_str(), 0);
} }
CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS) void CDBEnv::Reset()
{ {
delete dbenv;
dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
fDbEnvInit = false; fDbEnvInit = false;
fMockDb = false; fMockDb = false;
} }
CDBEnv::CDBEnv() : dbenv(NULL)
{
Reset();
}
CDBEnv::~CDBEnv() CDBEnv::~CDBEnv()
{ {
EnvShutdown(); EnvShutdown();
delete dbenv;
dbenv = NULL;
} }
void CDBEnv::Close() void CDBEnv::Close()
@ -79,17 +88,17 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn)
if (GetBoolArg("-privdb", true)) if (GetBoolArg("-privdb", true))
nEnvFlags |= DB_PRIVATE; nEnvFlags |= DB_PRIVATE;
dbenv.set_lg_dir(pathLogDir.string().c_str()); dbenv->set_lg_dir(pathLogDir.string().c_str());
dbenv.set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet dbenv->set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
dbenv.set_lg_bsize(0x10000); dbenv->set_lg_bsize(0x10000);
dbenv.set_lg_max(1048576); dbenv->set_lg_max(1048576);
dbenv.set_lk_max_locks(40000); dbenv->set_lk_max_locks(40000);
dbenv.set_lk_max_objects(40000); dbenv->set_lk_max_objects(40000);
dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug dbenv->set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
dbenv.set_flags(DB_AUTO_COMMIT, 1); dbenv->set_flags(DB_AUTO_COMMIT, 1);
dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1); dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
int ret = dbenv.open(path.string().c_str(), int ret = dbenv->open(path.string().c_str(),
DB_CREATE | DB_CREATE |
DB_INIT_LOCK | DB_INIT_LOCK |
DB_INIT_LOG | DB_INIT_LOG |
@ -116,14 +125,14 @@ void CDBEnv::MakeMock()
LogPrint("db", "CDBEnv::MakeMock\n"); LogPrint("db", "CDBEnv::MakeMock\n");
dbenv.set_cachesize(1, 0, 1); dbenv->set_cachesize(1, 0, 1);
dbenv.set_lg_bsize(10485760 * 4); dbenv->set_lg_bsize(10485760 * 4);
dbenv.set_lg_max(10485760); dbenv->set_lg_max(10485760);
dbenv.set_lk_max_locks(10000); dbenv->set_lk_max_locks(10000);
dbenv.set_lk_max_objects(10000); dbenv->set_lk_max_objects(10000);
dbenv.set_flags(DB_AUTO_COMMIT, 1); dbenv->set_flags(DB_AUTO_COMMIT, 1);
dbenv.log_set_config(DB_LOG_IN_MEMORY, 1); dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
int ret = dbenv.open(NULL, int ret = dbenv->open(NULL,
DB_CREATE | DB_CREATE |
DB_INIT_LOCK | DB_INIT_LOCK |
DB_INIT_LOG | DB_INIT_LOG |
@ -144,7 +153,7 @@ CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool (*recoverFunc)(CDB
LOCK(cs_db); LOCK(cs_db);
assert(mapFileUseCount.count(strFile) == 0); assert(mapFileUseCount.count(strFile) == 0);
Db db(&dbenv, 0); Db db(dbenv, 0);
int result = db.verify(strFile.c_str(), NULL, NULL, 0); int result = db.verify(strFile.c_str(), NULL, NULL, 0);
if (result == 0) if (result == 0)
return VERIFY_OK; return VERIFY_OK;
@ -167,7 +176,7 @@ bool CDBEnv::Salvage(std::string strFile, bool fAggressive, std::vector<CDBEnv::
stringstream strDump; stringstream strDump;
Db db(&dbenv, 0); Db db(dbenv, 0);
int result = db.verify(strFile.c_str(), NULL, &strDump, flags); int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
if (result == DB_VERIFY_BAD) { if (result == DB_VERIFY_BAD) {
LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n"); 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::
void CDBEnv::CheckpointLSN(const std::string& strFile) void CDBEnv::CheckpointLSN(const std::string& strFile)
{ {
dbenv.txn_checkpoint(0, 0, 0); dbenv->txn_checkpoint(0, 0, 0);
if (fMockDb) if (fMockDb)
return; return;
dbenv.lsn_reset(strFile.c_str(), 0); dbenv->lsn_reset(strFile.c_str(), 0);
} }
@ -237,7 +246,7 @@ CDB::CDB(const std::string& strFilename, const char* pszMode, bool fFlushOnClose
++bitdb.mapFileUseCount[strFile]; ++bitdb.mapFileUseCount[strFile];
pdb = bitdb.mapDb[strFile]; pdb = bitdb.mapDb[strFile];
if (pdb == NULL) { if (pdb == NULL) {
pdb = new Db(&bitdb.dbenv, 0); pdb = new Db(bitdb.dbenv, 0);
bool fMockDb = bitdb.IsMock(); bool fMockDb = bitdb.IsMock();
if (fMockDb) { if (fMockDb) {
@ -284,7 +293,7 @@ void CDB::Flush()
if (fReadOnly) if (fReadOnly)
nMinutes = 1; nMinutes = 1;
bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0); bitdb.dbenv->txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
} }
void CDB::Close() void CDB::Close()
@ -324,7 +333,7 @@ bool CDBEnv::RemoveDb(const string& strFile)
this->CloseDb(strFile); this->CloseDb(strFile);
LOCK(cs_db); LOCK(cs_db);
int rc = dbenv.dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT); int rc = dbenv->dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
return (rc == 0); return (rc == 0);
} }
@ -344,7 +353,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
string strFileRes = strFile + ".rewrite"; string strFileRes = strFile + ".rewrite";
{ // surround usage of db with extra {} { // surround usage of db with extra {}
CDB db(strFile.c_str(), "r"); CDB db(strFile.c_str(), "r");
Db* pdbCopy = new Db(&bitdb.dbenv, 0); Db* pdbCopy = new Db(bitdb.dbenv, 0);
int ret = pdbCopy->open(NULL, // Txn pointer int ret = pdbCopy->open(NULL, // Txn pointer
strFileRes.c_str(), // Filename strFileRes.c_str(), // Filename
@ -394,10 +403,10 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
} }
} }
if (fSuccess) { if (fSuccess) {
Db dbA(&bitdb.dbenv, 0); Db dbA(bitdb.dbenv, 0);
if (dbA.remove(strFile.c_str(), NULL, 0)) if (dbA.remove(strFile.c_str(), NULL, 0))
fSuccess = false; fSuccess = false;
Db dbB(&bitdb.dbenv, 0); Db dbB(bitdb.dbenv, 0);
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0)) if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
fSuccess = false; fSuccess = false;
} }
@ -430,10 +439,10 @@ void CDBEnv::Flush(bool fShutdown)
// Move log data to the dat file // Move log data to the dat file
CloseDb(strFile); CloseDb(strFile);
LogPrint("db", "CDBEnv::Flush: %s checkpoint\n", strFile); LogPrint("db", "CDBEnv::Flush: %s checkpoint\n", strFile);
dbenv.txn_checkpoint(0, 0, 0); dbenv->txn_checkpoint(0, 0, 0);
LogPrint("db", "CDBEnv::Flush: %s detach\n", strFile); LogPrint("db", "CDBEnv::Flush: %s detach\n", strFile);
if (!fMockDb) if (!fMockDb)
dbenv.lsn_reset(strFile.c_str(), 0); dbenv->lsn_reset(strFile.c_str(), 0);
LogPrint("db", "CDBEnv::Flush: %s closed\n", strFile); LogPrint("db", "CDBEnv::Flush: %s closed\n", strFile);
mapFileUseCount.erase(mi++); mapFileUseCount.erase(mi++);
} else } else
@ -443,7 +452,7 @@ void CDBEnv::Flush(bool fShutdown)
if (fShutdown) { if (fShutdown) {
char** listp; char** listp;
if (mapFileUseCount.empty()) { if (mapFileUseCount.empty()) {
dbenv.log_archive(&listp, DB_ARCH_REMOVE); dbenv->log_archive(&listp, DB_ARCH_REMOVE);
Close(); Close();
if (!fMockDb) if (!fMockDb)
boost::filesystem::remove_all(path / "database"); boost::filesystem::remove_all(path / "database");

View file

@ -39,12 +39,14 @@ private:
public: public:
mutable CCriticalSection cs_db; mutable CCriticalSection cs_db;
DbEnv dbenv; DbEnv *dbenv;
std::map<std::string, int> mapFileUseCount; std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb; std::map<std::string, Db*> mapDb;
CDBEnv(); CDBEnv();
~CDBEnv(); ~CDBEnv();
void Reset();
void MakeMock(); void MakeMock();
bool IsMock() { return fMockDb; } bool IsMock() { return fMockDb; }
@ -79,7 +81,7 @@ public:
DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC) DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
{ {
DbTxn* ptxn = NULL; DbTxn* ptxn = NULL;
int ret = dbenv.txn_begin(NULL, &ptxn, flags); int ret = dbenv->txn_begin(NULL, &ptxn, flags);
if (!ptxn || ret != 0) if (!ptxn || ret != 0)
return NULL; return NULL;
return ptxn; return ptxn;

View file

@ -54,7 +54,6 @@ bool fTxIndex = false;
bool fIsBareMultisigStd = true; bool fIsBareMultisigStd = true;
unsigned int nCoinCacheSize = 5000; unsigned int nCoinCacheSize = 5000;
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */ /** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
CFeeRate minRelayTxFee = CFeeRate(1000); CFeeRate minRelayTxFee = CFeeRate(1000);
@ -3085,10 +3084,31 @@ bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth
void UnloadBlockIndex() void UnloadBlockIndex()
{ {
mapBlockIndex.clear(); LOCK(cs_main);
setBlockIndexCandidates.clear(); setBlockIndexCandidates.clear();
chainActive.SetTip(NULL); chainActive.SetTip(NULL);
pindexBestInvalid = NULL; pindexBestInvalid = NULL;
pindexBestHeader = NULL;
mempool.clear();
mapOrphanTransactions.clear();
mapOrphanTransactionsByPrev.clear();
nSyncStarted = 0;
mapBlocksUnlinked.clear();
vinfoBlockFile.clear();
nLastBlockFile = 0;
nBlockSequenceId = 1;
mapBlockSource.clear();
mapBlocksInFlight.clear();
nQueuedValidatedHeaders = 0;
nPreferredDownload = 0;
setDirtyBlockIndex.clear();
setDirtyFileInfo.clear();
mapNodeState.clear();
BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
delete entry.second;
}
mapBlockIndex.clear();
} }
bool LoadBlockIndex() bool LoadBlockIndex()

View file

@ -16,6 +16,8 @@
#include "serialize.h" #include "serialize.h"
#include "util.h" #include "util.h"
#include "test/test_bitcoin.h"
#include <stdint.h> #include <stdint.h>
#include <boost/assign/list_of.hpp> // for 'map_list_of()' #include <boost/assign/list_of.hpp> // for 'map_list_of()'
@ -41,7 +43,7 @@ CService ip(uint32_t i)
return CService(CNetAddr(s), Params().GetDefaultPort()); return CService(CNetAddr(s), Params().GetDefaultPort());
} }
BOOST_AUTO_TEST_SUITE(DoS_tests) BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(DoS_banning) BOOST_AUTO_TEST_CASE(DoS_banning)
{ {

View file

@ -5,6 +5,8 @@
#include "wallet.h" #include "wallet.h"
#include "walletdb.h" #include "walletdb.h"
#include "test/test_bitcoin.h"
#include <stdint.h> #include <stdint.h>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -12,7 +14,7 @@
extern CWallet* pwalletMain; extern CWallet* pwalletMain;
BOOST_AUTO_TEST_SUITE(accounting_tests) BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
static void static void
GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results) GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)

View file

@ -15,6 +15,8 @@
#include "util.h" #include "util.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include "test/test_bitcoin.h"
#include <fstream> #include <fstream>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
@ -78,7 +80,7 @@
} }
#endif #endif
struct ReadAlerts struct ReadAlerts : public TestingSetup
{ {
ReadAlerts() ReadAlerts()
{ {

View file

@ -8,9 +8,11 @@
#include "uint256.h" #include "uint256.h"
#include "util.h" #include "util.h"
#include "test/test_bitcoin.h"
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(miner_tests) BOOST_FIXTURE_TEST_SUITE(miner_tests, TestingSetup)
static static
struct { struct {

View file

@ -8,6 +8,8 @@
#include "base58.h" #include "base58.h"
#include "netbase.h" #include "netbase.h"
#include "test/test_bitcoin.h"
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -45,7 +47,7 @@ Value CallRPC(string args)
} }
BOOST_AUTO_TEST_SUITE(rpc_tests) BOOST_FIXTURE_TEST_SUITE(rpc_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(rpc_rawparams) BOOST_AUTO_TEST_CASE(rpc_rawparams)
{ {

View file

@ -8,6 +8,8 @@
#include "base58.h" #include "base58.h"
#include "wallet.h" #include "wallet.h"
#include "test/test_bitcoin.h"
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -19,7 +21,7 @@ extern Value CallRPC(string args);
extern CWallet* pwalletMain; extern CWallet* pwalletMain;
BOOST_AUTO_TEST_SUITE(rpc_wallet_tests) BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(rpc_addmultisig) BOOST_AUTO_TEST_CASE(rpc_addmultisig)
{ {

View file

@ -4,6 +4,8 @@
#define BOOST_TEST_MODULE Bitcoin Test Suite #define BOOST_TEST_MODULE Bitcoin Test Suite
#include "test_bitcoin.h"
#include "main.h" #include "main.h"
#include "random.h" #include "random.h"
#include "txdb.h" #include "txdb.h"
@ -24,18 +26,15 @@ CWallet* pwalletMain;
extern bool fPrintToConsole; extern bool fPrintToConsole;
extern void noui_connect(); extern void noui_connect();
struct TestingSetup { TestingSetup::TestingSetup()
CCoinsViewDB *pcoinsdbview; {
boost::filesystem::path pathTemp;
boost::thread_group threadGroup;
TestingSetup() {
fPrintToDebugLog = false; // don't want to write to debug.log file fPrintToDebugLog = false; // don't want to write to debug.log file
SelectParams(CBaseChainParams::UNITTEST); SelectParams(CBaseChainParams::UNITTEST);
noui_connect(); noui_connect();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
bitdb.MakeMock(); bitdb.MakeMock();
#endif #endif
ClearDatadirCache();
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000))); pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
boost::filesystem::create_directories(pathTemp); boost::filesystem::create_directories(pathTemp);
mapArgs["-datadir"] = pathTemp.string(); mapArgs["-datadir"] = pathTemp.string();
@ -53,27 +52,28 @@ struct TestingSetup {
for (int i=0; i < nScriptCheckThreads-1; i++) for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck); threadGroup.create_thread(&ThreadScriptCheck);
RegisterNodeSignals(GetNodeSignals()); RegisterNodeSignals(GetNodeSignals());
} }
~TestingSetup()
{ TestingSetup::~TestingSetup()
{
UnregisterNodeSignals(GetNodeSignals());
threadGroup.interrupt_all(); threadGroup.interrupt_all();
threadGroup.join_all(); threadGroup.join_all();
UnregisterNodeSignals(GetNodeSignals());
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
UnregisterValidationInterface(pwalletMain);
delete pwalletMain; delete pwalletMain;
pwalletMain = NULL; pwalletMain = NULL;
#endif #endif
UnloadBlockIndex();
delete pcoinsTip; delete pcoinsTip;
delete pcoinsdbview; delete pcoinsdbview;
delete pblocktree; delete pblocktree;
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
bitdb.Flush(true); bitdb.Flush(true);
bitdb.Reset();
#endif #endif
boost::filesystem::remove_all(pathTemp); boost::filesystem::remove_all(pathTemp);
} }
};
BOOST_GLOBAL_FIXTURE(TestingSetup);
void Shutdown(void* parg) void Shutdown(void* parg)
{ {

18
src/test/test_bitcoin.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef BITCOIN_TEST_TEST_BITCOIN_H
#define BITCOIN_TEST_TEST_BITCOIN_H
#include "txdb.h"
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
struct TestingSetup {
CCoinsViewDB *pcoinsdbview;
boost::filesystem::path pathTemp;
boost::thread_group threadGroup;
TestingSetup();
~TestingSetup();
};
#endif

View file

@ -94,6 +94,7 @@ bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
bool TryCreateDirectory(const boost::filesystem::path& p); bool TryCreateDirectory(const boost::filesystem::path& p);
boost::filesystem::path GetDefaultDataDir(); boost::filesystem::path GetDefaultDataDir();
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true); const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
void ClearDatadirCache();
boost::filesystem::path GetConfigFile(); boost::filesystem::path GetConfigFile();
#ifndef WIN32 #ifndef WIN32
boost::filesystem::path GetPidFile(); boost::filesystem::path GetPidFile();

View file

@ -903,8 +903,8 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
int64_t now = GetTime(); int64_t now = GetTime();
std::string newFilename = strprintf("wallet.%d.bak", now); std::string newFilename = strprintf("wallet.%d.bak", now);
int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL, int result = dbenv.dbenv->dbrename(NULL, filename.c_str(), NULL,
newFilename.c_str(), DB_AUTO_COMMIT); newFilename.c_str(), DB_AUTO_COMMIT);
if (result == 0) if (result == 0)
LogPrintf("Renamed %s to %s\n", filename, newFilename); LogPrintf("Renamed %s to %s\n", filename, newFilename);
else else
@ -923,7 +923,7 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
bool fSuccess = allOK; bool fSuccess = allOK;
boost::scoped_ptr<Db> pdbCopy(new Db(&dbenv.dbenv, 0)); boost::scoped_ptr<Db> pdbCopy(new Db(dbenv.dbenv, 0));
int ret = pdbCopy->open(NULL, // Txn pointer int ret = pdbCopy->open(NULL, // Txn pointer
filename.c_str(), // Filename filename.c_str(), // Filename
"main", // Logical db name "main", // Logical db name