Skip to content

Commit

Permalink
test: Create test fixture for wallet
Browse files Browse the repository at this point in the history
Removes all the `#ifdef ENABLE_WALLET` from `test_bitcoin` by
making the wallet tests use their own fixture.
  • Loading branch information
laanwj committed Apr 18, 2016
1 parent de39c95 commit f4eae2d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Expand Up @@ -90,6 +90,8 @@ BITCOIN_TESTS =\

if ENABLE_WALLET
BITCOIN_TESTS += \
wallet/test/wallet_test_fixture.cpp \
wallet/test/wallet_test_fixture.h \
wallet/test/accounting_tests.cpp \
wallet/test/wallet_tests.cpp \
wallet/test/rpc_wallet_tests.cpp
Expand Down
23 changes: 0 additions & 23 deletions src/test/test_bitcoin.cpp
Expand Up @@ -19,10 +19,6 @@
#include "ui_interface.h"
#include "rpc/server.h"
#include "rpc/register.h"
#ifdef ENABLE_WALLET
#include "wallet/db.h"
#include "wallet/wallet.h"
#endif

#include "test/testutil.h"

Expand Down Expand Up @@ -57,10 +53,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
// Ideally we'd move all the RPC tests to the functional testing framework
// instead of unit tests, but for now we need these here.
RegisterAllCoreRPCCommands(tableRPC);
#ifdef ENABLE_WALLET
bitdb.MakeMock();
RegisterWalletRPCCommands(tableRPC);
#endif
ClearDatadirCache();
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
boost::filesystem::create_directories(pathTemp);
Expand All @@ -69,12 +61,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
InitBlockIndex(chainparams);
#ifdef ENABLE_WALLET
bool fFirstRun;
pwalletMain = new CWallet("wallet.dat");
pwalletMain->LoadWallet(fFirstRun);
RegisterValidationInterface(pwalletMain);
#endif
nScriptCheckThreads = 3;
for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck);
Expand All @@ -86,19 +72,10 @@ TestingSetup::~TestingSetup()
UnregisterNodeSignals(GetNodeSignals());
threadGroup.interrupt_all();
threadGroup.join_all();
#ifdef ENABLE_WALLET
UnregisterValidationInterface(pwalletMain);
delete pwalletMain;
pwalletMain = NULL;
#endif
UnloadBlockIndex();
delete pcoinsTip;
delete pcoinsdbview;
delete pblocktree;
#ifdef ENABLE_WALLET
bitdb.Flush(true);
bitdb.Reset();
#endif
boost::filesystem::remove_all(pathTemp);
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/test_bitcoin.h
Expand Up @@ -25,8 +25,7 @@ struct BasicTestingSetup {
};

/** Testing setup that configures a complete environment.
* Included are data directory, coins database, script check threads
* and wallet (if enabled) setup.
* Included are data directory, coins database, script check threads setup.
*/
struct TestingSetup: public BasicTestingSetup {
CCoinsViewDB *pcoinsdbview;
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/test/accounting_tests.cpp
Expand Up @@ -5,7 +5,7 @@
#include "wallet/wallet.h"
#include "wallet/walletdb.h"

#include "test/test_bitcoin.h"
#include "wallet/test/wallet_test_fixture.h"

#include <stdint.h>

Expand All @@ -14,7 +14,7 @@

extern CWallet* pwalletMain;

BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
BOOST_FIXTURE_TEST_SUITE(accounting_tests, WalletTestingSetup)

static void
GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/test/rpc_wallet_tests.cpp
Expand Up @@ -9,7 +9,7 @@
#include "main.h"
#include "wallet/wallet.h"

#include "test/test_bitcoin.h"
#include "wallet/test/wallet_test_fixture.h"

#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
Expand All @@ -23,7 +23,7 @@ extern UniValue CallRPC(string args);

extern CWallet* pwalletMain;

BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, WalletTestingSetup)

BOOST_AUTO_TEST_CASE(rpc_addmultisig)
{
Expand Down
28 changes: 28 additions & 0 deletions src/wallet/test/wallet_test_fixture.cpp
@@ -0,0 +1,28 @@
#include "wallet/test/wallet_test_fixture.h"

#include "rpc/server.h"
#include "wallet/db.h"
#include "wallet/wallet.h"

WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
TestingSetup(chainName)
{
bitdb.MakeMock();

bool fFirstRun;
pwalletMain = new CWallet("wallet.dat");
pwalletMain->LoadWallet(fFirstRun);
RegisterValidationInterface(pwalletMain);

RegisterWalletRPCCommands(tableRPC);
}

WalletTestingSetup::~WalletTestingSetup()
{
UnregisterValidationInterface(pwalletMain);
delete pwalletMain;
pwalletMain = NULL;

bitdb.Flush(true);
bitdb.Reset();
}
18 changes: 18 additions & 0 deletions src/wallet/test/wallet_test_fixture.h
@@ -0,0 +1,18 @@
// Copyright (c) 2016 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_WALLET_TEST_FIXTURE_H
#define BITCOIN_WALLET_TEST_FIXTURE_H

#include "test/test_bitcoin.h"

/** Testing setup and teardown for wallet.
*/
struct WalletTestingSetup: public TestingSetup {
WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~WalletTestingSetup();
};

#endif

4 changes: 2 additions & 2 deletions src/wallet/test/wallet_tests.cpp
Expand Up @@ -9,7 +9,7 @@
#include <utility>
#include <vector>

#include "test/test_bitcoin.h"
#include "wallet/test/wallet_test_fixture.h"

#include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp>
Expand All @@ -25,7 +25,7 @@ using namespace std;

typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;

BOOST_FIXTURE_TEST_SUITE(wallet_tests, TestingSetup)
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)

static CWallet wallet;
static vector<COutput> vCoins;
Expand Down

0 comments on commit f4eae2d

Please sign in to comment.