Skip to content

Commit d009014

Browse files
committed
fixed merge fail
1 parent 958db64 commit d009014

File tree

1 file changed

+108
-58
lines changed

1 file changed

+108
-58
lines changed

reference/async_reference/test/async_ref_test.cc

Lines changed: 108 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{
@@ -33,6 +29,15 @@ void async_test::TryEntity::impl(context& ctx) {
3329
auto action = ctx.action();
3430
}
3531

32+
void assert_time_past(
33+
std::chrono::time_point<std::chrono::high_resolution_clock> start_time,
34+
std::chrono::milliseconds time_to_assert
35+
) {
36+
auto wait_end = std::chrono::high_resolution_clock::now();
37+
auto time = duration_cast<std::chrono::milliseconds>(wait_end - start_time);
38+
ASSERT_LT(time, time_to_assert);
39+
}
40+
3641
TEST(AsyncRef, ConnectBad) {
3742
auto connect_req_id = ecsact_async_connect("bad");
3843

@@ -80,6 +85,8 @@ TEST(AsyncRef, Disconnect) {
8085

8186
TEST(AsyncRef, AddUpdateAndRemove) {
8287
using namespace std::chrono_literals;
88+
using clock = std::chrono::high_resolution_clock;
89+
using std::chrono::duration_cast;
8390

8491
// In this test we're adding to components and then immediately updating one
8592
// and then removing it.
@@ -93,6 +100,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
93100
struct callback_data {
94101
std::atomic_bool wait = false;
95102
ecsact_entity_id entity = {};
103+
96104
std::atomic_bool init_happened = false;
97105
std::atomic_bool update_happened = false;
98106
std::atomic_bool remove_happened = false;
@@ -111,6 +119,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
111119
void* callback_user_data
112120
) {
113121
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
122+
114123
cb_info.wait = true;
115124
cb_info.entity = entity_id;
116125
};
@@ -119,12 +128,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
119128
entity_async_evc.async_entity_callback = entity_cb;
120129
entity_async_evc.async_entity_callback_user_data = &cb_info;
121130

122-
int check_count = 0;
123-
131+
auto start_tick = ecsact_async_get_current_tick();
124132
while(cb_info.wait != true) {
125-
ASSERT_LT(++check_count, 100);
126133
ecsact_async_flush_events(nullptr, &entity_async_evc);
127-
std::this_thread::sleep_for(25ms);
134+
auto current_tick = ecsact_async_get_current_tick();
135+
auto tick_diff = current_tick - start_tick;
136+
ASSERT_LT(tick_diff, 10);
128137
}
129138

130139
// Preparing add component data
@@ -185,11 +194,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
185194
}
186195
};
187196

188-
check_count = 0;
197+
start_tick = ecsact_async_get_current_tick();
189198
while(!cb_info.init_happened) {
190-
ASSERT_LT(++check_count, 100);
191199
ecsact_async_flush_events(&evc, nullptr);
192-
std::this_thread::sleep_for(25ms);
200+
auto current_tick = ecsact_async_get_current_tick();
201+
auto tick_diff = current_tick - start_tick;
202+
ASSERT_LT(tick_diff, 10);
193203
}
194204

195205
evc.init_callback = {};
@@ -204,8 +214,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
204214
void* callback_user_data
205215
) {
206216
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
207-
cb_info.update_happened = true;
208217

218+
cb_info.update_happened = true;
209219
ASSERT_EQ(component_id, async_test::ComponentUpdate::id);
210220

211221
auto comp =
@@ -221,11 +231,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
221231
update_options.update_components = update_components.data();
222232
ecsact_async_enqueue_execution_options(update_options);
223233

224-
check_count = 0;
234+
start_tick = ecsact_async_get_current_tick();
225235
while(!cb_info.update_happened) {
226-
ASSERT_LT(++check_count, 100);
227236
ecsact_async_flush_events(&evc, nullptr);
228-
std::this_thread::sleep_for(25ms);
237+
auto current_tick = ecsact_async_get_current_tick();
238+
auto tick_diff = current_tick - start_tick;
239+
ASSERT_LT(tick_diff, 10);
229240
}
230241

231242
evc.update_callback = {};
@@ -252,8 +263,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
252263
void* callback_user_data
253264
) {
254265
auto& cb_info = *static_cast<callback_data*>(callback_user_data);
255-
cb_info.remove_happened = true;
256266

267+
cb_info.remove_happened = true;
257268
ASSERT_EQ(component_id, async_test::ComponentUpdate::id);
258269

259270
auto comp =
@@ -262,25 +273,33 @@ TEST(AsyncRef, AddUpdateAndRemove) {
262273
ASSERT_EQ(comp->value_to_update, 6);
263274
};
264275

276+
start_tick = ecsact_async_get_current_tick();
265277
// Flush until we hit a maximum or get all our events we expect.
266-
check_count = 0;
267278
while(!cb_info.all_events_happened()) {
268-
ASSERT_LT(++check_count, 100) //
279+
ecsact_async_flush_events(&evc, nullptr);
280+
auto current_tick = ecsact_async_get_current_tick();
281+
auto tick_diff = current_tick - start_tick;
282+
ASSERT_LT(tick_diff, 10)
269283
<< "Not all events happened before maximum was reached\n"
270284
<< " init = " << cb_info.init_happened << "\n"
271285
<< " update = " << cb_info.update_happened << "\n"
272286
<< " remove = " << cb_info.remove_happened << "\n";
273-
ecsact_async_flush_events(&evc, nullptr);
274-
std::this_thread::sleep_for(25ms);
275287
}
276288

277289
ecsact_async_disconnect();
278290
}
279291

280292
TEST(AsyncRef, TryMergeFailure) {
281293
using namespace std::chrono_literals;
294+
using clock = std::chrono::high_resolution_clock;
295+
using std::chrono::duration_cast;
282296

283-
auto connect_req_id = ecsact_async_connect("good?tick_rate=25");
297+
struct entity_cb_info {
298+
ecsact_entity_id entity;
299+
bool wait;
300+
};
301+
302+
ecsact_async_connect("good?tick_rate=25");
284303

285304
auto my_needed_component = async_test::NeededComponent{};
286305
auto another_my_needed_component = async_test::NeededComponent{};
@@ -305,6 +324,7 @@ TEST(AsyncRef, TryMergeFailure) {
305324
) {
306325
entity_cb_info& entity_info =
307326
*static_cast<entity_cb_info*>(callback_user_data);
327+
308328
entity_info.wait = true;
309329
entity_info.entity = entity_id;
310330
};
@@ -315,15 +335,15 @@ TEST(AsyncRef, TryMergeFailure) {
315335
entity_async_evc.async_entity_callback = entity_cb;
316336
entity_async_evc.async_entity_callback_user_data = &cb_info;
317337

318-
int check_count = 0;
319-
338+
auto start_tick = ecsact_async_get_current_tick();
320339
while(cb_info.wait != true) {
321-
std::this_thread::sleep_for(25ms);
322-
ASSERT_LT(++check_count, 100);
323340
ecsact_async_flush_events(nullptr, &entity_async_evc);
341+
auto current_tick = ecsact_async_get_current_tick();
342+
auto tick_diff = current_tick - start_tick;
343+
ASSERT_LT(tick_diff, 10);
324344
}
325345

326-
struct callback_data {
346+
struct merge_callback_data {
327347
ecsact_async_request_id request_id;
328348
bool wait;
329349
};
@@ -335,9 +355,11 @@ TEST(AsyncRef, TryMergeFailure) {
335355
ecsact_async_request_id* request_ids,
336356
void* callback_user_data
337357
) {
358+
auto& cb_data = *static_cast<merge_callback_data*>(callback_user_data);
359+
338360
ASSERT_EQ(async_err, ECSACT_ASYNC_ERR_EXECUTION_MERGE_FAILURE);
339-
auto& cb_data = *static_cast<callback_data*>(callback_user_data);
340-
auto request_id = request_ids[0];
361+
362+
auto request_id = request_ids[0];
341363
ASSERT_EQ(cb_data.request_id, request_id);
342364
cb_data.wait = true;
343365
};
@@ -355,28 +377,30 @@ TEST(AsyncRef, TryMergeFailure) {
355377

356378
auto options_request = ecsact_async_enqueue_execution_options(options);
357379

358-
callback_data cb_data{
380+
merge_callback_data merge_cb_info{
359381
.request_id = options_request,
360382
.wait = false,
361383
};
362384

363385
ecsact_async_events_collector async_evc{};
364386
async_evc.async_error_callback = async_err_cb;
365-
async_evc.async_error_callback_user_data = &cb_data;
387+
async_evc.async_error_callback_user_data = &merge_cb_info;
366388

367-
check_count = 0;
368-
369-
while(cb_data.wait != true) {
370-
std::this_thread::sleep_for(25ms);
371-
ASSERT_LT(++check_count, 100);
389+
start_tick = ecsact_async_get_current_tick();
390+
while(merge_cb_info.wait != true) {
372391
ecsact_async_flush_events(nullptr, &async_evc);
392+
auto current_tick = ecsact_async_get_current_tick();
393+
auto tick_diff = current_tick - start_tick;
394+
ASSERT_LT(tick_diff, 10);
373395
}
374396

375397
ecsact_async_disconnect();
376398
}
377399

378400
TEST(AsyncRef, ReceiveMultipleEntities) {
379401
using namespace std::chrono_literals;
402+
using clock = std::chrono::high_resolution_clock;
403+
using std::chrono::duration_cast;
380404

381405
auto connect_req_id = ecsact_async_connect("good?tick_rate=25");
382406

@@ -388,8 +412,9 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
388412
ecsact_entity_id entity;
389413

390414
struct entity_cb_info {
391-
ecsact_entity_id& entity;
392-
int& counter;
415+
ecsact_entity_id& entity;
416+
std::array<int, 10> entity_request_ids;
417+
int& counter;
393418
};
394419

395420
auto entity_cb = //
@@ -400,34 +425,50 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
400425
) {
401426
entity_cb_info& entity_info =
402427
*static_cast<entity_cb_info*>(callback_user_data);
428+
entity_info.entity_request_ids[entity_info.counter] = entity_info.counter;
403429
entity_info.counter++;
404430
};
405431

406-
entity_cb_info entity_info{.entity = entity, .counter = counter};
432+
entity_cb_info entity_info{
433+
.entity = entity,
434+
.entity_request_ids = {},
435+
.counter = counter};
407436

408437
ecsact_async_events_collector entity_async_evc{};
409438
entity_async_evc.async_entity_callback = entity_cb;
410439
entity_async_evc.async_entity_callback_user_data = &entity_info;
411440

412-
int check_count = 0;
413-
441+
auto start_tick = ecsact_async_get_current_tick();
414442
while(counter < 10) {
415-
std::this_thread::sleep_for(25ms);
416-
ASSERT_LT(++check_count, 100);
417443
ecsact_async_flush_events(nullptr, &entity_async_evc);
444+
auto current_tick = ecsact_async_get_current_tick();
445+
auto tick_diff = current_tick - start_tick;
446+
ASSERT_LT(tick_diff, 10);
447+
}
448+
449+
for(int i = 0; i < entity_info.entity_request_ids.size(); i++) {
450+
ASSERT_EQ(i, entity_info.entity_request_ids[i]);
418451
}
419452

420453
ecsact_async_disconnect();
421454
}
422455

423456
TEST(AsyncRef, TryAction) {
424457
using namespace std::chrono_literals;
458+
using clock = std::chrono::high_resolution_clock;
459+
using std::chrono::duration_cast;
460+
425461
static std::atomic_bool reached_system = false;
426462

427463
ecsact_async_connect("good?tick_rate=25");
428464

429465
auto entity_request = ecsact_async_create_entity();
430466

467+
struct entity_cb_info {
468+
ecsact_entity_id entity;
469+
bool wait;
470+
};
471+
431472
static entity_cb_info cb_info;
432473

433474
auto entity_cb = //
@@ -437,6 +478,7 @@ TEST(AsyncRef, TryAction) {
437478
void* callback_user_data
438479
) {
439480
auto& entity_info = *static_cast<entity_cb_info*>(callback_user_data);
481+
440482
entity_info.wait = true;
441483
entity_info.entity = entity_id;
442484
};
@@ -445,20 +487,24 @@ TEST(AsyncRef, TryAction) {
445487
entity_async_evc.async_entity_callback = entity_cb;
446488
entity_async_evc.async_entity_callback_user_data = &cb_info;
447489

448-
int check_count = 0;
449-
490+
auto start_tick = ecsact_async_get_current_tick();
450491
while(cb_info.wait != true) {
451-
std::this_thread::sleep_for(25ms);
452-
ASSERT_LT(++check_count, 100);
453492
ecsact_async_flush_events(nullptr, &entity_async_evc);
493+
auto current_tick = ecsact_async_get_current_tick();
494+
auto tick_diff = current_tick - start_tick;
495+
ASSERT_LT(tick_diff, 10);
454496
}
455497

456-
bool wait = false;
498+
struct callback_info {
499+
bool wait = false;
500+
};
501+
502+
callback_info init_cb_info{};
457503

458504
// Prepare the events collector for the flush to make sure we got all the
459505
// events we expected.
460506
auto evc = ecsact_execution_events_collector{};
461-
evc.init_callback_user_data = &wait;
507+
evc.init_callback_user_data = &init_cb_info;
462508
evc.init_callback = //
463509
[](
464510
ecsact_event event,
@@ -467,8 +513,10 @@ TEST(AsyncRef, TryAction) {
467513
const void* component_data,
468514
void* callback_user_data
469515
) {
470-
auto& wait = *static_cast<bool*>(callback_user_data);
471-
wait = true;
516+
auto wait_end = clock::now();
517+
auto& info = *static_cast<callback_info*>(callback_user_data);
518+
519+
info.wait = true;
472520
};
473521

474522
// Declare components required for the action
@@ -501,11 +549,12 @@ TEST(AsyncRef, TryAction) {
501549

502550
ecsact_async_enqueue_execution_options(options);
503551

504-
check_count = 0;
505-
while(!wait) {
506-
ASSERT_LT(++check_count, 100);
552+
start_tick = ecsact_async_get_current_tick();
553+
while(!cb_info.wait) {
507554
ecsact_async_flush_events(&evc, nullptr);
508-
std::this_thread::sleep_for(25ms);
555+
auto current_tick = ecsact_async_get_current_tick();
556+
auto tick_diff = current_tick - start_tick;
557+
ASSERT_LT(tick_diff, 10);
509558
}
510559

511560
async_test::TryEntity my_try_entity;
@@ -538,10 +587,11 @@ TEST(AsyncRef, TryAction) {
538587

539588
auto options_request = ecsact_async_enqueue_execution_options(options);
540589

541-
check_count = 0;
590+
start_tick = ecsact_async_get_current_tick();
542591
while(reached_system != true) {
543-
std::this_thread::sleep_for(25ms);
544-
ASSERT_LT(++check_count, 100);
592+
auto current_tick = ecsact_async_get_current_tick();
593+
auto tick_diff = current_tick - start_tick;
594+
ASSERT_LT(tick_diff, 10);
545595
}
546596

547597
ecsact_async_disconnect();

0 commit comments

Comments
 (0)