Skip to content

Commit 4e847c1

Browse files
authored
Add Coroutine::list() (#38)
1 parent 22d0af4 commit 4e847c1

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
composer.lock
33
*.cache
44
*.log
5-
.idea
5+
.idea
6+
.bashrc

src/Coroutine.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,12 @@ public static function exists(int $id): bool
147147
{
148148
return parent::get($id) !== null;
149149
}
150+
151+
/**
152+
* @return iterable<int>
153+
*/
154+
public static function list(): iterable
155+
{
156+
yield from array_keys(parent::getAll());
157+
}
150158
}

tests/Cases/CoroutineTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,22 @@ public function testCoroutineResumeById()
176176
$this->assertSame(3, $channel->pop());
177177
$this->assertSame(5, $channel->pop());
178178
}
179+
180+
public function testCoroutineList()
181+
{
182+
$list = Coroutine::list();
183+
$this->assertIsIterable($list);
184+
$this->assertContains(Coroutine::id(), $list);
185+
186+
Coroutine::create(function () {
187+
sleep(1);
188+
});
189+
Coroutine::create(function () {
190+
sleep(1);
191+
});
192+
Coroutine::create(function () {
193+
sleep(1);
194+
});
195+
$this->assertEquals(5, iterator_count(Coroutine::list()));
196+
}
179197
}

0 commit comments

Comments
 (0)