Skip to content

Commit 2fd771c

Browse files
committed
fixed merge fail
1 parent 557bcfa commit 2fd771c

File tree

1 file changed

+99
-58
lines changed

1 file changed

+99
-58
lines changed

tests/async/async_ref_test.cc

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
#include "async_test.ecsact.systems.hh"
1212

1313
using namespace std::chrono_literals;
14-
15-
struct entity_cb_info {
16-
ecsact_entity_id entity;
17-
bool wait;
18-
};
14+
using std::chrono::duration_cast;
1915

2016
void async_test::AddComponent::impl(context& ctx) {
2117
ctx.add(async_test::ComponentAddRemove{
@@ -98,6 +94,8 @@ TEST(AsyncRef, Disconnect) {
9894

9995
TEST(AsyncRef, AddUpdateAndRemove) {
10096
using namespace std::chrono_literals;
97+
using clock = std::chrono::high_resolution_clock;
98+
using std::chrono::duration_cast;
10199

102100
// In this test we're adding to components and then immediately updating one
103101
// and then removing it.
@@ -111,6 +109,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
111109
struct callback_data {
112110
std::atomic_bool wait = false;
113111
ecsact_entity_id entity = {};
112+
114113
std::atomic_bool init_happened = false;
115114
std::atomic_bool update_happened = false;
116115
std::atomic_bool remove_happened = false;
@@ -129,6 +128,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
129128
void* callback_user_data
130129
) {
131130
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
131+
132132
cb_info.wait = true;
133133
cb_info.entity = entity_id;
134134
};
@@ -137,12 +137,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
137137
entity_async_evc.async_entity_callback = entity_cb;
138138
entity_async_evc.async_entity_callback_user_data = &cb_info;
139139

140-
int check_count = 0;
141-
140+
auto start_tick = ecsact_async_get_current_tick();
142141
while(cb_info.wait != true) {
143-
ASSERT_LT(++check_count, 100);
144142
ecsact_async_flush_events(nullptr, &entity_async_evc);
145-
std::this_thread::sleep_for(25ms);
143+
auto current_tick = ecsact_async_get_current_tick();
144+
auto tick_diff = current_tick - start_tick;
145+
ASSERT_LT(tick_diff, 10);
146146
}
147147

148148
// Preparing add component data
@@ -203,11 +203,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
203203
}
204204
};
205205

206-
check_count = 0;
206+
start_tick = ecsact_async_get_current_tick();
207207
while(!cb_info.init_happened) {
208-
ASSERT_LT(++check_count, 100);
209208
ecsact_async_flush_events(&evc, nullptr);
210-
std::this_thread::sleep_for(25ms);
209+
auto current_tick = ecsact_async_get_current_tick();
210+
auto tick_diff = current_tick - start_tick;
211+
ASSERT_LT(tick_diff, 10);
211212
}
212213

213214
evc.init_callback = {};
@@ -222,8 +223,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
222223
void* callback_user_data
223224
) {
224225
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
225-
cb_info.update_happened = true;
226226

227+
cb_info.update_happened = true;
227228
ASSERT_EQ(component_id, async_test::ComponentUpdate::id);
228229

229230
auto comp =
@@ -239,11 +240,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
239240
update_options.update_components = update_components.data();
240241
ecsact_async_enqueue_execution_options(update_options);
241242

242-
check_count = 0;
243+
start_tick = ecsact_async_get_current_tick();
243244
while(!cb_info.update_happened) {
244-
ASSERT_LT(++check_count, 100);
245245
ecsact_async_flush_events(&evc, nullptr);
246-
std::this_thread::sleep_for(25ms);
246+
auto current_tick = ecsact_async_get_current_tick();
247+
auto tick_diff = current_tick - start_tick;
248+
ASSERT_LT(tick_diff, 10);
247249
}
248250

249251
evc.update_callback = {};
@@ -270,8 +272,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
270272
void* callback_user_data
271273
) {
272274
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
273-
cb_info.remove_happened = true;
274275

276+
cb_info.remove_happened = true;
275277
ASSERT_EQ(component_id, async_test::ComponentUpdate::id);
276278

277279
auto comp =
@@ -280,25 +282,33 @@ TEST(AsyncRef, AddUpdateAndRemove) {
280282
ASSERT_EQ(comp->value_to_update, 6);
281283
};
282284

285+
start_tick = ecsact_async_get_current_tick();
283286
// Flush until we hit a maximum or get all our events we expect.
284-
check_count = 0;
285287
while(!cb_info.all_events_happened()) {
286-
ASSERT_LT(++check_count, 100) //
288+
ecsact_async_flush_events(&evc, nullptr);
289+
auto current_tick = ecsact_async_get_current_tick();
290+
auto tick_diff = current_tick - start_tick;
291+
ASSERT_LT(tick_diff, 10)
287292
<< "Not all events happened before maximum was reached\n"
288293
<< " init = " << cb_info.init_happened << "\n"
289294
<< " update = " << cb_info.update_happened << "\n"
290295
<< " remove = " << cb_info.remove_happened << "\n";
291-
ecsact_async_flush_events(&evc, nullptr);
292-
std::this_thread::sleep_for(25ms);
293296
}
294297

295298
ecsact_async_disconnect();
296299
}
297300

298301
TEST(AsyncRef, TryMergeFailure) {
299302
using namespace std::chrono_literals;
303+
using clock = std::chrono::high_resolution_clock;
304+
using std::chrono::duration_cast;
300305

301-
auto connect_req_id = ecsact_async_connect("good?tick_rate=25");
306+
struct entity_cb_info {
307+
ecsact_entity_id entity;
308+
bool wait;
309+
};
310+
311+
ecsact_async_connect("good?tick_rate=25");
302312

303313
auto my_needed_component = async_test::NeededComponent{};
304314
auto another_my_needed_component = async_test::NeededComponent{};
@@ -323,6 +333,7 @@ TEST(AsyncRef, TryMergeFailure) {
323333
) {
324334
entity_cb_info& entity_info =
325335
*static_cast<entity_cb_info*>(callback_user_data);
336+
326337
entity_info.wait = true;
327338
entity_info.entity = entity_id;
328339
};
@@ -333,15 +344,15 @@ TEST(AsyncRef, TryMergeFailure) {
333344
entity_async_evc.async_entity_callback = entity_cb;
334345
entity_async_evc.async_entity_callback_user_data = &cb_info;
335346

336-
int check_count = 0;
337-
347+
auto start_tick = ecsact_async_get_current_tick();
338348
while(cb_info.wait != true) {
339-
std::this_thread::sleep_for(25ms);
340-
ASSERT_LT(++check_count, 100);
341349
ecsact_async_flush_events(nullptr, &entity_async_evc);
350+
auto current_tick = ecsact_async_get_current_tick();
351+
auto tick_diff = current_tick - start_tick;
352+
ASSERT_LT(tick_diff, 10);
342353
}
343354

344-
struct callback_data {
355+
struct merge_callback_data {
345356
ecsact_async_request_id request_id;
346357
bool wait;
347358
};
@@ -353,9 +364,11 @@ TEST(AsyncRef, TryMergeFailure) {
353364
ecsact_async_request_id* request_ids,
354365
void* callback_user_data
355366
) {
367+
auto& cb_data = *static_cast<merge_callback_data*>(callback_user_data);
368+
356369
ASSERT_EQ(async_err, ECSACT_ASYNC_ERR_EXECUTION_MERGE_FAILURE);
357-
auto& cb_data = *static_cast<callback_data*>(callback_user_data);
358-
auto request_id = request_ids[0];
370+
371+
auto request_id = request_ids[0];
359372
ASSERT_EQ(cb_data.request_id, request_id);
360373
cb_data.wait = true;
361374
};
@@ -373,28 +386,30 @@ TEST(AsyncRef, TryMergeFailure) {
373386

374387
auto options_request = ecsact_async_enqueue_execution_options(options);
375388

376-
callback_data cb_data{
389+
merge_callback_data merge_cb_info{
377390
.request_id = options_request,
378391
.wait = false,
379392
};
380393

381394
ecsact_async_events_collector async_evc{};
382395
async_evc.async_error_callback = async_err_cb;
383-
async_evc.async_error_callback_user_data = &cb_data;
384-
385-
check_count = 0;
396+
async_evc.async_error_callback_user_data = &merge_cb_info;
386397

387-
while(cb_data.wait != true) {
388-
std::this_thread::sleep_for(25ms);
389-
ASSERT_LT(++check_count, 100);
398+
start_tick = ecsact_async_get_current_tick();
399+
while(merge_cb_info.wait != true) {
390400
ecsact_async_flush_events(nullptr, &async_evc);
401+
auto current_tick = ecsact_async_get_current_tick();
402+
auto tick_diff = current_tick - start_tick;
403+
ASSERT_LT(tick_diff, 10);
391404
}
392405

393406
ecsact_async_disconnect();
394407
}
395408

396409
TEST(AsyncRef, ReceiveMultipleEntities) {
397410
using namespace std::chrono_literals;
411+
using clock = std::chrono::high_resolution_clock;
412+
using std::chrono::duration_cast;
398413

399414
auto connect_req_id = ecsact_async_connect("good?tick_rate=25");
400415

@@ -406,8 +421,9 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
406421
ecsact_entity_id entity;
407422

408423
struct entity_cb_info {
409-
ecsact_entity_id& entity;
410-
int& counter;
424+
ecsact_entity_id& entity;
425+
std::array<int, 10> entity_request_ids;
426+
int& counter;
411427
};
412428

413429
auto entity_cb = //
@@ -418,34 +434,50 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
418434
) {
419435
entity_cb_info& entity_info =
420436
*static_cast<entity_cb_info*>(callback_user_data);
437+
entity_info.entity_request_ids[entity_info.counter] = entity_info.counter;
421438
entity_info.counter++;
422439
};
423440

424-
entity_cb_info entity_info{.entity = entity, .counter = counter};
441+
entity_cb_info entity_info{
442+
.entity = entity,
443+
.entity_request_ids = {},
444+
.counter = counter};
425445

426446
ecsact_async_events_collector entity_async_evc{};
427447
entity_async_evc.async_entity_callback = entity_cb;
428448
entity_async_evc.async_entity_callback_user_data = &entity_info;
429449

430-
int check_count = 0;
431-
450+
auto start_tick = ecsact_async_get_current_tick();
432451
while(counter < 10) {
433-
std::this_thread::sleep_for(25ms);
434-
ASSERT_LT(++check_count, 100);
435452
ecsact_async_flush_events(nullptr, &entity_async_evc);
453+
auto current_tick = ecsact_async_get_current_tick();
454+
auto tick_diff = current_tick - start_tick;
455+
ASSERT_LT(tick_diff, 10);
456+
}
457+
458+
for(int i = 0; i < entity_info.entity_request_ids.size(); i++) {
459+
ASSERT_EQ(i, entity_info.entity_request_ids[i]);
436460
}
437461

438462
ecsact_async_disconnect();
439463
}
440464

441465
TEST(AsyncRef, TryAction) {
442466
using namespace std::chrono_literals;
467+
using clock = std::chrono::high_resolution_clock;
468+
using std::chrono::duration_cast;
469+
443470
static std::atomic_bool reached_system = false;
444471

445472
ecsact_async_connect("good?tick_rate=25");
446473

447474
auto entity_request = ecsact_async_create_entity();
448475

476+
struct entity_cb_info {
477+
ecsact_entity_id entity;
478+
bool wait;
479+
};
480+
449481
static entity_cb_info cb_info;
450482

451483
auto entity_cb = //
@@ -455,6 +487,7 @@ TEST(AsyncRef, TryAction) {
455487
void* callback_user_data
456488
) {
457489
auto& entity_info = *static_cast<entity_cb_info*>(callback_user_data);
490+
458491
entity_info.wait = true;
459492
entity_info.entity = entity_id;
460493
};
@@ -464,20 +497,24 @@ TEST(AsyncRef, TryAction) {
464497
entity_async_evc.async_entity_callback_user_data = &cb_info;
465498
entity_async_evc.async_error_callback = &assert_never_error;
466499

467-
int check_count = 0;
468-
500+
auto start_tick = ecsact_async_get_current_tick();
469501
while(cb_info.wait != true) {
470-
std::this_thread::sleep_for(25ms);
471-
ASSERT_LT(++check_count, 100);
472502
ecsact_async_flush_events(nullptr, &entity_async_evc);
503+
auto current_tick = ecsact_async_get_current_tick();
504+
auto tick_diff = current_tick - start_tick;
505+
ASSERT_LT(tick_diff, 10);
473506
}
474507

475-
bool wait = false;
508+
struct callback_info {
509+
bool wait = false;
510+
};
511+
512+
callback_info init_cb_info{};
476513

477514
// Prepare the events collector for the flush to make sure we got all the
478515
// events we expected.
479516
auto evc = ecsact_execution_events_collector{};
480-
evc.init_callback_user_data = &wait;
517+
evc.init_callback_user_data = &init_cb_info;
481518
evc.init_callback = //
482519
[](
483520
ecsact_event event,
@@ -486,8 +523,10 @@ TEST(AsyncRef, TryAction) {
486523
const void* component_data,
487524
void* callback_user_data
488525
) {
489-
auto& wait = *static_cast<bool*>(callback_user_data);
490-
wait = true;
526+
auto wait_end = clock::now();
527+
auto& info = *static_cast<callback_info*>(callback_user_data);
528+
529+
info.wait = true;
491530
};
492531

493532
// Declare components required for the action
@@ -520,11 +559,12 @@ TEST(AsyncRef, TryAction) {
520559

521560
ecsact_async_enqueue_execution_options(options);
522561

523-
check_count = 0;
524-
while(!wait) {
525-
ASSERT_LT(++check_count, 100);
562+
start_tick = ecsact_async_get_current_tick();
563+
while(!cb_info.wait) {
526564
ecsact_async_flush_events(&evc, nullptr);
527-
std::this_thread::sleep_for(25ms);
565+
auto current_tick = ecsact_async_get_current_tick();
566+
auto tick_diff = current_tick - start_tick;
567+
ASSERT_LT(tick_diff, 10);
528568
}
529569

530570
async_test::TryEntity my_try_entity;
@@ -557,10 +597,11 @@ TEST(AsyncRef, TryAction) {
557597

558598
auto options_request = ecsact_async_enqueue_execution_options(options);
559599

560-
check_count = 0;
600+
start_tick = ecsact_async_get_current_tick();
561601
while(reached_system != true) {
562-
std::this_thread::sleep_for(25ms);
563-
ASSERT_LT(++check_count, 100);
602+
auto current_tick = ecsact_async_get_current_tick();
603+
auto tick_diff = current_tick - start_tick;
604+
ASSERT_LT(tick_diff, 10);
564605
}
565606

566607
ecsact_async_disconnect();

0 commit comments

Comments
 (0)