Skip to content

Commit

Permalink
Tests layout
Browse files Browse the repository at this point in the history
  • Loading branch information
soulseekah committed Sep 14, 2018
1 parent 53826fa commit 19d2e07
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1 +1,4 @@
vendor
wordpress-develop
*.swp
composer.lock
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -10,7 +10,7 @@ Consider the following user balance topup function that is susceptible to a race

```php
// A thread-safe version of the above topup function.
public function topup_user_balance( $topup ) {
public function topup_user_balance( $user_id, $topup ) {
$balance = get_user_meta( $user_id, 'balance', true );
$balance = $balance + $topup;
update_user_meta( $user_id, 'balance', $balance = get_user_meta( $user_id, 'balance', true ) + $topup );
Expand All @@ -22,7 +22,7 @@ Try to call the above code 100 times in 16 threads. The balance will be less tha

```php
// A thread-safe version of the above topup function.
public function topup_user_balance( $topup ) {
public function topup_user_balance( $user_id, $topup ) {
$user_balance_lock = new WP_Lock( "$user_id:meta:balance" );
$user_balance_lock->acquire( WP_Lock::EXCLUSIVE );

Expand Down
5 changes: 5 additions & 0 deletions composer.json
@@ -0,0 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^6"
}
}
32 changes: 32 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
>
<php>
<const name="DOING_TESTS" value="1" />
</php>
<testsuites>
<testsuite name="WP_Lock concurrency mutexes for WordPress test suite">
<directory suffix=".php">tests/lock</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">.</directory>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
<directory>wordpress-develop</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="/tmp/clover.xml" charset="UTF-8" />
</logging>
</phpunit>
10 changes: 10 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,10 @@
<?php
$test_root = getenv( 'WP_TESTS_DIR' ) ? : dirname( __FILE__ ) . '/../wordpress-develop/tests/phpunit';

require $test_root . '/includes/bootstrap.php';

function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../plugin.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
6 changes: 6 additions & 0 deletions tests/lock/flock.php
@@ -0,0 +1,6 @@
<?php
class WP_Lock_Backend_flock_UnitTestCase extends WP_UnitTestCase {
public function test_true() {
$this->assertTrue( true );
}
}

0 comments on commit 19d2e07

Please sign in to comment.