-
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.
test(Background): add functional test for membership sync
- Loading branch information
1 parent
d6bf64b
commit 677c4dd
Showing
2 changed files
with
195 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,72 @@ | ||
<?php | ||
|
||
namespace DataFixtures\Background; | ||
|
||
use App\Shared\Domain\Const\HistoryEventType; | ||
use App\Shared\Domain\Const\MembershipState; | ||
use App\Shared\Domain\Const\NodeState; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\HistoryEvent; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\Node; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\VirtualNode; | ||
use Doctrine\Bundle\FixturesBundle\Fixture; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Symfony\Component\Uid\UuidV7; | ||
|
||
class SyncMembershipFixtures extends Fixture | ||
{ | ||
public const string NODE_1 = '0191d6db-ada6-7632-b6d4-56f5094b0a86'; | ||
public const string VIRUTAL_NODE_1 = '0191d6db-733f-7d51-839c-954f6243f4c0'; | ||
|
||
public function load(ObjectManager $manager): void | ||
{ | ||
$node1VirtualNodesCollection = new ArrayCollection(); | ||
$node1 = new Node( | ||
'127.0.0.1', | ||
8000, | ||
MembershipState::JOINED, | ||
new \DateTimeImmutable('2024-09-09 14:46:00'), | ||
5, | ||
true, | ||
false, | ||
'D', | ||
NodeState::UP, | ||
new \DateTimeImmutable('2024-09-09 14:50:00'), | ||
$node1VirtualNodesCollection, | ||
UuidV7::fromString(self::NODE_1) | ||
); | ||
|
||
$vm1 = new VirtualNode( | ||
sprintf('%s_1', $node1->getLabel()), | ||
0, | ||
$node1, | ||
new \DateTimeImmutable('2024-09-09 14:55:0'), | ||
true, | ||
UuidV7::fromString(self::VIRUTAL_NODE_1) | ||
); | ||
|
||
$node1VirtualNodesCollection->add($vm1); | ||
|
||
$manager->persist($vm1); | ||
$manager->persist($node1); | ||
|
||
$this->loadHistory($manager); | ||
|
||
$manager->flush(); | ||
} | ||
|
||
private function loadHistory(ObjectManager $manager) | ||
{ | ||
$h = new HistoryEvent( | ||
UuidV7::fromString('019239be-19ea-7ba7-b38d-1855553df0f7'), | ||
UuidV7::fromString(self::NODE_1), | ||
HistoryEventType::CHANGE_MEMBERSHIP, | ||
new \DateTimeImmutable('2024-09-09 16:50:00'), | ||
(string) MembershipState::JOINED->value, | ||
null, | ||
null | ||
); | ||
|
||
$manager->persist($h); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
node/tests/Application/Background/SyncMembershipControllerTest.php
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,123 @@ | ||
<?php | ||
|
||
namespace App\Tests\Application\Background; | ||
|
||
use App\Shared\Domain\Const\RingInformations; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\HistoryEvent; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\Node; | ||
use App\Shared\Infrastructure\Persistence\Doctrine\PreferenceListEntry; | ||
use App\Tests\Application\AppTestCase; | ||
use DataFixtures\Background\SyncMembershipFixtures; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
use function PHPUnit\Framework\assertCount; | ||
use function PHPUnit\Framework\assertEmpty; | ||
use function PHPUnit\Framework\assertEquals; | ||
|
||
class SyncMembershipControllerTest extends AppTestCase | ||
{ | ||
private const string URI = 'background/sync/membership/v1'; | ||
|
||
private const HISTORY_EVENT_1 = '0191d74d-a192-713f-982d-80777d8c5589'; | ||
private const NODE_2 = '0191d74b-c96e-70eb-963e-33c06382c6cd'; | ||
private const VIRTUAL_NODE_2 = '0191d74b-7782-753d-aa01-57b7223a5492'; | ||
private const JSON_DATA = [ | ||
'sourceNode' => self::NODE_2, | ||
'historyEvents' => [ | ||
[ | ||
'id' => self::HISTORY_EVENT_1, | ||
'node' => self::NODE_2, | ||
'type' => 0, | ||
'data' => '0', | ||
'eventTime' => '2024-10-09T14:56:49.086Z', | ||
], | ||
], | ||
'nodes' => [ | ||
[ | ||
'id' => self::NODE_2, | ||
'host' => 'localhost', | ||
'networkPort' => 9003, | ||
'state' => 0, | ||
'joinedAt' => '2024-09-09T14:56:49.086Z', | ||
'weight' => 1, | ||
'seed' => true, | ||
'updatedAt' => '2024-09-09T14:56:49.086Z', | ||
'label' => 'F', | ||
'virtualNodes' => [ | ||
[ | ||
'id' => self::VIRTUAL_NODE_2, | ||
'label' => 'F1', | ||
'slot' => 180, | ||
'createdAt' => '2024-09-09T14:56:49.086Z', | ||
'active' => true, | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
public function testMembershipSyncSuccess(): void | ||
{ | ||
$this->databaseTool->loadFixtures([ | ||
SyncMembershipFixtures::class, | ||
]); | ||
|
||
$this->client->followRedirects(); | ||
$this->postJson(self::URI, self::JSON_DATA); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
/** @var EntityManagerInterface $em */ | ||
$em = self::getContainer()->get(EntityManagerInterface::class); | ||
$nodes = $em->getRepository(Node::class)->findAll(); | ||
$historyEvents = $em->getRepository(HistoryEvent::class)->findAll(); | ||
$preferenceListEntries = $em->getRepository(PreferenceListEntry::class)->findAll(); | ||
|
||
self::assertCount(2, $nodes); | ||
|
||
$nodesMap = []; | ||
foreach ($nodes as $node) { | ||
$nodesMap[$node->getId()->toRfc4122()] = $node; | ||
} | ||
|
||
$node1Result = $nodesMap[SyncMembershipFixtures::NODE_1]; | ||
$node2Result = $nodesMap[self::NODE_2]; | ||
|
||
self::assertCount(1, $node1Result->getVirtualNodes()); | ||
self::assertCount(1, $node2Result->getVirtualNodes()); | ||
|
||
$virtualNode1 = $node1Result->getVirtualNodes()->first(); | ||
$virtualNode2 = $node2Result->getVirtualNodes()->first(); | ||
|
||
self::assertEquals(SyncMembershipFixtures::VIRUTAL_NODE_1, $virtualNode1->getId()->toRfc4122()); | ||
self::assertEquals(self::VIRTUAL_NODE_2, $virtualNode2->getId()->toRfc4122()); | ||
|
||
self::assertTrue($virtualNode1->isActive()); | ||
self::assertTrue($virtualNode2->isActive()); | ||
|
||
self::assertCount(2, $historyEvents); | ||
|
||
$newHistoryEventFiltered = array_filter($historyEvents, fn (HistoryEvent $historyEvent) => self::HISTORY_EVENT_1 === $historyEvent->getId()->toRfc4122()); | ||
self::assertCount(1, $newHistoryEventFiltered); | ||
|
||
/** @var HistoryEvent $newHistoryEvent */ | ||
$newHistoryEvent = current($newHistoryEventFiltered); | ||
|
||
assertEquals(self::NODE_2, $newHistoryEvent->getNode()->toRfc4122()); | ||
self::assertNotNull($newHistoryEvent->getReceivedAt()); | ||
|
||
self::assertCount(RingInformations::RING_SIZE, $preferenceListEntries); | ||
|
||
foreach ($preferenceListEntries as $preferenceEntry) { | ||
if ($preferenceEntry->getSlot() >= 0 && $preferenceEntry->getSlot() < 180) { | ||
assertEquals(SyncMembershipFixtures::NODE_1, $preferenceEntry->getOwnerId()->toRfc4122()); | ||
assertEquals(self::NODE_2, current($preferenceEntry->getCoordinatorsIds())->toRfc4122()); | ||
} else { | ||
assertEquals(self::NODE_2, $preferenceEntry->getOwnerId()->toRfc4122()); | ||
assertEquals(SyncMembershipFixtures::NODE_1, current($preferenceEntry->getCoordinatorsIds())->toRfc4122()); | ||
} | ||
assertCount(1, $preferenceEntry->getCoordinatorsIds()); | ||
assertEmpty($preferenceEntry->getOthersNodesIds()); | ||
} | ||
} | ||
} |