Skip to content

Commit

Permalink
Update test bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Oct 29, 2020
1 parent cbef7dc commit 935d003
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -22,6 +22,7 @@
},
"require-dev": {
"ext-openssl": "*",
"amphp/process": "^1",
"phpunit/phpunit": "^8 || ^7",
"amphp/phpunit-util": "^1.1.2",
"amphp/php-cs-fixer-config": "dev-master",
Expand Down
123 changes: 68 additions & 55 deletions test/bootstrap.php
@@ -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);
});
});

0 comments on commit 935d003

Please sign in to comment.