rippled
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/basics/rocksdb.h>
6#include <xrpl/beast/utility/temp_dir.h>
7#include <xrpl/nodestore/DummyScheduler.h>
8#include <xrpl/nodestore/Manager.h>
9
10#include <algorithm>
11
12namespace xrpl {
13
14namespace NodeStore {
15
16// Tests the Backend interface
17//
18class Backend_test : public TestBase
19{
20public:
21 void
22 testBackend(std::string const& type, std::uint64_t const seedValue, int numObjsToTest = 2000)
23 {
24 DummyScheduler scheduler;
25
26 testcase("Backend type=" + type);
27
28 Section params;
29 beast::temp_dir tempDir;
30 params.set("type", type);
31 params.set("path", tempDir.path());
32
33 beast::xor_shift_engine rng(seedValue);
34
35 // Create a batch
36 auto batch = createPredictableBatch(numObjsToTest, rng());
37
38 using namespace beast::severities;
39 test::SuiteJournal journal("Backend_test", *this);
40
41 {
42 // Open the backend
44 Manager::instance().make_Backend(params, megabytes(4), scheduler, journal);
45 backend->open();
46
47 // Write the batch
48 storeBatch(*backend, batch);
49
50 {
51 // Read it back in
52 Batch copy;
53 fetchCopyOfBatch(*backend, &copy, batch);
54 BEAST_EXPECT(areBatchesEqual(batch, copy));
55 }
56
57 {
58 // Reorder and read the copy again
59 std::shuffle(batch.begin(), batch.end(), rng);
60 Batch copy;
61 fetchCopyOfBatch(*backend, &copy, batch);
62 BEAST_EXPECT(areBatchesEqual(batch, copy));
63 }
64 }
65
66 {
67 // Re-open the backend
69 Manager::instance().make_Backend(params, megabytes(4), scheduler, journal);
70 backend->open();
71
72 // Read it back in
73 Batch copy;
74 fetchCopyOfBatch(*backend, &copy, batch);
75 // Canonicalize the source and destination batches
76 std::sort(batch.begin(), batch.end(), LessThan{});
77 std::sort(copy.begin(), copy.end(), LessThan{});
78 BEAST_EXPECT(areBatchesEqual(batch, copy));
79 }
80 }
81
82 //--------------------------------------------------------------------------
83
84 void
85 run() override
86 {
87 std::uint64_t const seedValue = 50;
88
89 testBackend("nudb", seedValue);
90
91#if XRPL_ROCKSDB_AVAILABLE
92 testBackend("rocksdb", seedValue);
93#endif
94
95#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS
96 testBackend("sqlite", seedValue);
97#endif
98 }
99};
100
101BEAST_DEFINE_TESTSUITE(Backend, nodestore, xrpl);
102
103} // namespace NodeStore
104} // namespace xrpl
RAII temporary directory.
Definition temp_dir.h:16
std::string path() const
Get the native path for the temporary directory.
Definition temp_dir.h:48
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:148
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:21
Simple NodeStore Scheduler that just performs the tasks synchronously.
static Manager & instance()
Returns the instance of the manager singleton.
virtual std::unique_ptr< Backend > make_Backend(Section const &parameters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:97
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:132
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:122
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:58
Holds a collection of configuration values.
Definition BasicConfig.h:25
void set(std::string const &key, std::string const &value)
Set a key/value pair.
A namespace for easy access to logging severity values.
Definition Journal.h:11
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
constexpr auto megabytes(T value) noexcept
T shuffle(T... args)
T sort(T... args)
Binary function that satisfies the strict-weak-ordering requirement.
Definition TestBase.h:28