|
| 1 | +<?php |
| 2 | +/** Native wpdb opens independent logical connections that contend for advisory locks like MySQL connections. */ |
| 3 | + |
| 4 | +declare( strict_types=1 ); |
| 5 | + |
| 6 | +define( 'ABSPATH', __DIR__ . '/' ); |
| 7 | +define( 'DB_NAME', 'wordpress_tests' ); |
| 8 | + |
| 9 | +class wpdb { |
| 10 | + public string $prefix = ''; |
| 11 | + public string $base_prefix = ''; |
| 12 | + public bool $ready = false; |
| 13 | + public int $num_queries = 0; |
| 14 | + public int $num_rows = 0; |
| 15 | + public int $rows_affected = 0; |
| 16 | + public int $insert_id = 0; |
| 17 | + public string $last_error = ''; |
| 18 | + public ?string $last_query = null; |
| 19 | + public ?bool $is_mysql = null; |
| 20 | + public string $func_call = ''; |
| 21 | + public array $last_result = array(); |
| 22 | + protected array $col_info = array(); |
| 23 | + protected bool $check_current_query = true; |
| 24 | + protected bool $result = false; |
| 25 | + |
| 26 | + public function set_prefix( string $prefix ): string { |
| 27 | + $this->prefix = $prefix; |
| 28 | + $this->base_prefix = $prefix; |
| 29 | + return $prefix; |
| 30 | + } |
| 31 | + |
| 32 | + public function flush(): void { |
| 33 | + $this->last_result = array(); |
| 34 | + $this->col_info = array(); |
| 35 | + $this->last_error = ''; |
| 36 | + } |
| 37 | + |
| 38 | + public function add_placeholder_escape( string $value ): string { return $value; } |
| 39 | + public function remove_placeholder_escape( string $value ): string { return $value; } |
| 40 | +} |
| 41 | + |
| 42 | +require_once __DIR__ . '/../inc/native/class-wp-markdown-native-wpdb.php'; |
| 43 | + |
| 44 | +$root = sys_get_temp_dir() . '/mdi-native-open-connection-' . bin2hex( random_bytes( 6 ) ); |
| 45 | +if ( ! mkdir( $root . '/_options', 0777, true ) || ! mkdir( $root . '/_tables', 0777, true ) ) { |
| 46 | + throw new RuntimeException( 'Failed to create the open-connection probe fixture.' ); |
| 47 | +} |
| 48 | + |
| 49 | +/** @return int|string|null */ |
| 50 | +function open_connection_scalar( WP_Markdown_Native_WPDB $db, string $sql ): int|string|null { |
| 51 | + $db->query( $sql ); |
| 52 | + return isset( $db->last_result[0] ) ? current( get_object_vars( $db->last_result[0] ) ) : null; |
| 53 | +} |
| 54 | + |
| 55 | +$factory = static fn(): WP_Markdown_Query_Runtime => WP_Markdown_Native_Runtime_Factory::runtime( $root ); |
| 56 | +$primary = new WP_Markdown_Native_WPDB( $factory(), 'wp_', $factory ); |
| 57 | +$second = $primary->open_connection(); |
| 58 | +$third = $second->open_connection(); |
| 59 | + |
| 60 | +$owner_acquire = open_connection_scalar( $primary, "SELECT GET_LOCK('venue-lock', 0)" ); |
| 61 | +$second_contended = open_connection_scalar( $second, "SELECT GET_LOCK('venue-lock', 0)" ); |
| 62 | +$second_other_lock = open_connection_scalar( $second, "SELECT GET_LOCK('other-lock', 0)" ); |
| 63 | +$third_contended = open_connection_scalar( $third, "SELECT GET_LOCK('other-lock', 0)" ); |
| 64 | +$owner_release = open_connection_scalar( $primary, "SELECT RELEASE_LOCK('venue-lock')" ); |
| 65 | +$second_acquire = open_connection_scalar( $second, "SELECT GET_LOCK('venue-lock', 0)" ); |
| 66 | +$second->close(); |
| 67 | +$after_close = open_connection_scalar( $third, "SELECT GET_LOCK('other-lock', 0)" ); |
| 68 | + |
| 69 | +$without_factory = new WP_Markdown_Native_WPDB( $factory(), 'wp_' ); |
| 70 | +try { |
| 71 | + $without_factory->open_connection(); |
| 72 | + $fails_closed = false; |
| 73 | +} catch ( LogicException ) { |
| 74 | + $fails_closed = true; |
| 75 | +} |
| 76 | + |
| 77 | +$assertions = array( |
| 78 | + 'open_connection returns a distinct native wpdb' => $second instanceof WP_Markdown_Native_WPDB && $second !== $primary && $third !== $second, |
| 79 | + 'a held lock excludes the opened connection' => '1' === $owner_acquire && '0' === $second_contended, |
| 80 | + 'the opened connection owns its own locks' => '1' === $second_other_lock && '0' === $third_contended, |
| 81 | + 'release on the owner transfers the lock' => '1' === $owner_release && '1' === $second_acquire, |
| 82 | + 'closing an opened connection releases its locks' => '1' === $after_close, |
| 83 | + 'the opened connection keeps the table prefix' => 'wp_' === $second->prefix, |
| 84 | + 'without a factory open_connection fails closed' => $fails_closed, |
| 85 | +); |
| 86 | +$passed = ! in_array( false, $assertions, true ); |
| 87 | +fwrite( $passed ? STDOUT : STDERR, json_encode( array( 'assertions' => $assertions, 'passed' => $passed ), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ) . "\n" ); |
| 88 | +exit( $passed ? 0 : 1 ); |
0 commit comments