Skip to content

Commit

Permalink
redis repository test
Browse files Browse the repository at this point in the history
  • Loading branch information
leventcz committed May 19, 2024
1 parent 99652f4 commit 4ec302c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 55 deletions.
45 changes: 0 additions & 45 deletions tests/Pest.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/TestCase.php

This file was deleted.

127 changes: 127 additions & 0 deletions tests/Units/RedisRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

use Illuminate\Contracts\Redis\Factory as RedisFactory;
use Illuminate\Redis\Connections\Connection;
use Leventcz\Top\Data\CacheSummary;
use Leventcz\Top\Data\DatabaseSummary;
use Leventcz\Top\Data\EventCounter;
use Leventcz\Top\Data\HandledRequest;
use Leventcz\Top\Data\RequestSummary;
use Leventcz\Top\Data\RouteCollection;
use Leventcz\Top\Repositories\RedisRepository;

beforeEach(function () {
$this->redisFactory = Mockery::mock(RedisFactory::class);
$this->connection = Mockery::mock(Connection::class);
$this->redisFactory
->shouldReceive('connection')
->andReturn($this->connection);
$this->repository = new RedisRepository($this->redisFactory);
});

afterEach(function () {
Mockery::close();
});

it('saves handled request and events to redis', function () {
$request = new HandledRequest('GET', '/test', 100, 200, 1622505600);
$eventCounter = new EventCounter(['some-event' => 5, 'another-event' => 10]);
$pipe = Mockery::spy();

$this
->connection
->shouldReceive('pipeline')
->once()
->with(Mockery::on(function ($callback) use ($pipe) {
$callback($pipe);
return true;
}))
->andReturnNull();

$this->repository->save($request, $eventCounter);

$key = "top-requests:$request->timestamp";
$routeKey = "$request->method:$request->uri:data";

foreach ($eventCounter->get() as $event => $times) {
$pipe
->shouldHaveReceived('hIncrBy')
->with($key, "$routeKey:$event", $times)
->once();
}

$pipe
->shouldHaveReceived('hIncrBy')
->with($key, "$routeKey:hits", 1)
->once();

$pipe
->shouldHaveReceived('hIncrBy')
->with($key, "$routeKey:memory", $request->memory)
->once();

$pipe
->shouldHaveReceived('hIncrBy')
->with($key, "$routeKey:duration", $request->duration)
->once();

$pipe
->shouldHaveReceived('expire')
->with($key, 10)
->once();
});

it('fetches request summary from redis', function () {
$this
->connection
->shouldReceive('eval')
->once()
->andReturn(json_encode(['averageRequestPerSecond' => 1, 'averageMemoryUsage' => 2, 'averageDuration' => 3]));

$summary = $this->repository->getRequestSummary();

expect($summary)->toBeInstanceOf(RequestSummary::class);
});

it('fetches database summary from redis', function () {
$this
->connection
->shouldReceive('eval')
->once()
->andReturn(json_encode(['averageQueryPerSecond' => 1, 'averageQueryDuration' => 2]));

$summary = $this->repository->getDatabaseSummary();

expect($summary)->toBeInstanceOf(DatabaseSummary::class);
});

it('fetches cache summary from redis', function () {
$this
->connection
->shouldReceive('eval')
->once()
->andReturn(json_encode([
'averageHitPerSecond' => 1, 'averageMissPerSecond' => 2, 'averageWritePerSecond' => 3
]));

$summary = $this->repository->getCacheSummary();

expect($summary)->toBeInstanceOf(CacheSummary::class);
});

it('fetches top routes from redis', function () {
$this
->connection
->shouldReceive('eval')
->once()
->andReturn(json_encode([
[
'uri' => '', 'method' => '', 'averageRequestPerSecond' => 1, 'averageMemoryUsage' => 1,
'averageDuration' => 1
]
]));

$routes = $this->repository->getTopRoutes();

expect($routes)->toBeInstanceOf(RouteCollection::class);
});

0 comments on commit 4ec302c

Please sign in to comment.