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/unity/rocksdb.h>
21 #include <ripple/nodestore/DummyScheduler.h>
22 #include <ripple/nodestore/Manager.h>
23 #include <ripple/beast/utility/temp_dir.h>
24 #include <test/nodestore/TestBase.h>
25 #include <test/unit_test/SuiteJournal.h>
26 #include <algorithm>
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 testBackend (
37  std::string const& type,
38  std::uint64_t const seedValue,
39  int numObjsToTest = 2000)
40  {
41  DummyScheduler scheduler;
42 
43  testcase ("Backend type=" + type);
44 
45  Section params;
46  beast::temp_dir tempDir;
47  params.set ("type", type);
48  params.set ("path", tempDir.path());
49 
50  beast::xor_shift_engine rng (seedValue);
51 
52  // Create a batch
53  auto batch = createPredictableBatch (
54  numObjsToTest, rng());
55 
56  using namespace beast::severities;
57  test::SuiteJournal journal ("Backend_test", *this);
58 
59  {
60  // Open the backend
63  params, 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 (
79  batch.begin(),
80  batch.end(),
81  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, 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 run () override
107  {
108  std::uint64_t const seedValue = 50;
109 
110  testBackend ("nudb", seedValue);
111 
112  #if RIPPLE_ROCKSDB_AVAILABLE
113  testBackend ("rocksdb", seedValue);
114  #endif
115 
116  #ifdef RIPPLE_ENABLE_SQLITE_BACKEND_TESTS
117  testBackend ("sqlite", seedValue);
118  #endif
119  }
120 };
121 
122 BEAST_DEFINE_TESTSUITE(Backend,ripple_core,ripple);
123 
124 }
125 }
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:106
ripple::NodeStore::Backend_test::testBackend
void testBackend(std::string const &type, std::uint64_t const seedValue, int numObjsToTest=2000)
Definition: Backend_test.cpp:36
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
ripple::NodeStore::Manager::make_Backend
virtual std::unique_ptr< Backend > make_Backend(Section const &parameters, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
std::sort
T sort(T... args)
algorithm
ripple::NodeStore::TestBase::areBatchesEqual
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition: TestBase.h:117
ripple::NodeStore::Backend_test
Definition: Backend_test.cpp:33
ripple::NodeStore::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(Backend, ripple_core, ripple)
ripple::NodeStore::TestBase::fetchCopyOfBatch
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition: TestBase.h:150
std::uint64_t
ripple::test::SuiteJournal
Definition: SuiteJournal.h:81
beast::temp_dir::path
std::string path() const
Get the native path for the temporary directory.
Definition: temp_dir.h:68
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:33
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:141
ripple::NodeStore::Manager::instance
static Manager & instance()
Returns the instance of the manager singleton.
Definition: ManagerImp.cpp:117
std::unique_ptr
STL class.
std::shuffle
T shuffle(T... args)
beast::temp_dir
RAII temporary directory.
Definition: temp_dir.h:33