Skip to content

Commit c871576

Browse files
authored
jetpack: Use locking in CI coverage test (#45851)
Jetpack_Sync_Themes_Test creates some files on class setup and deletes them on teardown. When the CI coverage test tries to run multiple copies in parallen (for normal and multisite), these can wind up interfering with each other. Avoid this by locking a file when running the CI coverage test.
1 parent eda4c21 commit c871576

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Significance: patch
2+
Type: other
3+
Comment: CI: Use locking to avoid multiple instances of Jetpack_Sync_Themes_Test from interfering with each other.
4+
5+

projects/plugins/jetpack/tests/action-test-coverage.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -eo pipefail
44

5+
export PHPUNIT_JETPACK_TESTSUITE_IS_PARALLEL=true
6+
57
# To run the PHP tests in parallel, we need to create custom configs for each.
68
for test in lfs multi; do
79
P="${WP_TESTS_CONFIG_FILE_PATH%.php}.$test.php"

projects/plugins/jetpack/tests/php/sync/Jetpack_Sync_Themes_Test.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,32 @@ class Jetpack_Sync_Themes_Test extends Jetpack_Sync_TestBase {
5151
'theme-file-sync-child',
5252
);
5353

54+
/**
55+
* Lock file.
56+
*
57+
* @var resource|null
58+
*/
59+
protected static $lockfile = null;
60+
5461
/**
5562
* Move Dummy Themes to proper location for testing.
63+
*
64+
* @throws RuntimeException If locking is needed and fails.
5665
*/
5766
public static function set_up_before_class() {
5867
parent::set_up_before_class();
5968

69+
// For CI coverage tests, use a lock file to avoid multiple copies of this test from interfering with each other.
70+
if ( getenv( 'PHPUNIT_JETPACK_TESTSUITE_IS_PARALLEL' ) === 'true' ) {
71+
static::$lockfile = fopen( WP_CONTENT_DIR . '/themes/.jplock', 'c+' );
72+
if ( ! static::$lockfile ) {
73+
throw new RuntimeException( 'Failed to open lockfile ' . WP_CONTENT_DIR . '/themes/.jplock' );
74+
}
75+
if ( ! flock( static::$lockfile, LOCK_EX ) ) {
76+
throw new RuntimeException( 'Failed to lock lockfile ' . WP_CONTENT_DIR . '/themes/.jplock' );
77+
}
78+
}
79+
6080
// Copy themes from tests/php/files/ to wp-content/themes.
6181
foreach ( static::$themes as $theme ) {
6282
$source_dir = __DIR__ . '/../files/' . $theme;
@@ -86,6 +106,10 @@ public static function tear_down_after_class() {
86106

87107
rmdir( $dest_dir );
88108
}
109+
110+
if ( static::$lockfile ) {
111+
fclose( static::$lockfile );
112+
}
89113
}
90114

91115
/**

0 commit comments

Comments
 (0)