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{
@@ -33,6 +29,15 @@ void async_test::TryEntity::impl(context& ctx) {
33
29
auto action = ctx.action ();
34
30
}
35
31
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
+
36
41
TEST (AsyncRef, ConnectBad) {
37
42
auto connect_req_id = ecsact_async_connect (" bad" );
38
43
@@ -80,6 +85,8 @@ TEST(AsyncRef, Disconnect) {
80
85
81
86
TEST (AsyncRef, AddUpdateAndRemove) {
82
87
using namespace std ::chrono_literals;
88
+ using clock = std::chrono::high_resolution_clock;
89
+ using std::chrono::duration_cast;
83
90
84
91
// In this test we're adding to components and then immediately updating one
85
92
// and then removing it.
@@ -93,6 +100,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
93
100
struct callback_data {
94
101
std::atomic_bool wait = false ;
95
102
ecsact_entity_id entity = {};
103
+
96
104
std::atomic_bool init_happened = false ;
97
105
std::atomic_bool update_happened = false ;
98
106
std::atomic_bool remove_happened = false ;
@@ -111,6 +119,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
111
119
void * callback_user_data
112
120
) {
113
121
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
122
+
114
123
cb_info.wait = true ;
115
124
cb_info.entity = entity_id;
116
125
};
@@ -119,12 +128,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
119
128
entity_async_evc.async_entity_callback = entity_cb;
120
129
entity_async_evc.async_entity_callback_user_data = &cb_info;
121
130
122
- int check_count = 0 ;
123
-
131
+ auto start_tick = ecsact_async_get_current_tick ();
124
132
while (cb_info.wait != true ) {
125
- ASSERT_LT (++check_count, 100 );
126
133
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 );
128
137
}
129
138
130
139
// Preparing add component data
@@ -185,11 +194,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
185
194
}
186
195
};
187
196
188
- check_count = 0 ;
197
+ start_tick = ecsact_async_get_current_tick () ;
189
198
while (!cb_info.init_happened ) {
190
- ASSERT_LT (++check_count, 100 );
191
199
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 );
193
203
}
194
204
195
205
evc.init_callback = {};
@@ -204,8 +214,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
204
214
void * callback_user_data
205
215
) {
206
216
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
207
- cb_info.update_happened = true ;
208
217
218
+ cb_info.update_happened = true ;
209
219
ASSERT_EQ (component_id, async_test::ComponentUpdate::id);
210
220
211
221
auto comp =
@@ -221,11 +231,12 @@ TEST(AsyncRef, AddUpdateAndRemove) {
221
231
update_options.update_components = update_components.data ();
222
232
ecsact_async_enqueue_execution_options (update_options);
223
233
224
- check_count = 0 ;
234
+ start_tick = ecsact_async_get_current_tick () ;
225
235
while (!cb_info.update_happened ) {
226
- ASSERT_LT (++check_count, 100 );
227
236
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 );
229
240
}
230
241
231
242
evc.update_callback = {};
@@ -252,8 +263,8 @@ TEST(AsyncRef, AddUpdateAndRemove) {
252
263
void * callback_user_data
253
264
) {
254
265
auto & cb_info = *static_cast <callback_data*>(callback_user_data);
255
- cb_info.remove_happened = true ;
256
266
267
+ cb_info.remove_happened = true ;
257
268
ASSERT_EQ (component_id, async_test::ComponentUpdate::id);
258
269
259
270
auto comp =
@@ -262,25 +273,33 @@ TEST(AsyncRef, AddUpdateAndRemove) {
262
273
ASSERT_EQ (comp->value_to_update , 6 );
263
274
};
264
275
276
+ start_tick = ecsact_async_get_current_tick ();
265
277
// Flush until we hit a maximum or get all our events we expect.
266
- check_count = 0 ;
267
278
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 )
269
283
<< " Not all events happened before maximum was reached\n "
270
284
<< " init = " << cb_info.init_happened << " \n "
271
285
<< " update = " << cb_info.update_happened << " \n "
272
286
<< " remove = " << cb_info.remove_happened << " \n " ;
273
- ecsact_async_flush_events (&evc, nullptr );
274
- std::this_thread::sleep_for (25ms);
275
287
}
276
288
277
289
ecsact_async_disconnect ();
278
290
}
279
291
280
292
TEST (AsyncRef, TryMergeFailure) {
281
293
using namespace std ::chrono_literals;
294
+ using clock = std::chrono::high_resolution_clock;
295
+ using std::chrono::duration_cast;
282
296
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" );
284
303
285
304
auto my_needed_component = async_test::NeededComponent{};
286
305
auto another_my_needed_component = async_test::NeededComponent{};
@@ -305,6 +324,7 @@ TEST(AsyncRef, TryMergeFailure) {
305
324
) {
306
325
entity_cb_info& entity_info =
307
326
*static_cast <entity_cb_info*>(callback_user_data);
327
+
308
328
entity_info.wait = true ;
309
329
entity_info.entity = entity_id;
310
330
};
@@ -315,15 +335,15 @@ TEST(AsyncRef, TryMergeFailure) {
315
335
entity_async_evc.async_entity_callback = entity_cb;
316
336
entity_async_evc.async_entity_callback_user_data = &cb_info;
317
337
318
- int check_count = 0 ;
319
-
338
+ auto start_tick = ecsact_async_get_current_tick ();
320
339
while (cb_info.wait != true ) {
321
- std::this_thread::sleep_for (25ms);
322
- ASSERT_LT (++check_count, 100 );
323
340
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 );
324
344
}
325
345
326
- struct callback_data {
346
+ struct merge_callback_data {
327
347
ecsact_async_request_id request_id;
328
348
bool wait;
329
349
};
@@ -335,9 +355,11 @@ TEST(AsyncRef, TryMergeFailure) {
335
355
ecsact_async_request_id* request_ids,
336
356
void * callback_user_data
337
357
) {
358
+ auto & cb_data = *static_cast <merge_callback_data*>(callback_user_data);
359
+
338
360
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 ];
341
363
ASSERT_EQ (cb_data.request_id , request_id);
342
364
cb_data.wait = true ;
343
365
};
@@ -355,28 +377,30 @@ TEST(AsyncRef, TryMergeFailure) {
355
377
356
378
auto options_request = ecsact_async_enqueue_execution_options (options);
357
379
358
- callback_data cb_data {
380
+ merge_callback_data merge_cb_info {
359
381
.request_id = options_request,
360
382
.wait = false ,
361
383
};
362
384
363
385
ecsact_async_events_collector async_evc{};
364
386
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 ;
366
388
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 ) {
372
391
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 );
373
395
}
374
396
375
397
ecsact_async_disconnect ();
376
398
}
377
399
378
400
TEST (AsyncRef, ReceiveMultipleEntities) {
379
401
using namespace std ::chrono_literals;
402
+ using clock = std::chrono::high_resolution_clock;
403
+ using std::chrono::duration_cast;
380
404
381
405
auto connect_req_id = ecsact_async_connect (" good?tick_rate=25" );
382
406
@@ -388,8 +412,9 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
388
412
ecsact_entity_id entity;
389
413
390
414
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;
393
418
};
394
419
395
420
auto entity_cb = //
@@ -400,34 +425,50 @@ TEST(AsyncRef, ReceiveMultipleEntities) {
400
425
) {
401
426
entity_cb_info& entity_info =
402
427
*static_cast <entity_cb_info*>(callback_user_data);
428
+ entity_info.entity_request_ids [entity_info.counter ] = entity_info.counter ;
403
429
entity_info.counter ++;
404
430
};
405
431
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};
407
436
408
437
ecsact_async_events_collector entity_async_evc{};
409
438
entity_async_evc.async_entity_callback = entity_cb;
410
439
entity_async_evc.async_entity_callback_user_data = &entity_info;
411
440
412
- int check_count = 0 ;
413
-
441
+ auto start_tick = ecsact_async_get_current_tick ();
414
442
while (counter < 10 ) {
415
- std::this_thread::sleep_for (25ms);
416
- ASSERT_LT (++check_count, 100 );
417
443
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]);
418
451
}
419
452
420
453
ecsact_async_disconnect ();
421
454
}
422
455
423
456
TEST (AsyncRef, TryAction) {
424
457
using namespace std ::chrono_literals;
458
+ using clock = std::chrono::high_resolution_clock;
459
+ using std::chrono::duration_cast;
460
+
425
461
static std::atomic_bool reached_system = false ;
426
462
427
463
ecsact_async_connect (" good?tick_rate=25" );
428
464
429
465
auto entity_request = ecsact_async_create_entity ();
430
466
467
+ struct entity_cb_info {
468
+ ecsact_entity_id entity;
469
+ bool wait;
470
+ };
471
+
431
472
static entity_cb_info cb_info;
432
473
433
474
auto entity_cb = //
@@ -437,6 +478,7 @@ TEST(AsyncRef, TryAction) {
437
478
void * callback_user_data
438
479
) {
439
480
auto & entity_info = *static_cast <entity_cb_info*>(callback_user_data);
481
+
440
482
entity_info.wait = true ;
441
483
entity_info.entity = entity_id;
442
484
};
@@ -445,20 +487,24 @@ TEST(AsyncRef, TryAction) {
445
487
entity_async_evc.async_entity_callback = entity_cb;
446
488
entity_async_evc.async_entity_callback_user_data = &cb_info;
447
489
448
- int check_count = 0 ;
449
-
490
+ auto start_tick = ecsact_async_get_current_tick ();
450
491
while (cb_info.wait != true ) {
451
- std::this_thread::sleep_for (25ms);
452
- ASSERT_LT (++check_count, 100 );
453
492
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 );
454
496
}
455
497
456
- bool wait = false ;
498
+ struct callback_info {
499
+ bool wait = false ;
500
+ };
501
+
502
+ callback_info init_cb_info{};
457
503
458
504
// Prepare the events collector for the flush to make sure we got all the
459
505
// events we expected.
460
506
auto evc = ecsact_execution_events_collector{};
461
- evc.init_callback_user_data = &wait ;
507
+ evc.init_callback_user_data = &init_cb_info ;
462
508
evc.init_callback = //
463
509
[](
464
510
ecsact_event event,
@@ -467,8 +513,10 @@ TEST(AsyncRef, TryAction) {
467
513
const void * component_data,
468
514
void * callback_user_data
469
515
) {
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 ;
472
520
};
473
521
474
522
// Declare components required for the action
@@ -501,11 +549,12 @@ TEST(AsyncRef, TryAction) {
501
549
502
550
ecsact_async_enqueue_execution_options (options);
503
551
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 ) {
507
554
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 );
509
558
}
510
559
511
560
async_test::TryEntity my_try_entity;
@@ -538,10 +587,11 @@ TEST(AsyncRef, TryAction) {
538
587
539
588
auto options_request = ecsact_async_enqueue_execution_options (options);
540
589
541
- check_count = 0 ;
590
+ start_tick = ecsact_async_get_current_tick () ;
542
591
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 );
545
595
}
546
596
547
597
ecsact_async_disconnect ();
0 commit comments