Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,88 @@ | ||
<?php | ||
|
||
namespace Amp\Mysql\Test; | ||
|
||
use Amp\ByteStream; | ||
use Amp\Loop; | ||
use Amp\Process\Process; | ||
use function Amp\delay; | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
const DB_HOST = 'localhost:10101'; | ||
const DB_USER = 'root'; | ||
const DB_PASS = ''; | ||
const DB_NAME = 'test'; | ||
|
||
$pipes = []; | ||
$buf = ""; | ||
|
||
/* cleanup in case it wasn't terminated properly... */ | ||
$pidfile = __DIR__."/mysql.pid"; | ||
if (\file_exists($pidfile)) { | ||
if (\stripos(PHP_OS, "win") === 0) { | ||
\shell_exec("Taskkill /PID ".\file_get_contents($pidfile)." /F"); | ||
} else { | ||
\shell_exec("kill -9 `cat '$pidfile'` 2>/dev/null"); | ||
(function (): void { | ||
/* cleanup in case it wasn't terminated properly... */ | ||
$pidfile = __DIR__ . "/mysql.pid"; | ||
if (\file_exists($pidfile)) { | ||
if (\stripos(PHP_OS, "win") === 0) { | ||
\shell_exec("Taskkill /PID " . \file_get_contents($pidfile) . " /F"); | ||
} else { | ||
\shell_exec("kill -9 `cat '$pidfile'` 2>/dev/null"); | ||
} | ||
\sleep(1); | ||
} | ||
\sleep(1); | ||
} | ||
if (\file_exists(__DIR__."/mysql_db")) { | ||
$rm_all = function ($dir) use (&$rm_all) { | ||
$files = \glob("$dir/*"); | ||
if (\is_array($files)) { | ||
foreach ($files as $file) { | ||
if (\is_dir($file)) { | ||
$rm_all($file); | ||
\rmdir($file); | ||
} else { | ||
\unlink($file); | ||
|
||
if (\file_exists(__DIR__ . "/mysql_db")) { | ||
$rm_all = function ($dir) use (&$rm_all) { | ||
$files = \glob("$dir/*"); | ||
if (\is_array($files)) { | ||
foreach ($files as $file) { | ||
if (\is_dir($file)) { | ||
$rm_all($file); | ||
\rmdir($file); | ||
} else { | ||
\unlink($file); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
$rm_all(__DIR__ . "/mysql_db"); | ||
} else { | ||
@\mkdir(__DIR__ . "/mysql_db"); | ||
} | ||
|
||
$proc = \proc_open("mysqld --defaults-file=my.cnf --initialize-insecure", [2 => ["pipe", "w"]], $pipes, __DIR__); | ||
$stderr = $pipes[2]; | ||
do { | ||
if (!($row = \fgets($stderr)) || \preg_match("# \[ERROR\] #", $row)) { | ||
print "\nERROR: Aborting, couldn't start mysql successfully\n$buf$row"; | ||
exit(127); | ||
}; | ||
$rm_all(__DIR__ . "/mysql_db"); | ||
} else { | ||
@\mkdir(__DIR__ . "/mysql_db"); | ||
} | ||
$buf .= $row; | ||
} while (!\preg_match("(root@localhost is created with an empty password)", $row)); | ||
\sleep(3); // :-( | ||
\proc_terminate($proc, 9); | ||
@\proc_terminate($proc, 9); | ||
})(); | ||
|
||
$proc = \proc_open("mysqld --defaults-file=my.cnf --user=root", [2 => ["pipe", "w"]], $pipes, __DIR__); | ||
Loop::run(function () { | ||
print "\rCreating mysql server... "; | ||
|
||
\register_shutdown_function(function () use ($proc) { | ||
\proc_terminate($proc, 9); | ||
}); | ||
$dir = __DIR__; | ||
|
||
$process = new Process("mysqld --defaults-file={$dir}/my.cnf --initialize-insecure", __DIR__); | ||
|
||
$stderr = $pipes[2]; | ||
do { | ||
if (!($row = \fgets($stderr)) || \preg_match("# \[ERROR\] #", $row)) { | ||
print "\nERROR: Aborting, couldn't start mysql successfully\n$buf$row"; | ||
yield $process->start(); | ||
|
||
$stderr = yield ByteStream\buffer($process->getStderr()); | ||
|
||
if (\preg_match("# \[ERROR\] #", $stderr)) { | ||
print "\nERROR: Aborting, couldn't start mysql successfully\n{$stderr}"; | ||
exit(127); | ||
} | ||
$buf .= $row; | ||
} while (!\preg_match("(^Version: '[0-9.a-zA-Z]+')", $row)); | ||
|
||
yield $process->join(); | ||
|
||
$process = new Process("mysqld --defaults-file={$dir}/my.cnf --user=root", __DIR__); | ||
|
||
yield $process->start(); | ||
|
||
print "\rStarting mysqld... "; | ||
|
||
$db = new mysqli(DB_HOST, DB_USER, DB_PASS); | ||
$db->query("CREATE DATABASE test"); | ||
$db->query("CREATE TABLE test.main (a INT(11), b INT(11))"); | ||
$db->query("INSERT INTO test.main VALUES (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)"); | ||
$db->close(); | ||
yield delay(1000); // Give mysqld time to start. | ||
|
||
print "\rCreating test database..."; | ||
|
||
$db = new \mysqli(DB_HOST, DB_USER, DB_PASS); | ||
$db->query("CREATE DATABASE test"); | ||
$db->query("CREATE TABLE test.main (a INT(11), b INT(11))"); | ||
$db->query("INSERT INTO test.main VALUES (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)"); | ||
$db->close(); | ||
|
||
print "\r"; | ||
|
||
\register_shutdown_function(function () use ($process): void { | ||
$process->signal(\SIGTERM); | ||
}); | ||
}); |