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 
30 namespace NodeStore {
31 
32 // Tests the Backend interface
33 //
34 class Backend_test : public TestBase
35 {
36 public:
37  void
39  std::string const& type,
40  std::uint64_t const seedValue,
41  int numObjsToTest = 2000)
42  {
43  DummyScheduler scheduler;
44 
45  testcase("Backend type=" + type);
46 
47  Section params;
48  beast::temp_dir tempDir;
49  params.set("type", type);
50  params.set("path", tempDir.path());
51 
52  beast::xor_shift_engine rng(seedValue);
53 
54  // Create a batch
55  auto batch = createPredictableBatch(numObjsToTest, rng());
56 
57  using namespace beast::severities;
58  test::SuiteJournal journal("Backend_test", *this);
59 
60  {
61  // Open the backend
63  params, megabytes(4), scheduler, journal);
64  backend->open();
65 
66  // Write the batch
67  storeBatch(*backend, batch);
68 
69  {
70  // Read it back in
71  Batch copy;
72  fetchCopyOfBatch(*backend, &copy, batch);
73  BEAST_EXPECT(areBatchesEqual(batch, copy));
74  }
75 
76  {
77  // Reorder and read the copy again
78  std::shuffle(batch.begin(), batch.end(), rng);
79  Batch copy;
80  fetchCopyOfBatch(*backend, &copy, batch);
81  BEAST_EXPECT(areBatchesEqual(batch, copy));
82  }
83  }
84 
85  {
86  // Re-open the backend
88  params, megabytes(4), scheduler, journal);
89  backend->open();
90 
91  // Read it back in
92  Batch copy;
93  fetchCopyOfBatch(*backend, &copy, batch);
94  // Canonicalize the source and destination batches
95  std::sort(batch.begin(), batch.end(), LessThan{});
96  std::sort(copy.begin(), copy.end(), LessThan{});
97  BEAST_EXPECT(areBatchesEqual(batch, copy));
98  }
99  }
100 
101  //--------------------------------------------------------------------------
102 
103  void
104  run() override
105  {
106  std::uint64_t const seedValue = 50;
107 
108  testBackend("nudb", seedValue);
109 
110 #if RIPPLE_ROCKSDB_AVAILABLE
111  testBackend("rocksdb", seedValue);
112 #endif
113 
114 #ifdef RIPPLE_ENABLE_SQLITE_BACKEND_TESTS
115  testBackend("sqlite", seedValue);
116 #endif
117  }
118 };
119 
120 BEAST_DEFINE_TESTSUITE(Backend, ripple_core, ripple);
121 
122 } // namespace NodeStore
123 } // 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:42
ripple::NodeStore::TestBase
Definition: TestBase.h:68
std::string
STL class.
ripple::NodeStore::Backend_test::run
void run() override
Definition: Backend_test.cpp:104
ripple::NodeStore::Backend_test::testBackend
void testBackend(std::string const &type, std::uint64_t const seedValue, int numObjsToTest=2000)
Definition: Backend_test.cpp:38
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:34
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:120
std::unique_ptr
STL class.
std::shuffle
T shuffle(T... args)
beast::temp_dir
RAII temporary directory.
Definition: temp_dir.h:33