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

Make signal handling tests more robust and increase test timeouts #111

Merged
merged 1 commit into from
Oct 14, 2017
Merged
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
46 changes: 22 additions & 24 deletions tests/StreamSelectLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function signalProvider()
];
}

private $_signalHandled = false;

/**
* Test signal interrupt when no stream is attached to the loop
* @dataProvider signalProvider
Expand All @@ -59,20 +57,24 @@ public function testSignalInterruptNoStream($signal)
$this->markTestSkipped('"pcntl" extension is required to run this test.');
}

// dispatch signal handler once before signal is sent and once after
$this->loop->addTimer(0.01, function() { pcntl_signal_dispatch(); });
$this->loop->addTimer(0.03, function() { pcntl_signal_dispatch(); });
if (defined('HHVM_VERSION')) {
// hhvm startup is slow so we need to add another handler much later
$this->loop->addTimer(0.5, function() { pcntl_signal_dispatch(); });
}
// dispatch signal handler every 10ms for 0.1s
$check = $this->loop->addPeriodicTimer(0.01, function() {
pcntl_signal_dispatch();
});
$this->loop->addTimer(0.1, function () use ($check) {
$this->loop->cancelTimer($check);
});

$this->setUpSignalHandler($signal);
$handled = false;
$this->assertTrue(pcntl_signal(constant($signal), function () use (&$handled) {
$handled = true;
}));

// spawn external process to send signal to current process id
$this->forkSendSignal($signal);

$this->loop->run();
$this->assertTrue($this->_signalHandled);
$this->assertTrue($handled);
}

/**
Expand All @@ -86,7 +88,9 @@ public function testSignalInterruptWithStream($signal)
}

// dispatch signal handler every 10ms
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
$this->loop->addPeriodicTimer(0.01, function() {
pcntl_signal_dispatch();
});

// add stream to the loop
list($writeStream, $readStream) = $this->createSocketPair();
Expand All @@ -97,27 +101,21 @@ public function testSignalInterruptWithStream($signal)
$loop->stop();
}
});
$this->loop->addTimer(0.05, function() use ($writeStream) {
$this->loop->addTimer(0.1, function() use ($writeStream) {
fwrite($writeStream, "end loop\n");
});

$this->setUpSignalHandler($signal);
$handled = false;
$this->assertTrue(pcntl_signal(constant($signal), function () use (&$handled) {
$handled = true;
}));

// spawn external process to send signal to current process id
$this->forkSendSignal($signal);

$this->loop->run();

$this->assertTrue($this->_signalHandled);
}

/**
* add signal handler for signal
*/
protected function setUpSignalHandler($signal)
{
$this->_signalHandled = false;
$this->assertTrue(pcntl_signal(constant($signal), function() { $this->_signalHandled = true; }));
$this->assertTrue($handled);
}

/**
Expand Down