xrpld
Loading...
Searching...
No Matches
Backend_test.cpp
1#include <test/nodestore/TestBase.h>
2#include <test/unit_test/SuiteJournal.h>
3
4#include <xrpl/basics/ByteUtilities.h>
5#include <xrpl/beast/unit_test/suite.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/temp_dir.h>
8#include <xrpl/beast/xor_shift_engine.h>
9#include <xrpl/config/BasicConfig.h>
10#include <xrpl/config/Constants.h>
11#include <xrpl/nodestore/Backend.h>
12#include <xrpl/nodestore/DummyScheduler.h>
13#include <xrpl/nodestore/Manager.h>
14#include <xrpl/nodestore/Types.h>
15
16#include <algorithm>
17#include <cstdint>
18#include <memory>
19#include <string>
20
21namespace xrpl::NodeStore {
22
23// Tests the Backend interface
24//
25class Backend_test : public TestBase
26{
27public:
28 void
29 testBackend(std::string const& type, std::uint64_t const seedValue, int numObjsToTest = 2000)
30 {
31 DummyScheduler scheduler;
32
33 testcase("Backend type=" + type);
34
35 Section params;
36 beast::TempDir const tempDir;
37 params.set(Keys::kType, type);
38 params.set(Keys::kPath, tempDir.path());
39
40 beast::xor_shift_engine rng(seedValue);
41
42 // Create a batch
43 auto batch = createPredictableBatch(numObjsToTest, rng());
44
45 using beast::Severity;
46 test::SuiteJournal journal("Backend_test", *this);
47
48 {
49 // Open the backend
51 Manager::instance().makeBackend(params, megabytes(4), scheduler, journal);
52 backend->open();
53
54 // Write the batch
55 storeBatch(*backend, batch);
56
57 {
58 // Read it back in
59 Batch copy;
60 fetchCopyOfBatch(*backend, &copy, batch);
61 BEAST_EXPECT(areBatchesEqual(batch, copy));
62 }
63
64 {
65 // Reorder and read the copy again
66 std::shuffle(batch.begin(), batch.end(), rng);
67 Batch copy;
68 fetchCopyOfBatch(*backend, &copy, batch);
69 BEAST_EXPECT(areBatchesEqual(batch, copy));
70 }
71 }
72
73 {
74 // Re-open the backend
76 Manager::instance().makeBackend(params, megabytes(4), scheduler, journal);
77 backend->open();
78
79 // Read it back in
80 Batch copy;
81 fetchCopyOfBatch(*backend, &copy, batch);
82 // Canonicalize the source and destination batches
85 BEAST_EXPECT(areBatchesEqual(batch, copy));
86 }
87 }
88
89 //--------------------------------------------------------------------------
90
91 void
92 run() override
93 {
94 std::uint64_t const seedValue = 50;
95
96 testBackend("nudb", seedValue);
97
98#if XRPL_ROCKSDB_AVAILABLE
99 testBackend("rocksdb", seedValue);
100#endif
101
102#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS
103 testBackend("sqlite", seedValue);
104#endif
105 }
106};
107
109
110} // namespace xrpl::NodeStore
RAII temporary directory.
Definition temp_dir.h:15
std::string path() const
Get the native path for the temporary directory.
Definition temp_dir.h:47
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
void run() override
Runs the suite.
void testBackend(std::string const &type, std::uint64_t const seedValue, int numObjsToTest=2000)
A backend used for the NodeStore.
Definition Backend.h:19
Simple NodeStore Scheduler that just performs the tasks synchronously.
virtual std::unique_ptr< Backend > makeBackend(Section const &parameters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
static Manager & instance()
Returns the instance of the manager singleton.
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:95
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:130
static void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:120
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:57
Holds a collection of configuration values.
Definition BasicConfig.h:24
void set(std::string const &key, std::string const &value)
Set a key/value pair.
detail::XorShiftEngine<> xor_shift_engine
XOR-shift Generator.
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:11
BEAST_DEFINE_TESTSUITE(Backend, nodestore, xrpl)
std::vector< std::shared_ptr< NodeObject > > Batch
A batch of NodeObjects to write at once.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr auto megabytes(T value) noexcept
T shuffle(T... args)
T sort(T... args)
static constexpr auto kType
Definition Constants.h:170
static constexpr auto kPath
Definition Constants.h:140
Binary function that satisfies the strict-weak-ordering requirement.
Definition TestBase.h:26