mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Use beast::temp_dir in tests (RIPD-1414):
Use non-throwing fs function in temp_dir destructor. Eliminate only use of BOOST_SCOPE_EXIT.
This commit is contained in:
committed by
Nik Bougalis
parent
026a249173
commit
22c97ba801
@@ -57,7 +57,10 @@ public:
|
|||||||
/// Destroy a temporary directory.
|
/// Destroy a temporary directory.
|
||||||
~temp_dir()
|
~temp_dir()
|
||||||
{
|
{
|
||||||
boost::filesystem::remove_all (path_);
|
// use non-throwing calls in the destructor
|
||||||
|
boost::system::error_code ec;
|
||||||
|
boost::filesystem::remove_all(path_, ec);
|
||||||
|
// TODO: warn/notify if ec set ?
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the native path for the temporary directory
|
/// Get the native path for the temporary directory
|
||||||
@@ -80,4 +83,4 @@ public:
|
|||||||
|
|
||||||
} // beast
|
} // beast
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -18,54 +18,49 @@
|
|||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|
||||||
#include <BeastConfig.h>
|
#include <BeastConfig.h>
|
||||||
|
#include <ripple/beast/unit_test.h>
|
||||||
#include <test/jtx.h>
|
#include <test/jtx.h>
|
||||||
#include <test/jtx/Env.h>
|
#include <test/jtx/Env.h>
|
||||||
#include <ripple/beast/unit_test.h>
|
#include <ripple/beast/utility/temp_dir.h>
|
||||||
#include <ripple/protocol/JsonFields.h>
|
#include <ripple/protocol/JsonFields.h>
|
||||||
#include <ripple/protocol/SField.h>
|
#include <ripple/protocol/SField.h>
|
||||||
#include <boost/scope_exit.hpp>
|
|
||||||
#include <boost/filesystem.hpp>
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
class LedgerLoad_test : public beast::unit_test::suite
|
class LedgerLoad_test : public beast::unit_test::suite
|
||||||
{
|
{
|
||||||
Json::Value jvLedger_;
|
auto static ledgerConfig(
|
||||||
Json::Value jvHashes_;
|
std::unique_ptr<Config> cfg,
|
||||||
boost::filesystem::path ledgerFile_;
|
std::string const& dbPath,
|
||||||
boost::filesystem::path dbPath_;
|
std::string const& ledger,
|
||||||
|
Config::StartUpType type)
|
||||||
auto ledgerConfig(std::string const& ledger, Config::StartUpType type)
|
|
||||||
{
|
{
|
||||||
assert(! dbPath_.empty());
|
cfg->START_LEDGER = ledger;
|
||||||
auto p = test::jtx::envconfig();
|
cfg->START_UP = type;
|
||||||
p->START_LEDGER = ledger;
|
assert(! dbPath.empty());
|
||||||
p->START_UP = type;
|
cfg->legacy("database_path", dbPath);
|
||||||
p->legacy("database_path", dbPath_.string());
|
return cfg;
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup for test cases
|
// setup for test cases
|
||||||
void
|
struct SetupData
|
||||||
setupLedger()
|
{
|
||||||
|
std::string const dbPath;
|
||||||
|
std::string ledgerFile;
|
||||||
|
Json::Value ledger;
|
||||||
|
Json::Value hashes;
|
||||||
|
};
|
||||||
|
|
||||||
|
SetupData
|
||||||
|
setupLedger(beast::temp_dir const& td)
|
||||||
{
|
{
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
|
SetupData retval = {td.path()};
|
||||||
|
|
||||||
boost::system::error_code ec;
|
retval.ledgerFile = td.file("ledgerdata.json");
|
||||||
// create a temporary path to write ledger files in
|
|
||||||
dbPath_ = boost::filesystem::temp_directory_path(ec);
|
|
||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
|
||||||
return;
|
|
||||||
dbPath_ /= boost::filesystem::unique_path("%%%%-%%%%-%%%%-%%%%", ec);
|
|
||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
|
||||||
return;
|
|
||||||
boost::filesystem::create_directories(dbPath_, ec);
|
|
||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
ledgerFile_ = dbPath_ / "ledgerdata.json";
|
|
||||||
|
|
||||||
Env env {*this};
|
Env env {*this};
|
||||||
Account prev;
|
Account prev;
|
||||||
@@ -85,62 +80,72 @@ class LedgerLoad_test : public beast::unit_test::suite
|
|||||||
prev = std::move(acct);
|
prev = std::move(acct);
|
||||||
}
|
}
|
||||||
|
|
||||||
jvLedger_ = env.rpc ("ledger", "current", "full") [jss::result];
|
retval.ledger = env.rpc ("ledger", "current", "full") [jss::result];
|
||||||
BEAST_EXPECT(jvLedger_[jss::ledger][jss::accountState].size() == 101);
|
BEAST_EXPECT(retval.ledger[jss::ledger][jss::accountState].size() == 101);
|
||||||
|
|
||||||
for(auto const& it : jvLedger_[jss::ledger][jss::accountState])
|
retval.hashes = [&] {
|
||||||
{
|
for(auto const& it : retval.ledger[jss::ledger][jss::accountState])
|
||||||
if(it[sfLedgerEntryType.fieldName] == "LedgerHashes")
|
|
||||||
{
|
{
|
||||||
jvHashes_ = it[sfHashes.fieldName];
|
if(it[sfLedgerEntryType.fieldName] == "LedgerHashes")
|
||||||
|
return it[sfHashes.fieldName];
|
||||||
}
|
}
|
||||||
}
|
return Json::Value {};
|
||||||
BEAST_EXPECT(jvHashes_.size() == 41);
|
}();
|
||||||
|
|
||||||
|
BEAST_EXPECT(retval.hashes.size() == 41);
|
||||||
|
|
||||||
//write this ledger data to a file.
|
//write this ledger data to a file.
|
||||||
std::ofstream o (ledgerFile_.string(), std::ios::out | std::ios::trunc);
|
std::ofstream o (retval.ledgerFile, std::ios::out | std::ios::trunc);
|
||||||
o << to_string(jvLedger_);
|
o << to_string(retval.ledger);
|
||||||
o.close();
|
o.close();
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testLoad ()
|
testLoad (SetupData const& sd)
|
||||||
{
|
{
|
||||||
testcase ("Load a saved ledger");
|
testcase ("Load a saved ledger");
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
|
|
||||||
// create a new env with the ledger file specified for startup
|
// create a new env with the ledger file specified for startup
|
||||||
Env env(*this, ledgerConfig(ledgerFile_.string(), Config::LOAD_FILE));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, sd.ledgerFile, Config::LOAD_FILE));
|
||||||
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
||||||
BEAST_EXPECT(
|
BEAST_EXPECT(
|
||||||
jvLedger_[jss::ledger][jss::accountState].size() ==
|
sd.ledger[jss::ledger][jss::accountState].size() ==
|
||||||
jrb[jss::ledger][jss::accountState].size());
|
jrb[jss::ledger][jss::accountState].size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testBadFiles ()
|
testBadFiles (SetupData const& sd)
|
||||||
{
|
{
|
||||||
testcase ("Load ledger: Bad Files");
|
testcase ("Load ledger: Bad Files");
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
using namespace boost::filesystem;
|
using namespace boost::filesystem;
|
||||||
|
|
||||||
// empty path
|
// empty path
|
||||||
except ([this]
|
except ([&]
|
||||||
{
|
{
|
||||||
Env env(*this, ledgerConfig("", Config::LOAD_FILE));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, "", Config::LOAD_FILE));
|
||||||
});
|
});
|
||||||
|
|
||||||
// file does not exist
|
// file does not exist
|
||||||
except ([this]
|
except ([&]
|
||||||
{
|
{
|
||||||
Env env(*this, ledgerConfig("badfile.json", Config::LOAD_FILE));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, "badfile.json", Config::LOAD_FILE));
|
||||||
});
|
});
|
||||||
|
|
||||||
// make a corrupted version of the ledger file (last 10 bytes removed).
|
// make a corrupted version of the ledger file (last 10 bytes removed).
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
auto ledgerFileCorrupt = dbPath_ / "ledgerdata_bad.json";
|
auto ledgerFileCorrupt =
|
||||||
|
boost::filesystem::path{sd.dbPath} / "ledgerdata_bad.json";
|
||||||
copy_file(
|
copy_file(
|
||||||
ledgerFile_,
|
sd.ledgerFile,
|
||||||
ledgerFileCorrupt,
|
ledgerFileCorrupt,
|
||||||
copy_option::overwrite_if_exists,
|
copy_option::overwrite_if_exists,
|
||||||
ec);
|
ec);
|
||||||
@@ -153,73 +158,77 @@ class LedgerLoad_test : public beast::unit_test::suite
|
|||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
if(! BEAST_EXPECTS(!ec, ec.message()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
except ([this, &ledgerFileCorrupt]
|
except ([&]
|
||||||
{
|
{
|
||||||
Env env(*this, ledgerConfig(ledgerFileCorrupt.string(), Config::LOAD_FILE));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, ledgerFileCorrupt.string(), Config::LOAD_FILE));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testLoadByHash ()
|
testLoadByHash (SetupData const& sd)
|
||||||
{
|
{
|
||||||
testcase ("Load by hash");
|
testcase ("Load by hash");
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
|
|
||||||
// create a new env with the ledger hash specified for startup
|
// create a new env with the ledger hash specified for startup
|
||||||
auto ledgerHash = to_string(jvHashes_[jvHashes_.size()-1]);
|
auto ledgerHash = to_string(sd.hashes[sd.hashes.size()-1]);
|
||||||
boost::erase_all(ledgerHash, "\"");
|
boost::erase_all(ledgerHash, "\"");
|
||||||
Env env(*this, ledgerConfig(ledgerHash, Config::LOAD));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, ledgerHash, Config::LOAD));
|
||||||
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
||||||
BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
|
BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
|
||||||
BEAST_EXPECT(
|
BEAST_EXPECT(
|
||||||
jrb[jss::ledger][jss::accountState].size() <=
|
jrb[jss::ledger][jss::accountState].size() <=
|
||||||
jvLedger_[jss::ledger][jss::accountState].size());
|
sd.ledger[jss::ledger][jss::accountState].size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testLoadLatest ()
|
testLoadLatest (SetupData const& sd)
|
||||||
{
|
{
|
||||||
testcase ("Load by keyword");
|
testcase ("Load by keyword");
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
|
|
||||||
// create a new env with the ledger "latest" specified for startup
|
// create a new env with the ledger "latest" specified for startup
|
||||||
Env env(*this, ledgerConfig("latest", Config::LOAD));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, "latest", Config::LOAD));
|
||||||
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
||||||
BEAST_EXPECT(
|
BEAST_EXPECT(
|
||||||
jvLedger_[jss::ledger][jss::accountState].size() ==
|
sd.ledger[jss::ledger][jss::accountState].size() ==
|
||||||
jrb[jss::ledger][jss::accountState].size());
|
jrb[jss::ledger][jss::accountState].size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testLoadIndex ()
|
testLoadIndex (SetupData const& sd)
|
||||||
{
|
{
|
||||||
testcase ("Load by index");
|
testcase ("Load by index");
|
||||||
using namespace test::jtx;
|
using namespace test::jtx;
|
||||||
|
|
||||||
// create a new env with specific ledger index at startup
|
// create a new env with specific ledger index at startup
|
||||||
Env env(*this, ledgerConfig("43", Config::LOAD));
|
Env env(*this,
|
||||||
|
envconfig( ledgerConfig,
|
||||||
|
sd.dbPath, "43", Config::LOAD));
|
||||||
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
|
||||||
BEAST_EXPECT(
|
BEAST_EXPECT(
|
||||||
jvLedger_[jss::ledger][jss::accountState].size() ==
|
sd.ledger[jss::ledger][jss::accountState].size() ==
|
||||||
jrb[jss::ledger][jss::accountState].size());
|
jrb[jss::ledger][jss::accountState].size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void run ()
|
void run ()
|
||||||
{
|
{
|
||||||
setupLedger();
|
beast::temp_dir td;
|
||||||
BOOST_SCOPE_EXIT( this_ ) {
|
auto sd = setupLedger(td);
|
||||||
boost::system::error_code ec;
|
|
||||||
boost::filesystem::remove_all(this_->dbPath_, ec);
|
|
||||||
this_->expect(!ec, ec.message(), __FILE__, __LINE__);
|
|
||||||
} BOOST_SCOPE_EXIT_END
|
|
||||||
|
|
||||||
// test cases
|
// test cases
|
||||||
testLoad ();
|
testLoad (sd);
|
||||||
testBadFiles ();
|
testBadFiles (sd);
|
||||||
testLoadByHash ();
|
testLoadByHash (sd);
|
||||||
testLoadLatest ();
|
testLoadLatest (sd);
|
||||||
testLoadIndex ();
|
testLoadIndex (sd);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user