-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: set up project to use Paratest #136
base: master
Are you sure you want to change the base?
Changes from all commits
946ef08
346d9a2
2922936
8eae11d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,28 +2,43 @@ | |||||
|
||||||
namespace RonasIT\AutoDoc\Drivers; | ||||||
|
||||||
use Illuminate\Support\Facades\ParallelTesting; | ||||||
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract; | ||||||
use RuntimeException; | ||||||
|
||||||
abstract class BaseDriver implements SwaggerDriverContract | ||||||
{ | ||||||
protected string $tempFilePath; | ||||||
protected string $sharedTempFilePath; | ||||||
|
||||||
public function __construct() | ||||||
{ | ||||||
$this->tempFilePath = storage_path('temp_documentation.json'); | ||||||
$this->sharedTempFilePath = storage_path('temp_documentation.json'); | ||||||
|
||||||
$this->tempFilePath = ($token = ParallelTesting::token()) | ||||||
? storage_path("temp_documentation_{$token}.json") | ||||||
: $this->sharedTempFilePath; | ||||||
} | ||||||
|
||||||
public function saveTmpData($data): void | ||||||
public function saveTmpData(array $data): void | ||||||
{ | ||||||
file_put_contents($this->tempFilePath, json_encode($data)); | ||||||
$this->saveJsonToFile($this->tempFilePath, $data); | ||||||
} | ||||||
|
||||||
public function getTmpData(): ?array | ||||||
{ | ||||||
if (file_exists($this->tempFilePath)) { | ||||||
$content = file_get_contents($this->tempFilePath); | ||||||
return $this->getJsonFromFile($this->tempFilePath); | ||||||
} | ||||||
|
||||||
return json_decode($content, true); | ||||||
public function saveSharedTmpData(callable $callback): void | ||||||
{ | ||||||
$this->writeFileWithLock($this->sharedTempFilePath, $callback); | ||||||
} | ||||||
|
||||||
public function getSharedTmpData(): ?array | ||||||
{ | ||||||
if (file_exists($this->sharedTempFilePath)) { | ||||||
return $this->readFileWithLock($this->sharedTempFilePath); | ||||||
} | ||||||
|
||||||
return null; | ||||||
|
@@ -35,4 +50,88 @@ protected function clearTmpData(): void | |||||
unlink($this->tempFilePath); | ||||||
} | ||||||
} | ||||||
|
||||||
protected function saveJsonToFile(string $filePath, array $data): void | ||||||
{ | ||||||
file_put_contents($filePath, json_encode($data)); | ||||||
} | ||||||
|
||||||
protected function getJsonFromFile(string $filePath): ?array | ||||||
{ | ||||||
if (file_exists($filePath)) { | ||||||
$content = file_get_contents($filePath); | ||||||
|
||||||
return json_decode($content, true); | ||||||
} | ||||||
|
||||||
return null; | ||||||
} | ||||||
|
||||||
protected function readFileWithLock(string $filePath): array | ||||||
{ | ||||||
$handle = fopen($filePath, 'r'); | ||||||
|
||||||
try { | ||||||
$this->acquireLock($handle, LOCK_SH); | ||||||
|
||||||
return $this->readJsonFromStream($handle); | ||||||
} finally { | ||||||
flock($handle, LOCK_UN); | ||||||
fclose($handle); | ||||||
} | ||||||
} | ||||||
|
||||||
protected function writeFileWithLock(string $filePath, callable $callback): void | ||||||
{ | ||||||
$handle = fopen($filePath, 'c+'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move |
||||||
|
||||||
try { | ||||||
$this->acquireLock($handle, LOCK_EX | LOCK_NB); | ||||||
|
||||||
$data = $callback($this->readJsonFromStream($handle)); | ||||||
|
||||||
$this->writeJsonToStream($handle, $data); | ||||||
} finally { | ||||||
flock($handle, LOCK_UN); | ||||||
fclose($handle); | ||||||
} | ||||||
} | ||||||
|
||||||
protected function writeJsonToStream($handle, array $data): void | ||||||
{ | ||||||
ftruncate($handle, 0); | ||||||
rewind($handle); | ||||||
fwrite($handle, json_encode($data)); | ||||||
fflush($handle); | ||||||
} | ||||||
|
||||||
protected function readJsonFromStream($handle): ?array | ||||||
{ | ||||||
$content = stream_get_contents($handle); | ||||||
|
||||||
return ($content === false) ? null : json_decode($content, true); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @codeCoverageIgnore | ||||||
*/ | ||||||
protected function acquireLock( | ||||||
$handle, | ||||||
int $operation, | ||||||
int $maxRetries = 20, | ||||||
int $minWaitTime = 100, | ||||||
int $maxWaitTime = 1000, | ||||||
): void { | ||||||
$retryCounter = 0; | ||||||
|
||||||
while (!flock($handle, $operation)) { | ||||||
if ($retryCounter >= $maxRetries) { | ||||||
throw new RuntimeException('Unable to lock file'); | ||||||
} | ||||||
|
||||||
usleep(rand($minWaitTime, $maxWaitTime)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
$retryCounter++; | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
namespace RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers; | ||
|
||
use Illuminate\Contracts\Console\Kernel; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Support\Facades\ParallelTesting; | ||
use PHPUnit\Event\Application\Finished; | ||
use PHPUnit\Event\Application\FinishedSubscriber; | ||
use RonasIT\AutoDoc\Services\SwaggerService; | ||
|
@@ -13,12 +15,18 @@ public function notify(Finished $event): void | |
{ | ||
$this->createApplication(); | ||
|
||
app(SwaggerService::class)->saveProductionData(); | ||
$swaggerService = app(SwaggerService::class); | ||
|
||
if (ParallelTesting::token()) { | ||
$swaggerService->mergeTempDocumentation(); | ||
} | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move it into the |
||
|
||
$swaggerService->saveProductionData(); | ||
} | ||
|
||
protected function createApplication(): void | ||
{ | ||
$app = require base_path('bootstrap/app.php'); | ||
$app = require Application::inferBasePath() . '/bootstrap/app.php'; | ||
|
||
$app->loadEnvironmentFrom('.env.testing'); | ||
$app->make(Kernel::class)->bootstrap(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.