summaryrefslogtreecommitdiff
path: root/ext/sysvsem
diff options
context:
space:
mode:
authorMitch Hagstrand <mhagstrand@gmail.com>2017-01-01 21:19:14 -0800
committerJoe Watkins <krakjoe@php.net>2017-01-02 09:40:46 +0000
commitb4459af0256d991d21aef8ed167be47512d20e5d (patch)
tree4230c3820d269a0f13d77306aed60e8d02a3776f /ext/sysvsem
parent876b7b177f5622d5065185a2c3277fa1576e6d79 (diff)
downloadphp-git-b4459af0256d991d21aef8ed167be47512d20e5d.tar.gz
Fix nowait.phpt to no longer depend on sleeping
In Travis CI the nowait.phpt test sometimes fails because of the usleep() not being long enough ensure proper execution order. Instead of depending on sleeping the test now uses two semaphores to guarantee execution order.
Diffstat (limited to 'ext/sysvsem')
-rw-r--r--ext/sysvsem/tests/nowait.phpt157
1 files changed, 92 insertions, 65 deletions
diff --git a/ext/sysvsem/tests/nowait.phpt b/ext/sysvsem/tests/nowait.phpt
index 0a6fdf4a50..8557ce33ee 100644
--- a/ext/sysvsem/tests/nowait.phpt
+++ b/ext/sysvsem/tests/nowait.phpt
@@ -1,5 +1,5 @@
--TEST--
-sem_acquire with nowait
+Test sem_acquire with nowait option
--SKIPIF--
<?php // vim600: ts=4 sw=4 syn=php fdm=marker
if(!extension_loaded('sysvsem') || !extension_loaded('pcntl')) {
@@ -8,96 +8,123 @@ if(!extension_loaded('sysvsem') || !extension_loaded('pcntl')) {
?>
--FILE--
<?php
-$SEMKEY = ftok(__FILE__, 'P'); // Semaphore key
+$P_SEMKEY = ftok(__FILE__, 'P'); // Parent Semaphore key
+$C_SEMKEY = ftok(__FILE__, 'C'); // Child Semaphore key
-$pid = pcntl_fork();
+echo "P: parent process running.\n";
-if ($pid) {
- echo "Parent.\n";
+pcntl_signal(SIGCHLD, SIG_IGN);
- pcntl_signal(SIGCHLD, SIG_IGN);
+// Get semaphore for parent
+$p_sem_id = sem_get($P_SEMKEY, 1);
+if ($p_sem_id === FALSE) {
+ echo "P: failed to parent get semaphore\n";
+ exit;
+}
- // Get semaphore
- $sem_id = sem_get($SEMKEY, 1);
- if ($sem_id === FALSE) {
- echo "P: fail to get semaphore";
- exit;
- }
- echo "P: got semaphore $sem_id.\n";
+echo "P: got semaphore $p_sem_id.\n";
- register_shutdown_function(function () use ($sem_id) {
- echo "P: cleanup.\n";
- sem_remove($sem_id);
- });
-
- // Acquire semaphore
- if (! sem_acquire($sem_id)) {
- echo "P: fail to acquire semaphore $sem_id.\n";
- sem_remove($sem_id);
- exit;
- }
- echo "P: success acquire semaphore $sem_id.\n";
+// Get semaphore for child
+$c_sem_id = sem_get($C_SEMKEY, 1);
+if ($c_sem_id === FALSE) {
+ echo "P: failed to child get semaphore\n";
+ exit;
+}
- usleep(20000);
- echo "P: releases.\n";
- sem_release($sem_id);
+// Acquire semaphore for parent
+if (!sem_acquire($p_sem_id)) {
+ echo "P: fail to acquire semaphore $p_sem_id.\n";
+ sem_remove($p_sem_id);
+ exit;
+}
+echo "P: acquired semaphore $p_sem_id.\n";
- usleep(5000);
+// Acquire semaphore for child
+if (!sem_acquire($c_sem_id)) {
+ echo "P: fail to acquire semaphore $c_sem_id.\n";
+ sem_remove($c_sem_id);
+ exit;
+}
+echo "P: acquired semaphore $c_sem_id\n";
+// Fork process
+$pid = pcntl_fork();
+
+if ($pid) {
- // Acquire semaphore
- if (! sem_acquire($sem_id)) {
- echo "P: fail to acquire semaphore $sem_id.\n";
- sem_remove($sem_id);
+ register_shutdown_function(function () use ($p_sem_id) {
+ echo "P: removing semaphore $p_sem_id.\n";
+ sem_remove($p_sem_id);
+ });
+
+ // Release semaphore after 50ms
+ usleep(50000);
+
+ /* Wait for the child semaphore to be released to
+ to release the parent semaphore */
+ if (!sem_acquire($c_sem_id)) {
+ echo "P: failed to acquire semaphore $c_sem_id.\n";
exit;
}
- echo "P: success acquire semaphore $sem_id.\n";
+ echo "P: releasing semaphore $p_sem_id.\n";
+ if (!sem_release($p_sem_id)) {
+ echo "P: failed to release semaphore\n";
+ }
+
+ sem_release($c_sem_id);
$status = null;
pcntl_waitpid($pid, $status);
} else {
- usleep(10000);
- echo "Child.\n";
- // Get semaphore
- $sem_id = sem_get($SEMKEY, 1);
- if ($sem_id === FALSE) {
- echo "C: fail to get semaphore";
- exit;
- }
- echo "C: got semaphore $sem_id.\n";
-
- // Acquire semaphore
- if (! sem_acquire($sem_id)) {
- echo "C: fail to acquire semaphore $sem_id.\n";
+ register_shutdown_function(function () use ($c_sem_id) {
+ echo "C: removing semaphore $c_sem_id.\n";
+ sem_remove($c_sem_id);
+ });
+
+ echo "C: child process running.\n";
+
+ // Have the semaphore after process forked
+ echo "C: got semaphore $p_sem_id and $c_sem_id.\n";
+
+ // This should fail to get to the semaphore and not wait
+ if (sem_acquire($p_sem_id, true)) {
+ echo "C: test failed, Child was able to acquire semaphore $p_sem_id.\n";
exit;
}
- echo "C: success acquire semaphore $sem_id.\n";
- echo "C: releases.\n";
- sem_release($sem_id);
+ // The child process did not wait to acquire the semaphore
+ echo "C: failed to acquire semaphore $p_sem_id.\n";
- usleep(10000);
+ echo "C: releasing semaphore $c_sem_id\n";
+ if (!sem_release($c_sem_id)) {
+ echo "C: Failed to release semaphore\n";
+ }
- // Acquire semaphore
- if (! sem_acquire($sem_id, true)) {
- echo "C: fail to acquire semaphore $sem_id.\n";
+ // Acquire semaphore with waiting
+ if (!sem_acquire($p_sem_id)) {
+ echo "C: fail to acquire semaphore $p_sem_id.\n";
exit;
}
- echo "C: success acquire semaphore $sem_id.\n";
+ echo "C: success acquired semaphore $p_sem_id.\n";
+
+ echo "C: releasing semaphore $p_sem_id.\n";
+ sem_release($p_sem_id);
}
?>
--EXPECTF--
-Parent.
+P: parent process running.
P: got semaphore Resource id #%i.
-P: success acquire semaphore Resource id #%i.
-Child.
-C: got semaphore Resource id #%i.
-P: releases.
-C: success acquire semaphore Resource id #%i.
-C: releases.
-P: success acquire semaphore Resource id #%i.
-C: fail to acquire semaphore Resource id #%i.
-P: cleanup.
+P: acquired semaphore Resource id #%i.
+P: acquired semaphore Resource id #%i
+C: child process running.
+C: got semaphore Resource id #%i and Resource id #%i.
+C: failed to acquire semaphore Resource id #%i.
+C: releasing semaphore Resource id #%i
+P: releasing semaphore Resource id #%i.
+C: success acquired semaphore Resource id #%i.
+C: releasing semaphore Resource id #%i.
+C: removing semaphore Resource id #%i.
+P: removing semaphore Resource id #%i.