rippled
Loading...
Searching...
No Matches
Backend_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/nodestore/TestBase.h>
21#include <test/unit_test/SuiteJournal.h>
22
23#include <xrpl/basics/ByteUtilities.h>
24#include <xrpl/basics/rocksdb.h>
25#include <xrpl/beast/utility/temp_dir.h>
26#include <xrpl/nodestore/DummyScheduler.h>
27#include <xrpl/nodestore/Manager.h>
28
29#include <algorithm>
30
31namespace ripple {
32
33namespace NodeStore {
34
35// Tests the Backend interface
36//
37class Backend_test : public TestBase
38{
39public:
40 void
42 std::string const& type,
43 std::uint64_t const seedValue,
44 int numObjsToTest = 2000)
45 {
46 DummyScheduler scheduler;
47
48 testcase("Backend type=" + type);
49
50 Section params;
51 beast::temp_dir tempDir;
52 params.set("type", type);
53 params.set("path", tempDir.path());
54
55 beast::xor_shift_engine rng(seedValue);
56
57 // Create a batch
58 auto batch = createPredictableBatch(numObjsToTest, rng());
59
60 using namespace beast::severities;
61 test::SuiteJournal journal("Backend_test", *this);
62
63 {
64 // Open the backend
66 params, megabytes(4), scheduler, journal);
67 backend->open();
68
69 // Write the batch
70 storeBatch(*backend, batch);
71
72 {
73 // Read it back in
74 Batch copy;
75 fetchCopyOfBatch(*backend, &copy, batch);
76 BEAST_EXPECT(areBatchesEqual(batch, copy));
77 }
78
79 {
80 // Reorder and read the copy again
81 std::shuffle(batch.begin(), batch.end(), rng);
82 Batch copy;
83 fetchCopyOfBatch(*backend, &copy, batch);
84 BEAST_EXPECT(areBatchesEqual(batch, copy));
85 }
86 }
87
88 {
89 // Re-open the backend
91 params, megabytes(4), scheduler, journal);
92 backend->open();
93
94 // Read it back in
95 Batch copy;
96 fetchCopyOfBatch(*backend, &copy, batch);
97 // Canonicalize the source and destination batches
98 std::sort(batch.begin(), batch.end(), LessThan{});
99 std::sort(copy.begin(), copy.end(), LessThan{});
100 BEAST_EXPECT(areBatchesEqual(batch, copy));
101 }
102 }
103
104 //--------------------------------------------------------------------------
105
106 void
107 run() override
108 {
109 std::uint64_t const seedValue = 50;
110
111 testBackend("nudb", seedValue);
112
113#if XRPL_ROCKSDB_AVAILABLE
114 testBackend("rocksdb", seedValue);
115#endif
116
117#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS
118 testBackend("sqlite", seedValue);
119#endif
120 }
121};
122
123BEAST_DEFINE_TESTSUITE(Backend, nodestore, ripple);
124
125} // namespace NodeStore
126} // namespace ripple
RAII temporary directory.
Definition temp_dir.h:35
std::string path() const
Get the native path for the temporary directory.
Definition temp_dir.h:67
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
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:40
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:82
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:122
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:157
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:147
Holds a collection of configuration values.
Definition BasicConfig.h:45
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:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:47