Skip to content

Commit 97d1edf

Browse files
committed
* fix errors for basical await tests
1 parent 9e1b5b5 commit 97d1edf

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

async.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ PHP_FUNCTION(Async_awaitAnyOf)
434434

435435
HashTable * results = zend_new_array(8);
436436

437+
if (count == 0) {
438+
RETURN_ARR(results);
439+
}
440+
437441
async_await_futures(futures,
438442
(int)count,
439443
false,

async_API.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ static zend_class_entry* async_get_class_ce(zend_async_class type)
342342
////////////////////////////////////////////////////////////////////
343343

344344
#define AWAIT_ALL(await_context) ((await_context)->waiting_count == 0 || (await_context)->waiting_count == (await_context)->total)
345+
#define ITERATOR_IS_FINISHED(await_context) \
346+
((await_context->ignore_errors ? await_context->success_count : await_context->resolved_count) >= await_context->waiting_count || \
347+
(await_context->total != 0 && await_context->resolved_count >= await_context->total) \
348+
)
345349

346350
static zend_always_inline zend_async_event_t * zval_to_event(const zval * current)
347351
{
@@ -442,6 +446,7 @@ void async_waiting_callback(
442446
if (exception == NULL && await_context->results != NULL && ZEND_ASYNC_EVENT_WILL_ZVAL_RESULT(event) && result != NULL) {
443447

444448
const zval *success = NULL;
449+
await_context->success_count++;
445450

446451
if (Z_TYPE(await_callback->key) == IS_STRING) {
447452
success = zend_hash_update(await_context->results, Z_STR(await_callback->key), result);
@@ -459,7 +464,7 @@ void async_waiting_callback(
459464
}
460465
}
461466

462-
if (await_context->resolved_count >= await_context->waiting_count) {
467+
if (UNEXPECTED(ITERATOR_IS_FINISHED(await_context))) {
463468
ZEND_ASYNC_RESUME(await_callback->callback.coroutine);
464469
}
465470
}
@@ -579,7 +584,7 @@ void iterator_coroutine_finish_callback(
579584
exception,
580585
false
581586
);
582-
} else if (iterator->await_context->resolved_count >= iterator->await_context->waiting_count) {
587+
} else if (ITERATOR_IS_FINISHED(iterator->await_context)) {
583588
// If iteration is finished, resume the waiting coroutine
584589
ZEND_ASYNC_RESUME(iterator->waiting_coroutine);
585590
}
@@ -669,6 +674,7 @@ void async_await_futures(
669674
await_context->total = futures != NULL ? (int) zend_hash_num_elements(futures) : 0;
670675
await_context->waiting_count = count > 0 ? count : await_context->total;
671676
await_context->resolved_count = 0;
677+
await_context->success_count = 0;
672678
await_context->ignore_errors = ignore_errors;
673679
await_context->concurrency = concurrency;
674680
await_context->fill_missing_with_null = fill_missing_with_null;

async_API.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,26 @@ typedef void (*async_await_context_dtor_t) (async_await_context_t *context);
2727
struct _async_await_context_t
2828
{
2929
unsigned int ref_count;
30+
/* The total number of futures to wait for */
3031
unsigned int total;
32+
/* The number of futures that are currently waiting */
3133
unsigned int waiting_count;
34+
/* The number of futures that have been resolved */
3235
unsigned int resolved_count;
36+
/* The number of futures that have been successfully resolved */
37+
unsigned int success_count;
38+
/* If errors should be ignored */
3339
bool ignore_errors;
40+
/* If we need to fill missing results with null */
41+
bool fill_missing_with_null;
42+
/* Number of concurrent coroutines that can be executed */
3443
unsigned int concurrency;
3544
async_await_context_dtor_t dtor;
3645
HashTable *futures;
3746
// Scope for the new coroutines
3847
zend_async_scope_t * scope;
3948
HashTable *results;
4049
HashTable *errors;
41-
bool fill_missing_with_null;
4250
};
4351

4452
typedef struct

tests/await/009-awaitFirstSuccess_all_errors.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var_dump($result);
2525

2626
echo "end\n";
2727
?>
28-
--EXPECT--
28+
--EXPECTF--
2929
start
3030
array(2) {
3131
[0]=>

tests/await/012-awaitAllWithErrors_basic.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var_dump($result);
2929

3030
echo "end\n";
3131
?>
32-
--EXPECT--
32+
--EXPECTF--
3333
start
3434
array(2) {
3535
[0]=>

tests/await/014-awaitAnyOf_basic.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ echo "start\n";
1111

1212
$coroutines = [
1313
spawn(function() {
14-
delay(50);
14+
delay(80);
1515
return "first";
1616
}),
1717
spawn(function() {
1818
delay(20);
1919
return "second";
2020
}),
2121
spawn(function() {
22-
delay(30);
22+
delay(60);
2323
return "third";
2424
}),
2525
spawn(function() {

tests/await/016-awaitAnyOfWithErrors_basic.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ echo "start\n";
1111

1212
$coroutines = [
1313
spawn(function() {
14-
delay(50);
14+
delay(80);
1515
return "first";
1616
}),
1717
spawn(function() {
1818
delay(20);
1919
throw new RuntimeException("test exception");
2020
}),
2121
spawn(function() {
22-
delay(30);
22+
delay(20);
2323
return "third";
2424
}),
2525
spawn(function() {
26-
delay(25);
26+
delay(35);
2727
return "fourth";
2828
}),
2929
];
@@ -33,7 +33,7 @@ var_dump($result);
3333

3434
echo "end\n";
3535
?>
36-
--EXPECT--
36+
--EXPECTF--
3737
start
3838
array(2) {
3939
[0]=>

0 commit comments

Comments
 (0)