/** * Test that the table is actually locked. */ public function test02() { $application = new Application(); $application->add(new AuditCommand()); // Start process that inserts rows into TABLE1. $pid = pcntl_fork(); if ($pid == 0) { // Child process. pcntl_exec(__DIR__ . '/config/generator.php'); } // Parent process. sleep(2); /** @var AuditCommand $command */ $command = $application->find('audit'); $command->setRewriteConfigFile(false); $commandTester = new CommandTester($command); $commandTester->execute(['command' => $command->getName(), 'config file' => __DIR__ . '/config/audit.json']); // Tell the generator it is time to stop. posix_kill($pid, SIGUSR1); $status = $commandTester->getStatusCode(); $this->assertSame(0, $status, 'status code'); pcntl_waitpid($pid, $status); $this->assertEquals(0, $status); // Reconnect to DB. StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema); // It can take some time before that all rows generated by $generator are visible by this process. $n1 = 0; $n2 = 0; sleep(5); for ($i = 0; $i < 60; $i++) { $n1 = StaticDataLayer::executeSingleton1("select AUTO_INCREMENT - 1 \n from information_schema.TABLES\n where TABLE_SCHEMA = 'test_data'\n and TABLE_NAME = 'TABLE1'"); $n2 = StaticDataLayer::executeSingleton1('select count(*) from test_audit.TABLE1'); if (4 * $n1 == $n2) { break; } sleep(3); } $this->assertEquals(4 * $n1, $n2, 'count'); }
/** * Executes a query that returns 1 and only 1 row with 1 column. * Throws an exception if the query selects none, 2 or more rows. * * @param string $query The SQL statement. * * @return int|string The selected row. */ public static function executeSingleton1($query) { self::logQuery($query); return self::$dl->executeSingleton1($query); }