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 <xrpld/nodestore/DummyScheduler.h>
24#include <xrpld/nodestore/Manager.h>
25#include <xrpld/unity/rocksdb.h>
26
27#include <xrpl/basics/ByteUtilities.h>
28#include <xrpl/beast/utility/temp_dir.h>
29
30#include <algorithm>
31
32namespace ripple {
33
34namespace NodeStore {
35
36// Tests the Backend interface
37//
38class Backend_test : public TestBase
39{
40public:
41 void
43 std::string const& type,
44 std::uint64_t const seedValue,
45 int numObjsToTest = 2000)
46 {
47 DummyScheduler scheduler;
48
49 testcase("Backend type=" + type);
50
51 Section params;
52 beast::temp_dir tempDir;
53 params.set("type", type);
54 params.set("path", tempDir.path());
55
56 beast::xor_shift_engine rng(seedValue);
57
58 // Create a batch
59 auto batch = createPredictableBatch(numObjsToTest, rng());
60
61 using namespace beast::severities;
62 test::SuiteJournal journal("Backend_test", *this);
63
64 {
65 // Open the backend
67 params, megabytes(4), scheduler, journal);
68 backend->open();
69
70 // Write the batch
71 storeBatch(*backend, batch);
72
73 {
74 // Read it back in
75 Batch copy;
76 fetchCopyOfBatch(*backend, &copy, batch);
77 BEAST_EXPECT(areBatchesEqual(batch, copy));
78 }
79
80 {
81 // Reorder and read the copy again
82 std::shuffle(batch.begin(), batch.end(), rng);
83 Batch copy;
84 fetchCopyOfBatch(*backend, &copy, batch);
85 BEAST_EXPECT(areBatchesEqual(batch, copy));
86 }
87 }
88
89 {
90 // Re-open the backend
92 params, megabytes(4), scheduler, journal);
93 backend->open();
94
95 // Read it back in
96 Batch copy;
97 fetchCopyOfBatch(*backend, &copy, batch);
98 // Canonicalize the source and destination batches
99 std::sort(batch.begin(), batch.end(), LessThan{});
100 std::sort(copy.begin(), copy.end(), LessThan{});
101 BEAST_EXPECT(areBatchesEqual(batch, copy));
102 }
103 }
104
105 //--------------------------------------------------------------------------
106
107 void
108 run() override
109 {
110 std::uint64_t const seedValue = 50;
111
112 testBackend("nudb", seedValue);
113
114#if RIPPLE_ROCKSDB_AVAILABLE
115 testBackend("rocksdb", seedValue);
116#endif
117
118#ifdef RIPPLE_ENABLE_SQLITE_BACKEND_TESTS
119 testBackend("sqlite", seedValue);
120#endif
121 }
122};
123
124BEAST_DEFINE_TESTSUITE(Backend, ripple_core, ripple);
125
126} // namespace NodeStore
127} // 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.
Definition: ManagerImp.cpp:116
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition: TestBase.h:83
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition: TestBase.h:123
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition: TestBase.h:158
void storeBatch(Backend &backend, Batch const &batch)
Definition: TestBase.h:148
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.
Definition: BasicConfig.cpp:41
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:26
constexpr auto megabytes(T value) noexcept
Definition: ByteUtilities.h:34
T shuffle(T... args)
T sort(T... args)
Binary function that satisfies the strict-weak-ordering requirement.
Definition: TestBase.h:48