Skip to content
Open
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
179 changes: 179 additions & 0 deletions __tests__/unit-tests/test-lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

namespace Automattic\WP\Cron_Control\Tests;

use Automattic\WP\Cron_Control\Lock;

class Lock_Tests extends \WP_UnitTestCase {
private $locks = array(
'concurrent-acquisition',
'rejected-admission',
'stale-lock',
'old-generation-cleanup',
'recreated-lock',
'reset-lock',
);

public function setUp(): void {
parent::setUp();

foreach ( $this->locks as $lock ) {
Lock::reset_lock( $lock );
}
}

public function tearDown(): void {
foreach ( $this->locks as $lock ) {
Lock::reset_lock( $lock );
}

parent::tearDown();
}

public function test_concurrent_acquisition_with_limit_one_admits_one_worker() {
$lock = 'concurrent-acquisition';

$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$this->assertFalse( Lock::check_lock( $lock, 1 ) );
$this->assertSame( 1, Lock::get_lock_value( $lock ) );
}

public function test_rejected_admission_does_not_change_lock_value() {
$lock = 'rejected-admission';

$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$this->assertFalse( Lock::check_lock( $lock, 1 ) );
$this->assertSame( 1, Lock::get_lock_value( $lock ) );
}

public function test_failed_lock_release_can_be_retried() {
global $wpdb;

$lock = 'failed-release';
$reflection = new \ReflectionProperty( Lock::class, 'acquired_locks' );
$reflection->setAccessible( true );
$original_locks = $reflection->getValue();
$original_wpdb = $wpdb;
$reflection->setValue(
array(
$lock => array( 1 ),
)
);
$wpdb = new class() {
public $options = 'wp_options';
public $queries = 0;

public function prepare( $query, ...$args ) {
return $query;
}

public function query( $query ) {
++$this->queries;

return 1 === $this->queries ? false : 0;
}
};

try {
$this->assertFalse( Lock::free_lock( $lock ) );
$this->assertSame( array( 1 ), $reflection->getValue()[ $lock ] );
$this->assertTrue( Lock::free_lock( $lock ) );
$this->assertSame( array(), $reflection->getValue()[ $lock ] );
} finally {
$wpdb = $original_wpdb;
$reflection->setValue( $original_locks );
}
}

public function test_stale_lock_recovery_records_the_new_admission() {
global $wpdb;

$lock = 'stale-lock';
$wpdb->update(
$wpdb->options,
array( 'option_value' => '1:' . ( time() - MINUTE_IN_SECONDS ) . ':1' ),
array( 'option_name' => 'a8ccc_lock_stale-lock' )
);

$this->assertTrue( Lock::check_lock( $lock, 1, 1 ) );
$this->assertSame( 1, Lock::get_lock_value( $lock ) );
$this->assertGreaterThan( time() - 2, Lock::get_lock_timestamp( $lock ) );
}

public function test_old_generation_cleanup_does_not_release_recovered_lock() {
global $wpdb;

$lock = 'old-generation-cleanup';
$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$wpdb->update(
$wpdb->options,
array( 'option_value' => '1:' . ( time() - MINUTE_IN_SECONDS ) . ':1' ),
array( 'option_name' => 'a8ccc_lock_old-generation-cleanup' )
);
$this->assertTrue( Lock::check_lock( $lock, 1, 1 ) );
$state = explode( ':', $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_old-generation-cleanup'" ) );

$reflection = new \ReflectionProperty( Lock::class, 'acquired_locks' );
$reflection->setAccessible( true );
$reflection->setValue(
array(
$lock => array( (int) $state[2], 1 ),
)
);

$this->assertTrue( Lock::free_lock( $lock ) );
$this->assertSame( 1, Lock::get_lock_value( $lock ) );
}

public function test_old_generation_cleanup_does_not_release_a_recreated_lock() {
global $wpdb;

$lock = 'recreated-lock';
$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$state = explode( ':', $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_recreated-lock'" ) );
$old_generation = (int) $state[2];
$this->assertTrue( Lock::free_lock( $lock ) );
$this->assertNull( $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_recreated-lock'" ) );

$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$state = explode( ':', $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_recreated-lock'" ) );
$replacement_state = implode( ':', $state );
$replacement_generation = (int) $state[2];

$reflection = new \ReflectionProperty( Lock::class, 'acquired_locks' );
$reflection->setAccessible( true );
$reflection->setValue(
array(
$lock => array( $replacement_generation, $old_generation ),
)
);

$this->assertTrue( Lock::free_lock( $lock ) );
$this->assertSame( $replacement_state, $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_recreated-lock'" ) );
}

public function test_old_generation_cleanup_does_not_release_a_reset_lock() {
global $wpdb;

$lock = 'reset-lock';
$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$state = explode( ':', $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_reset-lock'" ) );
$old_generation = (int) $state[2];
$this->assertTrue( Lock::reset_lock( $lock ) );
$this->assertTrue( Lock::check_lock( $lock, 1 ) );
$state = explode( ':', $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_reset-lock'" ) );
$replacement_state = implode( ':', $state );
$replacement_generation = (int) $state[2];

$reflection = new \ReflectionProperty( Lock::class, 'acquired_locks' );
$reflection->setAccessible( true );
$reflection->setValue(
array(
$lock => array( $replacement_generation, $old_generation ),
)
);

$this->assertTrue( Lock::free_lock( $lock ) );
$this->assertSame( $replacement_state, $wpdb->get_var( "SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'a8ccc_lock_reset-lock'" ) );
}
}
16 changes: 1 addition & 15 deletions includes/class-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class Events extends Singleton {
* Register hooks
*/
protected function class_init() {
// Prime lock cache if not present.
Lock::prime_lock( self::LOCK );

// Prepare environment as early as possible.
$earliest_action = did_action( 'muplugins_loaded' ) ? 'plugins_loaded' : 'muplugins_loaded';
add_action( $earliest_action, array( $this, 'prepare_environment' ) );
Expand Down Expand Up @@ -241,9 +238,6 @@ public function run_event( $timestamp, $action, $instance, $force = false ) {

// Limit how many events are processed concurrently, unless explicitly bypassed.
if ( ! $force ) {
// Prepare event-level lock.
$this->prime_event_action_lock( $event );

if ( ! $this->can_run_event( $event ) ) {
/* translators: 1: Event action, 2: Event arguments */
$error_message = sprintf( __( 'No resources available to run the job with action `%1$s` and arguments `%2$s`.', 'automattic-cron-control' ), $event->get_action(), maybe_serialize( $event->get_args() ) );
Expand Down Expand Up @@ -300,10 +294,6 @@ public function run_event( $timestamp, $action, $instance, $force = false ) {
return $return;
}

private function prime_event_action_lock( Event $event ): void {
Lock::prime_lock( $this->get_lock_key_for_event_action( $event ), JOB_LOCK_EXPIRY_IN_MINUTES * \MINUTE_IN_SECONDS );
}

// Checks concurrency locks, deciding if the event can be run at this moment.
private function can_run_event( Event $event ): bool {
// Limit to one concurrent execution of a specific action by default.
Expand Down Expand Up @@ -348,11 +338,7 @@ private function reset_event_lock( Event $event ): bool {
$lock_key = $this->get_lock_key_for_event_action( $event );
$expires = JOB_LOCK_EXPIRY_IN_MINUTES * \MINUTE_IN_SECONDS;

if ( isset( $this->concurrent_action_whitelist[ $event->get_action() ] ) ) {
return Lock::free_lock( $lock_key, $expires );
} else {
return Lock::reset_lock( $lock_key, $expires );
}
return Lock::free_lock( $lock_key, $expires );
}

/**
Expand Down
Loading
Loading