Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
php:
- 8.5
- 8.4
- 8.3
- 8.2
Expand DownExpand Up@@ -49,6 +50,7 @@ jobs:
strategy:
matrix:
php:
- 8.5
- 8.4
- 8.3
- 8.2
Expand DownExpand Up@@ -113,6 +115,7 @@ jobs:
strategy:
matrix:
php:
- 8.5
- 8.4
- 8.3
- 8.2
Expand Down
8 changes: 4 additions & 4 deletions src/ExtEvLoop.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,13 +143,13 @@ public function addTimer($interval, $callback)
$callback = function () use ($timer, $timers, $that) {
\call_user_func($timer->getCallback(), $timer);

if ($timers->contains($timer)) {
if ($timers->offsetExists($timer)) {
$that->cancelTimer($timer);
}
};

$event = $this->loop->timer($timer->getInterval(), 0.0, $callback);
$this->timers->attach($timer, $event);
$this->timers->offsetSet($timer, $event);

return $timer;
}
Expand All@@ -163,7 +163,7 @@ public function addPeriodicTimer($interval, $callback)
};

$event = $this->loop->timer($timer->getInterval(), $timer->getInterval(), $callback);
$this->timers->attach($timer, $event);
$this->timers->offsetSet($timer, $event);

return $timer;
}
Expand All@@ -176,7 +176,7 @@ public function cancelTimer(TimerInterface $timer)

$event = $this->timers[$timer];
$event->stop();
$this->timers->detach($timer);
$this->timers->offsetUnset($timer);
}

public function futureTick($listener)
Expand Down
8 changes: 4 additions & 4 deletions src/ExtEventLoop.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,7 +64,7 @@ public function __destruct()
{
// explicitly clear all references to Event objects to prevent SEGFAULTs on Windows
foreach ($this->timerEvents as $timer) {
$this->timerEvents->detach($timer);
$this->timerEvents->offsetUnset($timer);
}

$this->readEvents = array();
Expand DownExpand Up@@ -157,9 +157,9 @@ public function addPeriodicTimer($interval, $callback)

public function cancelTimer(TimerInterface $timer)
{
if ($this->timerEvents->contains($timer)) {
if ($this->timerEvents->offsetExists($timer)) {
$this->timerEvents[$timer]->free();
$this->timerEvents->detach($timer);
$this->timerEvents->offsetUnset($timer);
}
}

Expand DownExpand Up@@ -243,7 +243,7 @@ private function createTimerCallback()
$this->timerCallback = function ($_, $__, $timer) use ($timers) {
\call_user_func($timer->getCallback(), $timer);

if (!$timer->isPeriodic() && $timers->contains($timer)) {
if (!$timer->isPeriodic() && $timers->offsetExists($timer)) {
$this->cancelTimer($timer);
}
};
Expand Down
20 changes: 14 additions & 6 deletions src/ExtUvLoop.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,13 +119,13 @@ public function addTimer($interval, $callback)
$callback = function () use ($timer, $timers, $that) {
\call_user_func($timer->getCallback(), $timer);

if ($timers->contains($timer)) {
if ($timers->offsetExists($timer)) {
$that->cancelTimer($timer);
}
};

$event = \uv_timer_init($this->uv);
$this->timers->attach($timer, $event);
$this->timers->offsetSet($timer, $event);
\uv_timer_start(
$event,
$this->convertFloatSecondsToMilliseconds($interval),
Expand All@@ -149,7 +149,7 @@ public function addPeriodicTimer($interval, $callback)

$interval = $this->convertFloatSecondsToMilliseconds($interval);
$event = \uv_timer_init($this->uv);
$this->timers->attach($timer, $event);
$this->timers->offsetSet($timer, $event);
\uv_timer_start(
$event,
$interval,
Expand All@@ -167,7 +167,7 @@ public function cancelTimer(TimerInterface $timer)
{
if (isset($this->timers[$timer])) {
@\uv_timer_stop($this->timers[$timer]);
$this->timers->detach($timer);
$this->timers->offsetUnset($timer);
}
}

Expand DownExpand Up@@ -329,9 +329,17 @@ private function convertFloatSecondsToMilliseconds($interval)
}

$maxValue = (int) (\PHP_INT_MAX / 1000);
$intInterval = (int) $interval;
$intervalOverflow = false;
if (PHP_VERSION_ID > 80499 && $interval >= \PHP_INT_MAX + 1) {
$intervalOverflow = true;
} else {
$intInterval = (int) $interval;
if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
$intervalOverflow = true;
}
}

if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
if ($intervalOverflow) {
throw new \InvalidArgumentException(
"Interval overflow, value must be lower than '{$maxValue}', but '{$interval}' passed."
);
Expand Down
52 changes: 39 additions & 13 deletions tests/LoopTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,7 +64,9 @@ public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()
public function testStaticAddReadStreamWithNoDefaultLoopCallsAddReadStreamOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$stream = stream_socket_server('127.0.0.1:0');
Expand All@@ -90,7 +92,9 @@ public function testStaticAddWriteStreamCallsAddWriteStreamOnLoopInstance()
public function testStaticAddWriteStreamWithNoDefaultLoopCallsAddWriteStreamOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$stream = stream_socket_server('127.0.0.1:0');
Expand All@@ -115,7 +119,9 @@ public function testStaticRemoveReadStreamCallsRemoveReadStreamOnLoopInstance()
public function testStaticRemoveReadStreamWithNoDefaultLoopIsNoOp()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$stream = tmpfile();
Expand All@@ -139,7 +145,9 @@ public function testStaticRemoveWriteStreamCallsRemoveWriteStreamOnLoopInstance(
public function testStaticRemoveWriteStreamWithNoDefaultLoopIsNoOp()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$stream = tmpfile();
Expand DownExpand Up@@ -167,7 +175,9 @@ public function testStaticAddTimerCallsAddTimerOnLoopInstanceAndReturnsTimerInst
public function testStaticAddTimerWithNoDefaultLoopCallsAddTimerOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$interval = 1.0;
Expand DownExpand Up@@ -197,7 +207,9 @@ public function testStaticAddPeriodicTimerCallsAddPeriodicTimerOnLoopInstanceAnd
public function testStaticAddPeriodicTimerWithNoDefaultLoopCallsAddPeriodicTimerOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$interval = 1.0;
Expand All@@ -224,7 +236,9 @@ public function testStaticCancelTimerCallsCancelTimerOnLoopInstance()
public function testStaticCancelTimerWithNoDefaultLoopIsNoOp()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
Expand All@@ -248,7 +262,9 @@ public function testStaticFutureTickCallsFutureTickOnLoopInstance()
public function testStaticFutureTickWithNoDefaultLoopCallsFutureTickOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$listener = function () { };
Expand DownExpand Up@@ -277,7 +293,9 @@ public function testStaticAddSignalWithNoDefaultLoopCallsAddSignalOnNewLoopInsta
}

$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$signal = 1;
Expand DownExpand Up@@ -307,7 +325,9 @@ public function testStaticRemoveSignalCallsRemoveSignalOnLoopInstance()
public function testStaticRemoveSignalWithNoDefaultLoopIsNoOp()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

$signal = 1;
Expand All@@ -330,7 +350,9 @@ public function testStaticRunCallsRunOnLoopInstance()
public function testStaticRunWithNoDefaultLoopCallsRunsOnNewLoopInstance()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

Loop::run();
Expand All@@ -351,7 +373,9 @@ public function testStaticStopCallsStopOnLoopInstance()
public function testStaticStopCallWithNoDefaultLoopIsNoOp()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);

Loop::stop();
Expand All@@ -366,7 +390,9 @@ public function testStaticStopCallWithNoDefaultLoopIsNoOp()
public function unsetLoopFromLoopAccessor()
{
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
$ref->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$ref->setAccessible(true);
}
$ref->setValue(null, null);
}
}