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 ripple {
13
14namespace NodeStore {
15
16// Tests the Backend interface
17//
18class Backend_test : public TestBase
19{
20public:
21 void
23 std::string const& type,
24 std::uint64_t const seedValue,
25 int numObjsToTest = 2000)
26 {
27 DummyScheduler scheduler;
28
29 testcase("Backend type=" + type);
30
31 Section params;
32 beast::temp_dir tempDir;
33 params.set("type", type);
34 params.set("path", tempDir.path());
35
36 beast::xor_shift_engine rng(seedValue);
37
38 // Create a batch
39 auto batch = createPredictableBatch(numObjsToTest, rng());
40
41 using namespace beast::severities;
42 test::SuiteJournal journal("Backend_test", *this);
43
44 {
45 // Open the backend
47 params, megabytes(4), scheduler, journal);
48 backend->open();
49
50 // Write the batch
51 storeBatch(*backend, batch);
52
53 {
54 // Read it back in
55 Batch copy;
56 fetchCopyOfBatch(*backend, &copy, batch);
57 BEAST_EXPECT(areBatchesEqual(batch, copy));
58 }
59
60 {
61 // Reorder and read the copy again
62 std::shuffle(batch.begin(), batch.end(), rng);
63 Batch copy;
64 fetchCopyOfBatch(*backend, &copy, batch);
65 BEAST_EXPECT(areBatchesEqual(batch, copy));
66 }
67 }
68
69 {
70 // Re-open the backend
72 params, megabytes(4), scheduler, journal);
73 backend->open();
74
75 // Read it back in
76 Batch copy;
77 fetchCopyOfBatch(*backend, &copy, batch);
78 // Canonicalize the source and destination batches
79 std::sort(batch.begin(), batch.end(), LessThan{});
80 std::sort(copy.begin(), copy.end(), LessThan{});
81 BEAST_EXPECT(areBatchesEqual(batch, copy));
82 }
83 }
84
85 //--------------------------------------------------------------------------
86
87 void
88 run() override
89 {
90 std::uint64_t const seedValue = 50;
91
92 testBackend("nudb", seedValue);
93
94#if XRPL_ROCKSDB_AVAILABLE
95 testBackend("rocksdb", seedValue);
96#endif
97
98#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS
99 testBackend("sqlite", seedValue);
100#endif
101 }
102};
103
104BEAST_DEFINE_TESTSUITE(Backend, nodestore, ripple);
105
106} // namespace NodeStore
107} // namespace ripple
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:152
void testBackend(std::string const &type, std::uint64_t const seedValue, int numObjsToTest=2000)
void run() override
Runs the suite.
A backend used for the NodeStore.
Definition Backend.h:21
Simple NodeStore Scheduler that just peforms the tasks synchronously.
virtual std::unique_ptr< Backend > make_Backend(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 Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:63
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:103
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:138
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:128
Holds a collection of configuration values.
Definition BasicConfig.h:26
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