11
11
#include " async_test.ecsact.systems.hh"
12
12
13
13
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;
19
15
20
16
void async_test::AddComponent::impl (context& ctx) {
21
17
ctx.add (async_test::ComponentAddRemove{
@@ -98,6 +94,8 @@ TEST(AsyncRef, Disconnect) {
98
94
99
95
TEST (AsyncRef, AddUpdateAndRemove) {
100
96
using namespace std ::chrono_literals;
97
+ using clock = std::chrono::high_resolution_clock;
98
+ using std::chrono::duration_cast;
101
99
102
100
// In this test we're adding to components and then immediately updating one
103
101
// and then removing it.
@@ -111,6 +109,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
111
109
struct callback_data {
112
110
std::atomic_bool wait = false ;
113
111
ecsact_entity_id entity = {};
112
+
114
113
std::atomic_bool init_happened = false ;
115
114
std::atomic_bool update_happened = false ;
116
115
std::atomic_bool remove_happened = false ;
@@ -129,6 +128,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
129
128
void * callback_user_data
130
129
) {
131
130
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
131
+
132
132
cb_info.wait = true ;
133
133
cb_info.entity = entity_id;
134
134
};
@@ -137,12 +137,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
137
137
entity_async_evc.async_entity_callback = entity_cb;
138
138
entity_async_evc.async_entity_callback_user_data = &cb_info;
139
139
140
- int check_count = 0 ;
141
-
140
+ auto start_tick = ecsact_async_get_current_tick ();
142
141
while (cb_info.wait != true ) {
143
- ASSERT_LT (++check_count, 100 );
144
142
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 );
146
146
}
147
147
148
148
// Preparing add component data
@@ -203,11 +203,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
203
203
}
204
204
};
205
205
206
- check_count = 0 ;
206
+ start_tick = ecsact_async_get_current_tick () ;
207
207
while (!cb_info.init_happened ) {
208
- ASSERT_LT (++check_count, 100 );
209
208
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 );
211
212
}
212
213
213
214
evc.init_callback = {};
@@ -222,8 +223,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
222
223
void * callback_user_data
223
224
) {
224
225
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
225
- cb_info.update_happened = true ;
226
226
227
+ cb_info.update_happened = true ;
227
228
ASSERT_EQ (component_id, async_test::ComponentUpdate::id);
228
229
229
230
auto comp =
@@ -239,11 +240,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
239
240
update_options.update_components = update_components.data ();
240
241
ecsact_async_enqueue_execution_options (update_options);
241
242
242
- check_count = 0 ;
243
+ start_tick = ecsact_async_get_current_tick () ;
243
244
while (!cb_info.update_happened ) {
244
- ASSERT_LT (++check_count, 100 );
245
245
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 );
247
249
}
248
250
249
251
evc.update_callback = {};
@@ -270,8 +272,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
270
272
void * callback_user_data
271
273
) {
272
274
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
273
- cb_info.remove_happened = true ;
274
275
276
+ cb_info.remove_happened = true ;
275
277
ASSERT_EQ (component_id, async_test::ComponentUpdate::id);
276
278
277
279
auto comp =
@@ -280,25 +282,33 @@ TEST(AsyncRef, AddUpdateAndRemove) {
280
282
ASSERT_EQ (comp->value_to_update , 6 );
281
283
};
282
284
285
+ start_tick = ecsact_async_get_current_tick ();
283
286
// Flush until we hit a maximum or get all our events we expect.
284
- check_count = 0 ;
285
287
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 )
287
292
<< " Not all events happened before maximum was reached\n "
288
293
<< " init = " << cb_info.init_happened << " \n "
289
294
<< " update = " << cb_info.update_happened << " \n "
290
295
<< " remove = " << cb_info.remove_happened << " \n " ;
291
- ecsact_async_flush_events (&evc, nullptr );
292
- std::this_thread::sleep_for (25ms);
293
296
}
294
297
295
298
ecsact_async_disconnect ();
296
299
}
297
300
298
301
TEST (AsyncRef, TryMergeFailure) {
299
302
using namespace std ::chrono_literals;
303
+ using clock = std::chrono::high_resolution_clock;
304
+ using std::chrono::duration_cast;
300
305
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" );
302
312
303
313
auto my_needed_component = async_test::NeededComponent{};
304
314
auto another_my_needed_component = async_test::NeededComponent{};
@@ -323,6 +333,7 @@ TEST(AsyncRef, TryMergeFailure) {
323
333
) {
324
334
entity_cb_info& entity_info =
325
335
*static_cast <entity_cb_info*>(callback_user_data);
336
+
326
337
entity_info.wait = true ;
327
338
entity_info.entity = entity_id;
328
339
};
@@ -333,15 +344,15 @@ TEST(AsyncRef, TryMergeFailure) {
333
344
entity_async_evc.async_entity_callback = entity_cb;
334
345
entity_async_evc.async_entity_callback_user_data = &cb_info;
335
346
336
- int check_count = 0 ;
337
-
347
+ auto start_tick = ecsact_async_get_current_tick ();
338
348
while (cb_info.wait != true ) {
339
- std::this_thread::sleep_for (25ms);
340
- ASSERT_LT (++check_count, 100 );
341
349
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 );
342
353
}
343
354
344
- struct callback_data {
355
+ struct merge_callback_data {
345
356
ecsact_async_request_id request_id;
346
357
bool wait;
347
358
};
@@ -353,9 +364,11 @@ TEST(AsyncRef, TryMergeFailure) {
353
364
ecsact_async_request_id* request_ids,
354
365
void * callback_user_data
355
366
) {
367
+ auto & cb_data = *static_cast <merge_callback_data*>(callback_user_data);
368
+
356
369
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 ];
359
372
ASSERT_EQ (cb_data.request_id , request_id);
360
373
cb_data.wait = true ;
361
374
};
@@ -373,28 +386,30 @@ TEST(AsyncRef, TryMergeFailure) {
373
386
374
387
auto options_request = ecsact_async_enqueue_execution_options (options);
375
388
376
- callback_data cb_data {
389
+ merge_callback_data merge_cb_info {
377
390
.request_id = options_request,
378
391
.wait = false ,
379
392
};
380
393
381
394
ecsact_async_events_collector async_evc{};
382
395
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;
386
397
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 ) {
390
400
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 );
391
404
}
392
405
393
406
ecsact_async_disconnect ();
394
407
}
395
408
396
409
TEST (AsyncRef, ReceiveMultipleEntities) {
397
410
using namespace std ::chrono_literals;
411
+ using clock = std::chrono::high_resolution_clock;
412
+ using std::chrono::duration_cast;
398
413
399
414
auto connect_req_id = ecsact_async_connect (" good?tick_rate=25" );
400
415
@@ -406,8 +421,9 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
406
421
ecsact_entity_id entity;
407
422
408
423
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;
411
427
};
412
428
413
429
auto entity_cb = //
@@ -418,34 +434,50 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
418
434
) {
419
435
entity_cb_info& entity_info =
420
436
*static_cast <entity_cb_info*>(callback_user_data);
437
+ entity_info.entity_request_ids [entity_info.counter ] = entity_info.counter ;
421
438
entity_info.counter ++;
422
439
};
423
440
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};
425
445
426
446
ecsact_async_events_collector entity_async_evc{};
427
447
entity_async_evc.async_entity_callback = entity_cb;
428
448
entity_async_evc.async_entity_callback_user_data = &entity_info;
429
449
430
- int check_count = 0 ;
431
-
450
+ auto start_tick = ecsact_async_get_current_tick ();
432
451
while (counter < 10 ) {
433
- std::this_thread::sleep_for (25ms);
434
- ASSERT_LT (++check_count, 100 );
435
452
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]);
436
460
}
437
461
438
462
ecsact_async_disconnect ();
439
463
}
440
464
441
465
TEST (AsyncRef, TryAction) {
442
466
using namespace std ::chrono_literals;
467
+ using clock = std::chrono::high_resolution_clock;
468
+ using std::chrono::duration_cast;
469
+
443
470
static std::atomic_bool reached_system = false ;
444
471
445
472
ecsact_async_connect (" good?tick_rate=25" );
446
473
447
474
auto entity_request = ecsact_async_create_entity ();
448
475
476
+ struct entity_cb_info {
477
+ ecsact_entity_id entity;
478
+ bool wait;
479
+ };
480
+
449
481
static entity_cb_info cb_info;
450
482
451
483
auto entity_cb = //
@@ -455,6 +487,7 @@ TEST(AsyncRef, TryAction) {
455
487
void * callback_user_data
456
488
) {
457
489
auto & entity_info = *static_cast <entity_cb_info*>(callback_user_data);
490
+
458
491
entity_info.wait = true ;
459
492
entity_info.entity = entity_id;
460
493
};
@@ -464,20 +497,24 @@ TEST(AsyncRef, TryAction) {
464
497
entity_async_evc.async_entity_callback_user_data = &cb_info;
465
498
entity_async_evc.async_error_callback = &assert_never_error;
466
499
467
- int check_count = 0 ;
468
-
500
+ auto start_tick = ecsact_async_get_current_tick ();
469
501
while (cb_info.wait != true ) {
470
- std::this_thread::sleep_for (25ms);
471
- ASSERT_LT (++check_count, 100 );
472
502
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 );
473
506
}
474
507
475
- bool wait = false ;
508
+ struct callback_info {
509
+ bool wait = false ;
510
+ };
511
+
512
+ callback_info init_cb_info{};
476
513
477
514
// Prepare the events collector for the flush to make sure we got all the
478
515
// events we expected.
479
516
auto evc = ecsact_execution_events_collector{};
480
- evc.init_callback_user_data = &wait ;
517
+ evc.init_callback_user_data = &init_cb_info ;
481
518
evc.init_callback = //
482
519
[](
483
520
ecsact_event event,
@@ -486,8 +523,10 @@ TEST(AsyncRef, TryAction) {
486
523
const void * component_data,
487
524
void * callback_user_data
488
525
) {
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 ;
491
530
};
492
531
493
532
// Declare components required for the action
@@ -520,11 +559,12 @@ TEST(AsyncRef, TryAction) {
520
559
521
560
ecsact_async_enqueue_execution_options (options);
522
561
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 ) {
526
564
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 );
528
568
}
529
569
530
570
async_test::TryEntity my_try_entity;
@@ -557,10 +597,11 @@ TEST(AsyncRef, TryAction) {
557
597
558
598
auto options_request = ecsact_async_enqueue_execution_options (options);
559
599
560
- check_count = 0 ;
600
+ start_tick = ecsact_async_get_current_tick () ;
561
601
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 );
564
605
}
565
606
566
607
ecsact_async_disconnect ();
0 commit comments