@@ -42,50 +42,48 @@ static const unsigned int QUEUE_BATCH_SIZE = 128;
42
42
static const int SCRIPT_CHECK_THREADS = 3 ;
43
43
44
44
struct FakeCheck {
45
- bool operator ()() const
45
+ std::optional< int > operator ()() const
46
46
{
47
- return true ;
47
+ return std::nullopt ;
48
48
}
49
49
};
50
50
51
51
struct FakeCheckCheckCompletion {
52
52
static std::atomic<size_t > n_calls;
53
- bool operator ()()
53
+ std::optional< int > operator ()()
54
54
{
55
55
n_calls.fetch_add (1 , std::memory_order_relaxed);
56
- return true ;
56
+ return std::nullopt ;
57
57
}
58
58
};
59
59
60
- struct FailingCheck {
61
- bool fails;
62
- FailingCheck (bool _fails) : fails(_fails){};
63
- bool operator ()() const
64
- {
65
- return !fails;
66
- }
60
+ struct FixedCheck
61
+ {
62
+ std::optional<int > m_result;
63
+ FixedCheck (std::optional<int > result) : m_result(result){};
64
+ std::optional<int > operator ()() const { return m_result; }
67
65
};
68
66
69
67
struct UniqueCheck {
70
68
static Mutex m;
71
69
static std::unordered_multiset<size_t > results GUARDED_BY (m);
72
70
size_t check_id;
73
71
UniqueCheck (size_t check_id_in) : check_id(check_id_in){};
74
- bool operator ()()
72
+ std::optional< int > operator ()()
75
73
{
76
74
LOCK (m);
77
75
results.insert (check_id);
78
- return true ;
76
+ return std::nullopt ;
79
77
}
80
78
};
81
79
82
80
83
81
struct MemoryCheck {
84
82
static std::atomic<size_t > fake_allocated_memory;
85
83
bool b {false };
86
- bool operator ()() const
84
+ std::optional< int > operator ()() const
87
85
{
88
- return true ;
86
+ return std::nullopt ;
89
87
}
90
88
MemoryCheck (const MemoryCheck& x)
91
89
{
@@ -110,9 +108,9 @@ struct FrozenCleanupCheck {
110
108
static std::condition_variable cv;
111
109
static std::mutex m;
112
110
bool should_freeze{true };
113
- bool operator ()() const
111
+ std::optional< int > operator ()() const
114
112
{
115
- return true ;
113
+ return std::nullopt ;
116
114
}
117
115
FrozenCleanupCheck () = default ;
118
116
~FrozenCleanupCheck ()
@@ -149,7 +147,7 @@ std::atomic<size_t> MemoryCheck::fake_allocated_memory{0};
149
147
// Queue Typedefs
150
148
typedef CCheckQueue<FakeCheckCheckCompletion> Correct_Queue;
151
149
typedef CCheckQueue<FakeCheck> Standard_Queue;
152
- typedef CCheckQueue<FailingCheck> Failing_Queue ;
150
+ typedef CCheckQueue<FixedCheck> Fixed_Queue ;
153
151
typedef CCheckQueue<UniqueCheck> Unique_Queue;
154
152
typedef CCheckQueue<MemoryCheck> Memory_Queue;
155
153
typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
@@ -174,7 +172,7 @@ void CheckQueueTest::Correct_Queue_range(std::vector<size_t> range)
174
172
total -= vChecks.size ();
175
173
control.Add (std::move (vChecks));
176
174
}
177
- BOOST_REQUIRE (control.Wait ());
175
+ BOOST_REQUIRE (! control.Complete (). has_value ());
178
176
BOOST_REQUIRE_EQUAL (FakeCheckCheckCompletion::n_calls, i);
179
177
}
180
178
}
@@ -217,45 +215,45 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
217
215
}
218
216
219
217
220
- /* * Test that failing checks are caught */
218
+ /* * Test that distinct failing checks are caught */
221
219
BOOST_AUTO_TEST_CASE (test_CheckQueue_Catches_Failure)
222
220
{
223
- auto fail_queue = std::make_unique<Failing_Queue >(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
221
+ auto fixed_queue = std::make_unique<Fixed_Queue >(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
224
222
for (size_t i = 0 ; i < 1001 ; ++i) {
225
- CCheckQueueControl<FailingCheck > control (fail_queue .get ());
223
+ CCheckQueueControl<FixedCheck > control (fixed_queue .get ());
226
224
size_t remaining = i;
227
225
while (remaining) {
228
226
size_t r = m_rng.randrange (10 );
229
227
230
- std::vector<FailingCheck > vChecks;
228
+ std::vector<FixedCheck > vChecks;
231
229
vChecks.reserve (r);
232
230
for (size_t k = 0 ; k < r && remaining; k++, remaining--)
233
- vChecks.emplace_back (remaining == 1 );
231
+ vChecks.emplace_back (remaining == 1 ? std::make_optional< int >( 17 * i) : std::nullopt );
234
232
control.Add (std::move (vChecks));
235
233
}
236
- bool success = control.Wait ();
234
+ auto result = control.Complete ();
237
235
if (i > 0 ) {
238
- BOOST_REQUIRE (!success );
239
- } else if (i == 0 ) {
240
- BOOST_REQUIRE (success );
236
+ BOOST_REQUIRE (result. has_value () && *result == static_cast < int >( 17 * i) );
237
+ } else {
238
+ BOOST_REQUIRE (!result. has_value () );
241
239
}
242
240
}
243
241
}
244
242
// Test that a block validation which fails does not interfere with
245
243
// future blocks, ie, the bad state is cleared.
246
244
BOOST_AUTO_TEST_CASE (test_CheckQueue_Recovers_From_Failure)
247
245
{
248
- auto fail_queue = std::make_unique<Failing_Queue >(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
246
+ auto fail_queue = std::make_unique<Fixed_Queue >(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
249
247
for (auto times = 0 ; times < 10 ; ++times) {
250
248
for (const bool end_fails : {true , false }) {
251
- CCheckQueueControl<FailingCheck > control (fail_queue.get ());
249
+ CCheckQueueControl<FixedCheck > control (fail_queue.get ());
252
250
{
253
- std::vector<FailingCheck > vChecks;
254
- vChecks.resize (100 , false );
255
- vChecks[99 ] = end_fails;
251
+ std::vector<FixedCheck > vChecks;
252
+ vChecks.resize (100 , FixedCheck (std::nullopt) );
253
+ vChecks[99 ] = FixedCheck ( end_fails ? std::make_optional< int >( 2 ) : std::nullopt) ;
256
254
control.Add (std::move (vChecks));
257
255
}
258
- bool r =control.Wait ();
256
+ bool r = ! control.Complete (). has_value ();
259
257
BOOST_REQUIRE (r != end_fails);
260
258
}
261
259
}
@@ -329,8 +327,8 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
329
327
CCheckQueueControl<FrozenCleanupCheck> control (queue.get ());
330
328
std::vector<FrozenCleanupCheck> vChecks (1 );
331
329
control.Add (std::move (vChecks));
332
- bool waitResult = control.Wait (); // Hangs here
333
- assert (waitResult );
330
+ auto result = control.Complete (); // Hangs here
331
+ assert (!result );
334
332
});
335
333
{
336
334
std::unique_lock<std::mutex> l (FrozenCleanupCheck::m);
0 commit comments