Ensured events triggered as a result of a new visit are never skipped

This commit is contained in:
Alejandro Celaya 2021-04-08 14:09:26 +02:00
parent 86230d9bf3
commit 7b4456e73f
6 changed files with 17 additions and 11 deletions

3
.gitignore vendored
View File

@ -6,8 +6,7 @@ composer.phar
vendor/
data/database.sqlite
data/shlink-tests.db
data/GeoLite2-City.mmdb
data/GeoLite2-City.mmdb.*
data/GeoLite2-City.*
docs/swagger-ui*
docs/mercure.html
docker-compose.override.yml

View File

@ -22,6 +22,7 @@ use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;
class GetVisitsCommandTest extends TestCase

View File

@ -52,6 +52,12 @@ class LocateVisit
return;
}
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void
{
if (! $this->dbUpdater->databaseFileExists()) {
$this->logger->warning('Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.', [
'visitId' => $visitId,
@ -59,12 +65,6 @@ class LocateVisit
return;
}
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void
{
$isLocatable = $originalIpAddress !== null || $visit->isLocatable();
$addr = $originalIpAddress ?? $visit->getRemoteAddr();

View File

@ -26,11 +26,13 @@ class UpdateGeoLiteDb
$beforeDownload = fn (bool $olderDbExists) => $this->logger->notice(
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
);
$handleProgress = function (int $total, int $downloaded, bool $olderDbExists): void {
if ($total > $downloaded) {
$messageLogged = false;
$handleProgress = function (int $total, int $downloaded, bool $olderDbExists) use (&$messageLogged): void {
if ($messageLogged || $total > $downloaded) {
return;
}
$messageLogged = true;
$this->logger->notice(sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading'));
};

View File

@ -97,7 +97,7 @@ class LocateVisitTest extends TestCase
$this->em->flush()->shouldNotHaveBeenCalled();
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
$logWarning->shouldHaveBeenCalled();
$dispatch->shouldNotHaveBeenCalled();
$dispatch->shouldHaveBeenCalledOnce();
}
/** @test */

View File

@ -84,6 +84,10 @@ class UpdateGeoLiteDbTest extends TestCase
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
function (array $args) use ($total, $downloaded, $oldDbExists): void {
[, $secondCallback] = $args;
// Invoke several times to ensure the log is printed only once
$secondCallback($total, $downloaded, $oldDbExists);
$secondCallback($total, $downloaded, $oldDbExists);
$secondCallback($total, $downloaded, $oldDbExists);
},
);