Skip to content

Commit

Permalink
fix 641 (#644)
Browse files Browse the repository at this point in the history
* build(composer.json) remove unused dev req.

* fix(ChromedriverInstaller) correctly detect Chrome version on Windows

* fix(ChromedriverInstaller) Windows support

* doc(docs) add troubleshooting guide first entry

* refactor(src) use Syphony process in Loop

* fix(ChromedriverInstaller) command on Windows

* fix(ChromedriverInstaller) version detection
  • Loading branch information
lucatume authored Sep 29, 2023
1 parent e95647b commit 1fee145
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 311 deletions.
3 changes: 3 additions & 0 deletions bin/setup-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use lucatume\WPBrowser\WordPress\InstallationState\InstallationStateInterface;
use lucatume\WPBrowser\WordPress\InstallationState\Scaffolded;

$dockerComposeEnvFile = escapeshellarg(dirname(__DIR__) . '/tests/.env');
`docker compose --env-file $dockerComposeEnvFile up --wait`;

require_once dirname(__DIR__) . '/vendor/autoload.php';

global $_composer_autoload_path, $_composer_bin_dir;
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@
"ifsnop/mysqldump-php": "^2.12"
},
"require-dev": {
"ext-sockets": "*",
"ext-pcntl": "*",
"ext-mysqli": "*",
"lucatume/codeception-snapshot-assertions": "^1.0.0",
"gumlet/php-image-resize": "^1.6",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-symfony": "^1.3",
"squizlabs/php_codesniffer": "^3.7",
"webdriver-binary/binary-chromedriver": "^6.1"
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ use this command to update it.
This command will download the latest version of Chromedriver compatible with the Chrome version installed on your
machine in the Composer vendor directory.

> Note: if the download fails, it might be [a certificate issue](troubleshooting.md#downloads-fail-in-windows).

### `generate:wpunit`

Enable the command with:
Expand Down
18 changes: 18 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Troubleshooting common issues

### Downloads fail in Windows

If you're using code or commands, e.g. [the `chromedriver:update` one](commands.md#chromedriverupdate), that download files and those keep failing with a message like the following:

```
File ... download failed: SSL certificate problem: unable to get local issuer certificate
```

It's likely the issue originates from PHP not having access to the system certificate store.

You can fix this by downloading the [certificates](https://curl.haxx.se/docs/caextract.html) file and setting the `curl.cainfo` and `openssl.cafile` PHP configuration options to point to it:

```ini
curl.cainfo = "C:\path\to\cacert.pem"
openssl.cafile = "C:\path\to\cacert.pem"
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nav:
- wp:db:export: 'commands#wpdbexport'
- chromedriver:update: 'commands#chromedriverupdate'
- generate:wpunit: 'commands#generatewpunit'
- Troubleshooting: 'troubleshooting.md'
- Sponsorship: 'https://github.com/sponsors/lucatume'
- Changelog: 'https://github.com/lucatume/wp-browser/blob/master/CHANGELOG.md'
- Version 3 documentation:
Expand Down
110 changes: 0 additions & 110 deletions src/Process/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Loop
* @var array<string,Worker>
*/
private array $workers = [];
private bool $debugMode = false;

private bool $fastFailureFlagRaised = false;

Expand Down Expand Up @@ -107,14 +106,6 @@ public static function executeClosureOrFail(
return self::executeClosure($toInstallWordPressNetwork, $timeout, $options);
}

/**
* @return array<Worker>
*/
public function getWorkers(): array
{
return $this->workers;
}

/**
* @param array<int|string,Worker|callable> $workers
* @param array{
Expand Down Expand Up @@ -193,7 +184,6 @@ private function startWorker(): void
$w = Running::fromWorker($runnableWorker);
$this->started[$w->getId()] = $w;
$this->running[$w->getId()] = $w;
$this->debugLine("Worker {$w->getId()} started.");
$this->peakParallelism = max((int)$this->peakParallelism, count($this->running));
} catch (Throwable $t) {
$this->terminateAllRunningWorkers();
Expand Down Expand Up @@ -229,7 +219,6 @@ private function collectOutput(): void
$read = array_reduce(
$this->running,
function (array $streams, Running $w) use (&$readIndexToWorkerMap): array {
$this->debugLine("Collecting output of worker {$w->getId()}.");
$streams[] = $w->getStdoutStream();
$streams[] = $w->getStdErrStream();
$readIndexToWorkerMap[count($streams) - 2] = $w;
Expand Down Expand Up @@ -305,7 +294,6 @@ public function run(): Loop

if ($fastFailureFlagRaised) {
$this->fastFailureFlagRaised = true;
$this->debugLine('Fast failure flag raised, terminating all workers.');
$this->terminateAllRunningWorkers();
break 2;
}
Expand All @@ -314,7 +302,6 @@ public function run(): Loop
$exitedWorker = Exited::fromRunningWorker($w);
$this->exited[$w->getId()] = $exitedWorker;
unset($this->running[$w->getId()]);
$this->debugLine("Worker {$w->getId()} exited with status {$w->getExitCode()}.");
$this->startWorker();
continue;
}
Expand All @@ -323,7 +310,6 @@ public function run(): Loop
$exitedWorker = $w->terminate();
$this->exited[$w->getId()] = $exitedWorker;
unset($this->running[$w->getId()]);
$this->debugLine("Worker {$w->getId()} took too long, terminated.");
$this->startWorker();
}
}
Expand All @@ -334,10 +320,6 @@ public function run(): Loop
$this->collectOutput();
$this->collectResults();

if ($this->debugMode) {
$this->assertPostRunConditions();
}

return $this;
}

Expand All @@ -353,96 +335,13 @@ private function terminateAllRunningWorkers(): Loop
{
foreach ($this->running as $runningWorker) {
$this->exited[$runningWorker->getId()] = $runningWorker->terminate();
$this->debugLine("Worker {$runningWorker->getId()} terminated.");
}

$this->running = array_diff_key($this->running, $this->exited);

return $this;
}

protected function assertPostRunConditions(): void
{
assert(count($this->started) >= 1);
assert(count($this->started) === count($this->exited));
$uncollectedWorkerOutput = array_map(static function (Running $w): array {
return ['stdout' => $w->readStdoutStream(), 'stderr' => $w->readStderrStream()];
}, array_values($this->started));
assert(array_sum(array_merge(...$uncollectedWorkerOutput)) === 0, print_r($uncollectedWorkerOutput, true));
if (!$this->fastFailure) {
assert(count($this->started) === count($this->workers));
}
}

public function getPeakParallelism(): int
{
return $this->peakParallelism;
}

private function getExitedWorkerById(string $workerId): ?Exited
{
foreach ($this->exited as $id => $worker) {
if ($workerId === $id) {
return $worker;
}
}

return null;
}

private function getRunningWorkerById(string $workerId): ?Running
{
foreach ($this->running as $id => $worker) {
if ($workerId === $id) {
return $worker;
}
}

return null;
}

private function getWorkerById(string $workerId): ?WorkerInterface
{
if ($worker = $this->getExitedWorkerById($workerId)) {
return $worker;
}

if ($worker = $this->getRunningWorkerById($workerId)) {
return $worker;
}

foreach ($this->workers as $id => $worker) {
if ($workerId === $id) {
return $worker;
}
}

return null;
}

public function removeWorker(string $workerId): ?WorkerInterface
{
if ($worker = $this->getWorkerById($workerId)) {
unset(
$this->started[$workerId],
$this->running[$workerId],
$this->exited[$workerId],
$this->results[$workerId],
$this->workers[$workerId]
);

if ($worker instanceof Running) {
return $worker->terminate();
}

$this->sortWorkersByResource();

return $worker;
}

return null;
}

public function failed(): bool
{
return $this->fastFailure && $this->fastFailureFlagRaised;
Expand All @@ -467,13 +366,4 @@ private function buildWorker(string $id, callable $worker): Worker
{
return new Worker($id, $worker, [], []);
}

private function debugLine(string $line): void
{
if (!$this->debugMode) {
return;
}

codecept_debug("Loop: $line");
}
}
Loading

0 comments on commit 1fee145

Please sign in to comment.