Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(channel): reset state to EmptyChannelState after unsetting #201

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions await-generator/src/SOFe/AwaitGenerator/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ public function sendAndWait($value) : Generator{
});
} finally {
if($key !== null) {
unset($this->state->queue[spl_object_id($key)]);
if($this->state instanceof SendingChannelState) {
// our key may still exist in the channel state

unset($this->state->queue[spl_object_id($key)]);
if(count($this->state->queue) === 0) {
$this->state = new EmptyChannelState;
}
}
// else, state already changed means our key has been shifted already.
}
}
}
Expand Down Expand Up @@ -134,7 +142,15 @@ public function receive() : Generator{
});
} finally {
if($key !== null) {
unset($this->state->queue[spl_object_id($key)]);
if($this->state instanceof ReceivingChannelState) {
// our key may still exist in the channel state

unset($this->state->queue[spl_object_id($key)]);
if(count($this->state->queue) === 0) {
$this->state = new EmptyChannelState;
}
}
// else, state already changed means our key has been shifted already.
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/SOFe/AwaitGenerator/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,43 @@ public function testTryReceive() : void{

self::assertSame("a", $receive);
}

public function testTryCancelSender() : void{
$ok = false;
Await::f2c(function() use(&$ok){
/** @var Channel<null> $channel */
$channel = new Channel;

[$which, $_] = yield from Await::safeRace([
$channel->sendAndWait(null),
GeneratorUtil::empty(null),
]);
self::assertSame(1, $which);

$ret = $channel->tryReceiveOr("no sender");
self::assertSame("no sender", $ret);
$ok = true;
});

self::assertTrue($ok, "test run complete");
}

public function testTryCancelReceiver() : void{
$ok = false;
Await::f2c(function() use(&$ok){
/** @var Channel<null> $channel */
$channel = new Channel;

[$which, $_] = yield from Await::safeRace([
$channel->receive(),
GeneratorUtil::empty(null),
]);
self::assertSame(1, $which);

$channel->sendWithoutWait(null);
$ok = true;
});

self::assertTrue($ok, "test run complete");
}
}