| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Example usage: |
| 5 |
|
| 6 |
php -a |
| 7 |
require 'wp-load.php'; |
| 8 |
define('I_AM_TESTING', true); |
| 9 |
require 'test.php'; |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) die('No direct access.'); |
| 13 |
|
| 14 |
if (!defined('I_AM_TESTING') || !I_AM_TESTING) die('Please define I_AM_TESTING.'); |
| 15 |
|
| 16 |
require_once(dirname(__FILE__).'/class-updraft-semaphore.php'); |
| 17 |
|
| 18 |
class Test_Logger_1 { |
| 19 |
function log($message, $level) { |
| 20 |
echo "Test_Logger_1::log(level=$level, message=$message)\n"; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
class Test_Logger_2 { |
| 25 |
function log($message, $level) { |
| 26 |
echo "Test_Logger_2::log(level=$level, message=$message)\n"; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
$my_lock = new Updraft_Semaphore_3_0('my_test_lock_name', 4, array(new Test_Logger_1())); |
| 31 |
|
| 32 |
if ($my_lock->lock()) { |
| 33 |
try { |
| 34 |
// do stuff ... |
| 35 |
$my_lock_again = new Updraft_Semaphore_3_0('my_test_lock_name', 4, array(new Test_Logger_2())); |
| 36 |
$time_now = microtime(true); |
| 37 |
if ($my_lock_again->lock(6)) { |
| 38 |
echo "Eventually got it after ".round(microtime(true) - $time_now, 3)." seconds\n"; |
| 39 |
$my_lock_again->release(); |
| 40 |
} else { |
| 41 |
echo("Sorry, could not get the second lock\n"); |
| 42 |
} |
| 43 |
|
| 44 |
} catch (Exception $e) { |
| 45 |
var_dump($e); |
| 46 |
// We are making sure we release the lock in case of an error |
| 47 |
} catch (Error $e) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound |
| 48 |
var_dump($e); |
| 49 |
// We are making sure we release the lock in case of an error |
| 50 |
} |
| 51 |
|
| 52 |
$my_lock->release(); |
| 53 |
|
| 54 |
} else { |
| 55 |
echo("Sorry, could not get the first lock\n"); |
| 56 |
} |
| 57 |
|