Skip to content

Commit

Permalink
Add basic async test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorial1024 committed Dec 20, 2024
1 parent 9d6949c commit fff4ef6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function start(): void
{
// assume unix for now
$serializedTask = $this->toBase64Serial();
$this->theTask = Process::quietly()->start("php artisan async:run $serializedTask");
$this->runnerProcess = Process::quietly()->start("php artisan async:run $serializedTask");
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/AsyncTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class AsyncTaskTest extends BaseTestCase
{
// directly running the runner

public function testCanRunClosure()
{
// tests that our AsyncTask can run closures correctly.
Expand Down Expand Up @@ -39,4 +41,30 @@ public function testCanRunInterface()

unlink($testFileName);
}

// ---------

// integration test with the cli artisan via a mocked artisan file, which tests various features of this library

public function testAsyncBasic()
{
// tests that we can dispatch async tasks to the cli artisan
$testFileName = $this->getStoragePath("testAsyncBasic.txt");
$message = "Hello world!";
$task = new AsyncTask(function () use ($testFileName, $message) {
$fp = fopen($testFileName, "w");
fwrite($fp, $message);
fflush($fp);
fclose($fp);
});
$task->start();

// sleep a bit to wait for the async
sleep(1);

$this->assertFileExists($testFileName);
$this->assertStringEqualsFile($testFileName, $message);

unlink($testFileName);
}
}

0 comments on commit fff4ef6

Please sign in to comment.