-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29a9ac0
commit 6244058
Showing
11 changed files
with
655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orangesoft\Throttler\Tests\Cluster; | ||
|
||
use Orangesoft\Throttler\Cluster\Cluster; | ||
use Orangesoft\Throttler\Cluster\ClusterPool; | ||
use Orangesoft\Throttler\Cluster\ClusterSet; | ||
use Orangesoft\Throttler\Collection\InMemoryCollection; | ||
use Orangesoft\Throttler\Collection\Node; | ||
use Orangesoft\Throttler\Collection\NodeInterface; | ||
use Orangesoft\Throttler\Counter\InMemoryCounter; | ||
use Orangesoft\Throttler\RandomThrottler; | ||
use Orangesoft\Throttler\RoundRobinThrottler; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ClusterTest extends TestCase | ||
{ | ||
public function testBalance(): void | ||
{ | ||
$pool = new ClusterPool( | ||
new ClusterSet(new RoundRobinThrottler(new InMemoryCounter()), ['a']), | ||
new ClusterSet(new RandomThrottler(), ['b', 'c']), | ||
); | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
$cluster = new Cluster('a', $collection); | ||
|
||
$expectedNodes = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
]; | ||
$actualNodes = []; | ||
|
||
for ($i = 0; $i < 6; ++$i) { | ||
$actualNodes[] = $cluster->balance($pool); | ||
} | ||
|
||
$this->assertSame($expectedNodes, array_map(static fn (NodeInterface $actualNode): string => $actualNode->getName(), $actualNodes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orangesoft\Throttler\Tests\Collection; | ||
|
||
use Orangesoft\Throttler\Collection\InMemoryCollection; | ||
use Orangesoft\Throttler\Collection\Node; | ||
use Orangesoft\Throttler\Collection\NodeInterface; | ||
use Orangesoft\Throttler\Collection\Sort\Asc; | ||
use Orangesoft\Throttler\Collection\Sort\Desc; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InMemoryCollectionTest extends TestCase | ||
{ | ||
public function testCreateCollectionWithUniqueNodes(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$expectedResult = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
]; | ||
|
||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $collection->toArray())); | ||
} | ||
|
||
public function testCreateCollectionWithNotUniqueNodes(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('All nodes must be unique, "192.168.0.1" given as duplicate.'); | ||
|
||
new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.1'), | ||
]); | ||
} | ||
|
||
public function testAddNodeToExistedCollection(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
]); | ||
|
||
$other = $collection->add(new Node('192.168.0.3')); | ||
|
||
$expectedResult = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
]; | ||
|
||
$this->assertNotSame($collection, $other); | ||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $other->toArray())); | ||
} | ||
|
||
public function testAddTheSameNodeToExistedCollection(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
]); | ||
|
||
$this->expectException(\UnexpectedValueException::class); | ||
$this->expectExceptionMessage('The node "192.168.0.2" has been already added.'); | ||
|
||
$collection->add(new Node('192.168.0.2')); | ||
} | ||
|
||
public function testGetNodeByKey(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$expectedResult = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
]; | ||
$actualResult = []; | ||
|
||
for ($i = 0; $i < 3; ++$i) { | ||
$actualResult[] = $collection->get($i); | ||
} | ||
|
||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $actualResult)); | ||
} | ||
|
||
public function testGetOutOfRangeNodeByKey(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$this->expectException(\OutOfRangeException::class); | ||
$this->expectExceptionMessage('Can\'t get node at key "3".'); | ||
|
||
$collection->get(3); | ||
} | ||
|
||
public function testHasNodeInCollection(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$this->assertTrue($collection->has(new Node('192.168.0.1'))); | ||
} | ||
|
||
public function testHasNodeInEmptyCollection(): void | ||
{ | ||
$collection = new InMemoryCollection(); | ||
|
||
$this->assertFalse($collection->has(new Node('192.168.0.1'))); | ||
} | ||
|
||
public function testRemoveNode(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$other = $collection->remove(new Node('192.168.0.3')); | ||
|
||
$expectedResult = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
]; | ||
|
||
$this->assertNotSame($collection, $other); | ||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $other->toArray())); | ||
} | ||
|
||
public function testRemoveNotAddedNode(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
]); | ||
|
||
$this->expectException(\UnexpectedValueException::class); | ||
$this->expectExceptionMessage('The node "192.168.0.3" hasn\'t been already added.'); | ||
|
||
$collection->remove(new Node('192.168.0.3')); | ||
} | ||
|
||
public function testSortCollectionByWeightAscending(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1', 3), | ||
new Node('192.168.0.2', 2), | ||
new Node('192.168.0.3', 1), | ||
]); | ||
|
||
$other = $collection->sort(new Asc()); | ||
|
||
$expectedResult = [ | ||
'192.168.0.3', | ||
'192.168.0.2', | ||
'192.168.0.1', | ||
]; | ||
|
||
$this->assertNotSame($collection, $other); | ||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $other->toArray())); | ||
} | ||
|
||
public function testSortCollectionByWeightDescending(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1', 1), | ||
new Node('192.168.0.2', 2), | ||
new Node('192.168.0.3', 3), | ||
]); | ||
|
||
$other = $collection->sort(new Desc()); | ||
|
||
$expectedResult = [ | ||
'192.168.0.3', | ||
'192.168.0.2', | ||
'192.168.0.1', | ||
]; | ||
|
||
$this->assertNotSame($collection, $other); | ||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $other->toArray())); | ||
} | ||
|
||
public function testCollectionTraversable(): void | ||
{ | ||
$collection = new InMemoryCollection([ | ||
new Node('192.168.0.1'), | ||
new Node('192.168.0.2'), | ||
new Node('192.168.0.3'), | ||
]); | ||
|
||
$expectedResult = [ | ||
'192.168.0.1', | ||
'192.168.0.2', | ||
'192.168.0.3', | ||
]; | ||
$actualNodes = []; | ||
|
||
foreach ($collection as $key => $node) { | ||
$actualNodes[$key] = $node; | ||
} | ||
|
||
$this->assertSame($expectedResult, array_map(static fn (NodeInterface $node): string => $node->getName(), $actualNodes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orangesoft\Throttler\Tests\Collection; | ||
|
||
use Orangesoft\Throttler\Collection\Node; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NodeTest extends TestCase | ||
{ | ||
public function testNode(): void | ||
{ | ||
$node = new Node( | ||
name: '192.168.0.1', | ||
weight: 5, | ||
payload: [ | ||
'callback_url' => 'http://127.0.0.1/', | ||
], | ||
); | ||
|
||
$this->assertSame('192.168.0.1', $node->getName()); | ||
$this->assertSame(5, $node->getWeight()); | ||
$this->assertSame(['callback_url' => 'http://127.0.0.1/'], $node->getPayload()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orangesoft\Throttler\Tests\Counter; | ||
|
||
use Orangesoft\Throttler\Counter\InMemoryCounter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InMemoryCounterTest extends TestCase | ||
{ | ||
public function testDefaultInMemoryCounter(): void | ||
{ | ||
$counter = new InMemoryCounter(); | ||
|
||
$expectedResult = range(0, 5); | ||
$actualResult = []; | ||
|
||
for ($i = 0; $i < 6; ++$i) { | ||
$actualResult[] = $counter->next(); | ||
} | ||
|
||
$this->assertSame($expectedResult, $actualResult); | ||
} | ||
|
||
public function testInMemoryCounterWithStartNumber(): void | ||
{ | ||
$counter = new InMemoryCounter( | ||
start: 10, | ||
); | ||
|
||
$expectedResult = range(10, 15); | ||
$actualResult = []; | ||
|
||
for ($i = 0; $i < 6; ++$i) { | ||
$actualResult[] = $counter->next(); | ||
} | ||
|
||
$this->assertSame($expectedResult, $actualResult); | ||
} | ||
|
||
public function testInMemoryCounterWithDifferentNames(): void | ||
{ | ||
$counter = new InMemoryCounter(); | ||
|
||
$expectedResult = [ | ||
0, | ||
0, | ||
1, | ||
1, | ||
2, | ||
2, | ||
]; | ||
$actualResult = []; | ||
|
||
for ($i = 0; $i < 6; ++$i) { | ||
$actualResult[] = $counter->next( | ||
name: 0 === $i % 2 ? 'a' : 'b', | ||
); | ||
} | ||
|
||
$this->assertSame($expectedResult, $actualResult); | ||
} | ||
} |
Oops, something went wrong.