xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
nodestore
tests/libxrpl/nodestore/NuDBFactory.cpp
1
#include <xrpl/basics/ByteUtilities.h>
2
#include <xrpl/basics/FileUtilities.h>
3
#include <xrpl/beast/utility/Journal.h>
4
#include <xrpl/config/BasicConfig.h>
5
#include <xrpl/nodestore/DummyScheduler.h>
6
#include <xrpl/nodestore/Manager.h>
7
8
#include <gtest/gtest.h>
9
#include <helpers/CaptureSink.h>
10
#include <helpers/TestSink.h>
11
#include <nodestore/TestBase.h>
12
13
#include <
array
>
14
#include <
cstddef
>
15
#include <
exception
>
16
#include <
memory
>
17
#include <
string
>
18
#include <
utility
>
19
#include <
vector
>
20
21
namespace
xrpl::node_store
{
22
23
namespace
{
24
25
Section
26
makeSection(std::string
const
& path, std::string
const
& blockSize =
""
)
27
{
28
Section params;
29
params.set(
"type"
,
"nudb"
);
30
params.set(
"path"
, path);
31
if
(!blockSize.empty())
32
params.set(
"nudb_block_size"
, blockSize);
33
return
params;
34
}
35
36
void
37
runRoundTrip(Section
const
& params, std::size_t expectedBlocksize)
38
{
39
DummyScheduler
scheduler;
40
beast::Journal
const
journal(
TestSink::instance
());
41
auto
backend =
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
42
43
ASSERT_TRUE(backend);
44
ASSERT_EQ(backend->getBlockSize(), expectedBlocksize);
45
backend->open();
46
ASSERT_TRUE(backend->isOpen());
47
48
auto
const
batch =
createPredictableBatch
(10, 12345);
49
storeBatch
(*backend, batch);
50
51
auto
const
copy
=
fetchCopyOfBatch
(*backend, batch);
52
53
backend->close();
54
EXPECT_EQ(batch, copy);
55
}
56
57
}
// namespace
58
59
TEST
(
NuDBFactory
, default_block_size)
60
{
61
TempDir
const
tempDir;
62
auto
const
params = makeSection(tempDir.
path
());
63
ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 4096));
64
}
65
66
TEST
(
NuDBFactory
, valid_block_sizes)
67
{
68
auto
const
kValidSizes = std::to_array<std::size_t>({4096, 8192, 16384, 32768});
69
for
(
auto
const
size : kValidSizes)
70
{
71
SCOPED_TRACE(
"size="
+
std::to_string
(size));
72
TempDir
const
tempDir;
73
auto
const
params = makeSection(tempDir.
path
(),
std::to_string
(size));
74
ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, size));
75
}
76
77
// empty value is ignored by config parser; default (4096) is used
78
{
79
TempDir
const
tempDir;
80
auto
const
params = makeSection(tempDir.
path
(),
""
);
81
ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 4096));
82
}
83
}
84
85
TEST
(
NuDBFactory
, invalid_block_sizes)
86
{
87
std::vector<std::string>
const
kInvalidSizes = {
88
"2048"
,
// too small
89
"1024"
,
// too small
90
"65536"
,
// too large
91
"131072"
,
// too large
92
"5000"
,
// not power of 2
93
"6000"
,
// not power of 2
94
"10000"
,
// not power of 2
95
"0"
,
// zero
96
"-1"
,
// negative
97
"abc"
,
// non-numeric
98
"4k"
,
// invalid format
99
"4096.5"
,
// decimal
100
};
101
102
for
(
auto
const
& size : kInvalidSizes)
103
{
104
SCOPED_TRACE(
"size='"
+ size +
"'"
);
105
TempDir
const
tempDir;
106
auto
const
params = makeSection(tempDir.
path
(), size);
107
EXPECT_THROW(runRoundTrip(params, 4096),
std::exception
);
108
}
109
110
// whitespace handling — lexical_cast may or may not strip; treat as invalid
111
std::vector<std::string>
const
kWhitespaceSizes = {
"4096 "
,
" 4096"
};
112
for
(
auto
const
& size : kWhitespaceSizes)
113
{
114
SCOPED_TRACE(
"size='"
+ size +
"'"
);
115
TempDir
const
tempDir;
116
auto
const
params = makeSection(tempDir.
path
(), size);
117
EXPECT_THROW(runRoundTrip(params, 4096),
std::exception
);
118
}
119
}
120
121
TEST
(
NuDBFactory
, log_messages)
122
{
123
// valid custom block size emits info log
124
{
125
TempDir
const
tempDir;
126
auto
const
params = makeSection(tempDir.
path
(),
"8192"
);
127
test::CaptureSink
sink(
beast::Severity::Info
);
128
beast::Journal
const
journal(sink);
129
130
DummyScheduler
scheduler;
131
[[maybe_unused]]
auto
const
backend =
132
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
133
134
EXPECT_TRUE(sink.
messages
().contains(
"Using custom NuDB block size: 8192"
));
135
}
136
137
// invalid block size throws with informative message
138
{
139
TempDir
const
tempDir;
140
auto
const
params = makeSection(tempDir.
path
(),
"5000"
);
141
test::CaptureSink
sink(
beast::Severity::Warning
);
142
beast::Journal
const
journal(sink);
143
DummyScheduler
scheduler;
144
try
145
{
146
auto
const
backend =
147
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
148
FAIL() <<
"expected exception for invalid block size 5000"
;
149
}
150
catch
(
std::exception
const
& e)
151
{
152
std::string
const
what{e.
what
()};
153
EXPECT_TRUE(what.contains(
"Invalid nudb_block_size: 5000"
));
154
EXPECT_TRUE(what.contains(
"Must be power of 2 between 4096 and 32768"
));
155
}
156
}
157
158
// non-numeric value throws
159
{
160
TempDir
const
tempDir;
161
auto
const
params = makeSection(tempDir.
path
(),
"invalid"
);
162
test::CaptureSink
sink(
beast::Severity::Warning
);
163
beast::Journal
const
journal(sink);
164
DummyScheduler
scheduler;
165
try
166
{
167
auto
const
backend =
168
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
169
FAIL() <<
"expected exception for non-numeric block size"
;
170
}
171
catch
(
std::exception
const
& e)
172
{
173
std::string
const
what{e.
what
()};
174
EXPECT_TRUE(what.contains(
"Invalid nudb_block_size value: invalid"
));
175
}
176
}
177
}
178
179
TEST
(
NuDBFactory
, power_of_two_validation)
180
{
181
std::vector<std::pair<std::string, bool>
>
const
kCASES = {
182
{
"4095"
,
false
},
// just below minimum
183
{
"4096"
,
true
},
// minimum valid
184
{
"4097"
,
false
},
// not power of 2
185
{
"8192"
,
true
},
// valid power of 2
186
{
"8193"
,
false
},
// not power of 2
187
{
"16384"
,
true
},
// valid power of 2
188
{
"32768"
,
true
},
// maximum valid
189
{
"32769"
,
false
},
// just above maximum
190
{
"65536"
,
false
},
// power of 2 but too large
191
};
192
193
for
(
auto
const
& [size, shouldWork] : kCASES)
194
{
195
SCOPED_TRACE(
"size="
+ size +
" shouldWork="
+ (shouldWork ?
"true"
:
"false"
));
196
TempDir
const
tempDir;
197
auto
const
params = makeSection(tempDir.
path
(), size);
198
test::CaptureSink
sink(
beast::Severity::Warning
);
199
beast::Journal
const
journal(sink);
200
DummyScheduler
scheduler;
201
try
202
{
203
auto
const
backend =
204
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
205
EXPECT_TRUE(shouldWork);
206
}
207
catch
(
std::exception
const
& e)
208
{
209
// A throw is only expected for sizes that should NOT work; if a
210
// valid size throws, fail here instead of silently matching the
211
// message below (which would mask the regression).
212
EXPECT_FALSE(shouldWork);
213
std::string
const
what{e.
what
()};
214
EXPECT_TRUE(what.contains(
"Invalid nudb_block_size"
));
215
}
216
}
217
}
218
219
TEST
(
NuDBFactory
, both_constructor_variants)
220
{
221
TempDir
const
tempDir;
222
auto
const
params = makeSection(tempDir.
path
(),
"16384"
);
223
DummyScheduler
scheduler;
224
beast::Journal
const
journal(
TestSink::instance
());
225
226
auto
const
backend1 =
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
227
EXPECT_NE(backend1,
nullptr
);
228
ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 16384));
229
230
// Test second constructor (with nudb::context)
231
// Note: This would require access to nudb::context, which might not be
232
// easily testable without more complex setup. For now, we test that
233
// the factory can create backends with the first constructor.
234
}
235
236
TEST
(
NuDBFactory
, configuration_parsing)
237
{
238
// basic valid format emits success log
239
{
240
TempDir
const
tempDir;
241
auto
const
params = makeSection(tempDir.
path
(),
"8192"
);
242
test::CaptureSink
sink(
beast::Severity::Info
);
243
beast::Journal
const
journal(sink);
244
DummyScheduler
scheduler;
245
[[maybe_unused]]
auto
const
backend =
246
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
247
EXPECT_TRUE(sink.
messages
().contains(
"Using custom NuDB block size"
));
248
}
249
250
// Test whitespace handling separately since lexical_cast behavior may vary
251
std::vector<std::string>
const
kWhitespaceFormats = {
" 8192"
,
"8192 "
};
252
for
(
auto
const
& format : kWhitespaceFormats)
253
{
254
SCOPED_TRACE(
"format='"
+ format +
"'"
);
255
TempDir
const
tempDir;
256
auto
const
params = makeSection(tempDir.
path
(), format);
257
test::CaptureSink
sink(
beast::Severity::Debug
);
258
beast::Journal
const
journal(sink);
259
DummyScheduler
scheduler;
260
EXPECT_ANY_THROW(
Manager::instance
().makeBackend(params,
megabytes
(4), scheduler, journal));
261
}
262
}
263
264
TEST
(
NuDBFactory
, data_persistence)
265
{
266
std::vector<std::string>
const
kBlockSizes = {
"4096"
,
"8192"
,
"16384"
,
"32768"
};
267
for
(
auto
const
& size : kBlockSizes)
268
{
269
SCOPED_TRACE(
"size="
+ size);
270
TempDir
const
tempDir;
271
auto
const
params = makeSection(tempDir.
path
(), size);
272
DummyScheduler
scheduler;
273
beast::Journal
const
journal(
TestSink::instance
());
274
275
// Create test data
276
auto
const
batch =
createPredictableBatch
(50, 54321);
277
278
// Store data
279
{
280
auto
backend =
281
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
282
backend->open();
283
storeBatch
(*backend, batch);
284
backend->close();
285
}
286
287
// Retrieve data in new backend instance
288
{
289
auto
backend =
290
Manager::instance
().
makeBackend
(params,
megabytes
(4), scheduler, journal);
291
backend->open();
292
auto
const
copy =
fetchCopyOfBatch
(*backend, batch);
293
EXPECT_EQ(batch, copy);
294
backend->close();
295
}
296
}
297
}
298
299
}
// namespace xrpl::node_store
array
std::string
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::TempDir
RAII temporary directory.
Definition
FileUtilities.h:44
xrpl::TempDir::path
std::string path() const
Get the native path for the temporary directory.
Definition
libxrpl/basics/FileUtilities.cpp:130
xrpl::TestSink::instance
static TestSink & instance()
Definition
TestSink.h:12
xrpl::node_store::DummyScheduler
Simple NodeStore Scheduler that just performs the tasks synchronously.
Definition
DummyScheduler.h:12
xrpl::node_store::Manager::makeBackend
virtual std::unique_ptr< Backend > makeBackend(Section const ¶meters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
xrpl::node_store::Manager::instance
static Manager & instance()
Returns the instance of the manager singleton.
Definition
ManagerImp.cpp:133
xrpl::node_store::NuDBFactory
Definition
libxrpl/nodestore/backend/NuDBFactory.cpp:398
xrpl::test::CaptureSink
Definition
CaptureSink.h:12
xrpl::test::CaptureSink::messages
std::string messages() const
Definition
CaptureSink.h:41
std::copy
T copy(T... args)
cstddef
exception
memory
beast::Severity::Warning
@ Warning
Definition
Journal.h:22
beast::Severity::Info
@ Info
Definition
Journal.h:21
beast::Severity::Debug
@ Debug
Definition
Journal.h:20
xrpl::node_store
Definition
ServiceRegistry.h:18
xrpl::node_store::TEST
TEST(NodeStoreBasics, predictable_batches)
Definition
Basics.cpp:14
xrpl::node_store::createPredictableBatch
Batch createPredictableBatch(std::size_t numObjects, std::uint64_t seed)
Definition
TestBase.h:48
xrpl::node_store::storeBatch
void storeBatch(Backend &backend, Batch const &batch)
Definition
TestBase.h:85
xrpl::node_store::fetchCopyOfBatch
Batch fetchCopyOfBatch(Backend &backend, Batch const &batch)
Definition
TestBase.h:92
xrpl::megabytes
constexpr auto megabytes(T value) noexcept
Definition
ByteUtilities.h:14
string
std::to_string
T to_string(T... args)
utility
vector
std::exception::what
T what(T... args)
Generated by
1.17.0