Skip to content

Commit

Permalink
Add tests for pool limit (#105)
Browse files Browse the repository at this point in the history
* Add tests for pool limit

* Add minimum version of amphp/parallel to bypass "queue is not running" error

* Add keywords formatting back
  • Loading branch information
assertchris authored and freekmurze committed Dec 18, 2017
1 parent a4f4589 commit 0ac7398
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"illuminate/view": "~5.5.0"
},
"require-dev": {
"amphp/parallel": "^0.2.0",
"amphp/parallel-functions": "^0.1.2",
"mockery/mockery": "^1.0",
"orchestra/database": "~3.5.0",
Expand Down
34 changes: 34 additions & 0 deletions tests/ParallelMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\CollectionMacros\Test;

use Illuminate\Support\Collection;
use Amp\Parallel\Worker\DefaultPool;
use Symfony\Component\Stopwatch\Stopwatch;

class ParallelMapTest extends TestCase
Expand Down Expand Up @@ -33,6 +34,32 @@ public function it_can_perform_async_map_operations()
$this->assertEquals([10, 20, 30, 40, 50], $collection->toArray());
}

/** @test */
public function it_can_limit_worker_pool_size_with_pool()
{
$this->startStopWatch();

$pool = new DefaultPool(1);

$collection = Collection::make([1, 2, 3])->parallelMap(function (int $number) {
sleep(1);
}, $pool);

$this->assertTookMoreThanSeconds(2);
}

/** @test */
public function it_can_limit_worker_pool_size_with_int()
{
$this->startStopWatch();

$collection = Collection::make([1, 2, 3])->parallelMap(function (int $number) {
sleep(1);
}, 1);

$this->assertTookMoreThanSeconds(2);
}

/** @test */
public function it_can_handle_a_large_collection()
{
Expand Down Expand Up @@ -78,4 +105,11 @@ protected function assertTookLessThanSeconds(int $seconds)

$this->assertLessThan($seconds * 1000, $durationInMilliseconds);
}

protected function assertTookMoreThanSeconds(int $seconds)
{
$durationInMilliseconds = $this->stopWatch->stop('test')->getDuration();

$this->assertGreaterThan($seconds * 1000, $durationInMilliseconds);
}
}

0 comments on commit 0ac7398

Please sign in to comment.