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