Commit 8a0ecc2 1 parent f59dcf9 commit 8a0ecc2 Copy full SHA for 8a0ecc2
File tree 2 files changed +28
-5
lines changed
2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -489,12 +489,18 @@ public function take(int $count): self
489
489
return $ this ->flatMap (static function (mixed $ value ) use ($ count ) {
490
490
static $ i = 0 ;
491
491
492
- if ($ i ++ < $ count ) {
492
+ if (++ $ i < $ count ) {
493
493
return [$ value ];
494
494
}
495
495
496
- /** @var T[] */
497
- return [FlatMapOperation::getStopMarker ()];
496
+ /** @var T $stopMarker Fake stop marker as type T. */
497
+ $ stopMarker = FlatMapOperation::getStopMarker ();
498
+
499
+ if ($ i === $ count ) {
500
+ return [$ value , $ stopMarker ];
501
+ }
502
+
503
+ return [$ stopMarker ];
498
504
});
499
505
}
500
506
Original file line number Diff line number Diff line change 4
4
5
5
use Amp \PHPUnit \AsyncTestCase ;
6
6
use Amp \PHPUnit \TestException ;
7
+ use function Amp \delay ;
7
8
8
9
class TakeTest extends AsyncTestCase
9
10
{
@@ -15,10 +16,26 @@ public function testValuesEmitted(): void
15
16
self ::assertSame ([1 , 2 ], $ pipeline ->toArray ());
16
17
}
17
18
19
+ public function testCompleteBeforeSourceCompletes ()
20
+ {
21
+ $ count = 3 ;
22
+ $ this ->setTimeout (0.1 * $ count + 0.1 );
23
+
24
+ $ emitted = Pipeline::fromIterable (function () use ($ count ): \Generator {
25
+ for ($ i = 0 ; $ i < $ count ; ++$ i ) {
26
+ delay (0.1 );
27
+ yield $ i ;
28
+ }
29
+ delay (1 );
30
+ })->take ($ count )->toArray ();
31
+
32
+ self ::assertSame (\range (0 , $ count - 1 ), $ emitted );
33
+ }
34
+
18
35
public function testPipelineFails (): void
19
36
{
20
- $ exception = new TestException ;
21
- $ source = new Queue ;
37
+ $ exception = new TestException () ;
38
+ $ source = new Queue () ;
22
39
23
40
$ iterator = $ source ->pipe ()->take (2 )->getIterator ();
24
41
You can’t perform that action at this time.
0 commit comments