-
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
Showing
9 changed files
with
146 additions
and
7 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vendor | ||
.phpunit.cache | ||
.phpunit.cache | ||
phpstan.neon |
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
File renamed without changes.
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
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
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,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace AO\Internal; | ||
|
||
use Amp\{DeferredFuture}; | ||
use AO\Package\OutPackage; | ||
use AO\SendPriority; | ||
|
||
final class SendQueue { | ||
/** | ||
* @var array<int,SendQueueItem[]> | ||
* | ||
* @psalm-var array<int,list<SendQueueItem>> | ||
*/ | ||
private array $queue = []; | ||
|
||
/** @param DeferredFuture<void> $future */ | ||
public function push(OutPackage $package, SendPriority $priority, ?DeferredFuture $future=null): void { | ||
if (!isset($this->queue[$priority->value])) { | ||
$this->queue[$priority->value] = []; | ||
} | ||
$this->queue[$priority->value] []= new SendQueueItem( | ||
future: $future, | ||
package: $package, | ||
); | ||
} | ||
|
||
public function shift(): ?SendQueueItem { | ||
$priorities = array_keys($this->queue); | ||
asort($priorities); | ||
foreach ($priorities as $priority) { | ||
if (isset($this->queue[$priority]) && count($this->queue[$priority]) > 0) { | ||
return array_shift($this->queue[$priority]); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** Get the number of total items currently in the queue */ | ||
public function getSize(): int { | ||
$priorities = SendPriority::cases(); | ||
$size = 0; | ||
foreach ($priorities as $priority) { | ||
if (!isset($this->queue[$priority->value])) { | ||
continue; | ||
} | ||
$size += count($this->queue[$priority->value]); | ||
} | ||
return $size; | ||
} | ||
} |
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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace AO\Internal; | ||
|
||
use Amp\DeferredFuture; | ||
use AO\Package\OutPackage; | ||
|
||
final class SendQueueItem { | ||
/** @param DeferredFuture<void> $future */ | ||
public function __construct( | ||
public readonly ?DeferredFuture $future, | ||
public readonly OutPackage $package, | ||
) { | ||
} | ||
} |
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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace AO; | ||
|
||
enum SendPriority: int { | ||
case High = 1; | ||
case Medium = 2; | ||
case Low = 3; | ||
} |
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,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace AO\Tests; | ||
|
||
use AO\Internal\SendQueue; | ||
use AO\Package\Out\Pong; | ||
use AO\{SendPriority}; | ||
use PHPUnit\Framework\Attributes\{Small}; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[Small] | ||
class SendQueueTest extends TestCase { | ||
public function testCorrectOrder(): void { | ||
$queue = new SendQueue(); | ||
$queue->push(new Pong('1'), SendPriority::Low); | ||
$queue->push(new Pong('2'), SendPriority::High); | ||
$queue->push(new Pong('3'), SendPriority::Medium); | ||
$queue->push(new Pong('4'), SendPriority::High); | ||
$queue->push(new Pong('5'), SendPriority::Low); | ||
$queue->push(new Pong('6'), SendPriority::Medium); | ||
|
||
foreach (['2', '4', '3', '6', '1', '5'] as $prio) { | ||
$next = $queue->shift(); | ||
$this->assertNotNull($next); | ||
|
||
/** @var Pong */ | ||
$package = $next->package; | ||
$this->assertInstanceOf(Pong::class, $package, 'Wrong package dequeued'); | ||
|
||
$this->assertSame($package->extra, $prio, 'The order of priority is not uphold'); | ||
} | ||
|
||
$next = $queue->shift(); | ||
$this->assertNull($next); | ||
} | ||
} |