Skip to content

Commit

Permalink
ripristinati tests
Browse files Browse the repository at this point in the history
  • Loading branch information
januabisconti committed Jan 13, 2025
1 parent 3fdaf9a commit aa8835d
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 45 deletions.
19 changes: 12 additions & 7 deletions src/Console/ProcessCacheInvalidationEventsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t

// Fetch a batch of unprocessed events
$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsUnprocessedPartitionName($shardId, $priority);
//$this->info(now()->toDateTimeString() . ' - Partition=' . $partitionCache_invalidation_events);
$events = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))
->where('processed', '=', 0)
->where('shard', '=', $shardId)
Expand All @@ -90,7 +91,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
->limit($limit)
->get()
;

//$this->info(now()->toDateTimeString() . ' - Trovati Eventi=' . count($events));
if ($events->isEmpty()) {
// No more events to process
return;
Expand Down Expand Up @@ -134,7 +135,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
}
// Fetch last invalidation times in bulk
$lastInvalidationTimes = $this->getLastInvalidationTimes(array_unique($allIdentifiers));

//$this->info(now()->toDateTimeString() . ' - $lastInvalidationTimes=' . count($lastInvalidationTimes));
foreach ($eventsByIdentifier as $key => $eventsGroup) {
// Extract type and identifier
[$type, $identifier] = explode(':', $key, 2);
Expand All @@ -159,7 +160,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
$lastInvalidationTimesSubset = array_intersect_key($lastInvalidationTimes, array_flip($identifiersToCheck));

$shouldInvalidate = $this->shouldInvalidateMultiple($identifiersToCheck, $lastInvalidationTimesSubset, $invalidationWindow);

//$this->info(now()->toDateTimeString() . ' - $shouldInvalidate=' . $shouldInvalidate);
if ($shouldInvalidate) {
// Proceed to invalidate
$latestEvent = $eventsGroup->last();
Expand All @@ -175,7 +176,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t

// Update last invalidation times for all identifiers
$this->updateLastInvalidationTimes($identifiersToCheck);

//$this->info(now()->toDateTimeString() . ' - updateLastInvalidationTimes=OK');
// Mark all events in the group as processed
foreach ($eventsGroup as $event) {
$eventsToUpdate[] = ['id' => $event->id, 'event_time' => $event->event_time, 'partition_key' => $event->partition_key];
Expand Down Expand Up @@ -315,8 +316,8 @@ protected function updateLastInvalidationTimes(array $identifiers): void
;

// Riattiva i controlli
DB::statement('SET UNIQUE_CHECKS=1;');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
DB::statement('SET UNIQUE_CHECKS=1;');
}
}

Expand Down Expand Up @@ -369,6 +370,8 @@ protected function processBatch(array $batchIdentifiers, array $eventsToUpdate,
$keys = array_unique($keys);
$tags = array_unique($tags);

//$this->info(now()->toDateTimeString() . ' - Invalido ' . count($keys) . ' chiavi e ' . count($tags) . ' tag');

// Invalidate cache for keys
if (!empty($keys)) {
$this->invalidateKeys($keys);
Expand Down Expand Up @@ -398,11 +401,13 @@ protected function processBatch(array $batchIdentifiers, array $eventsToUpdate,
->update(['processed' => 1, 'updated_at' => now()])
;
// Riattiva i controlli
DB::statement('SET UNIQUE_CHECKS=1;');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
DB::statement('SET UNIQUE_CHECKS=1;');
// Commit transaction
//DB::commit();
$updatedOk = true;

//$this->info(now()->toDateTimeString() . ' - cache_invalidation_events UPDATED Ok - count( ' . count($eventsToUpdate) . ' )');
} catch (\Throwable $e) {
// Rollback transaction on error
//DB::rollBack();
Expand Down Expand Up @@ -505,7 +510,7 @@ public function handle(): void
}
*/
$lockValue = $this->helper->acquireShardLock($shardId, $priority, $lockTimeout, $connection_name);

//$this->info(now()->toDateTimeString() . ' Starting shard=' . $shardId . ' priority=' . $priority . ' lockValue=' . $lockValue);
if (!$lockValue) {
return;
}
Expand Down
220 changes: 186 additions & 34 deletions tests/Unit/ProcessCacheInvalidationEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Padosoft\SuperCacheInvalidate\Test\Unit;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
use Mockery;
use Padosoft\SuperCacheInvalidate\Helpers\SuperCacheInvalidationHelper;

Expand All @@ -18,21 +20,25 @@ protected function setUp(): void
$this->helper = new SuperCacheInvalidationHelper();
}

public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
public function test_process_events_with_associated_identifiers_within_window(): void
{
// Mock data
$now = now();
$events = collect([
(object)[
'id' => 1,
'type' => 'key',
'identifier' => 'article_ID:7',
'reason' => 'Article 7 removed',
'connection_name' => 'default',
'shard' => 1,
'priority' => 1
'shard' => 0,
'priority' => 0,
'event_time' => $now,
'partition_key' => 0,
],
]);

/*
$associations = collect([
(object)[
'event_id' => 1,
Expand All @@ -41,55 +47,161 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
'connection_name' => 'default',
],
]);
*/

// Mock DB queries
DB::shouldReceive('table->where->where->where->where->where->orderBy->limit->get')
Config::set('super_cache_invalidate.invalidation_window', 1);

// Recupera il valore di configurazione per verificare che sia stato impostato
$value = config('super_cache_invalidate.invalidation_window');

// Asserzione
$this->assertEquals(1, $value);

$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsUnprocessedPartitionName(0, 0);


DB::shouldReceive('raw')
->once()
->with("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})")
->andReturn("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})")
;

DB::shouldReceive('table')
->once()
->with("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})")
->andReturnSelf()
;

DB::shouldReceive('where')
->once()
->with('processed', '=', 0)
->andReturnSelf()
;

DB::shouldReceive('where')
->once()
->with('shard', '=', 0)
->andReturnSelf()
;

DB::shouldReceive('where')
->once()
->with('priority', '=', 0)
->andReturnSelf()
;

DB::shouldReceive('where')
->once()
->with('event_time', '<', Mockery::any())
->andReturnSelf()
;

DB::shouldReceive('where')
->once()
->with('connection_name', '=', 'default')
->andReturnSelf()
;

DB::shouldReceive('orderBy')
->once()
->with('event_time')
->andReturnSelf()
;

DB::shouldReceive('limit')
->once()
->with(1)
->andReturnSelf()
;

DB::shouldReceive('get')
->once()
->andReturn($events)
;
//DB::shouldReceive('table->raw->where->where->where->where->where->orderBy->limit->get')
// ->andReturn($events)
//;

/*
DB::shouldReceive('table->whereIn->get->groupBy')
->andReturn($associations)
;
*/

// Mock last invalidation times
DB::shouldReceive('select')
->andReturn([
(object)[
'identifier_type' => 'key',
'identifier' => 'article_ID:7',
'last_invalidated' => Carbon::now()->subSeconds(120)->toDateTimeString(),
],
(object)[
'identifier_type' => 'tag',
'identifier' => 'plp:sport',
'last_invalidated' => Carbon::now()->subSeconds(180)->toDateTimeString(),
],
]);
(object)[
'identifier_type' => 'key',
'identifier' => 'article_ID:7',
'last_invalidated' => Carbon::now()->subSeconds(120)->toDateTimeString(),
],
//(object)[
// 'identifier_type' => 'tag',
// 'identifier' => 'plp:sport',
// 'last_invalidated' => Carbon::now()->subSeconds(180)->toDateTimeString(),
//],
])
;
// Mock DB::statement
DB::shouldReceive('statement')
->once()
->with('SET FOREIGN_KEY_CHECKS=0;')
->andReturn(true);


DB::shouldReceive('statement')
->once()
->with('SET UNIQUE_CHECKS=0;')
->andReturn(true);


// Mock update or insert
DB::shouldReceive('table->updateOrInsert')->twice(); //1 per la chiave e 1 per il tag
DB::shouldReceive('table')
->once()
->with('cache_invalidation_timestamps')
->andReturnSelf();

DB::shouldReceive('updateOrInsert')
->once()
->andReturn(true);

DB::shouldReceive('statement')
->once()
->with('SET FOREIGN_KEY_CHECKS=1;')
->andReturn(true);


DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('statement')
->once()
->with('SET UNIQUE_CHECKS=1;')
->andReturn(true);
//DB::shouldReceive('beginTransaction')->once();

// CHIAVE
// Mock Cache
Cache::shouldReceive('store')
->with('redis-store-1') // Nome dello store
->once()
->andReturn(Mockery::mock(\Illuminate\Contracts\Cache\Repository::class, function ($mock) {
// Mockiamo il comportamento del repository cache
$mock->shouldReceive('forget')
->withArgs(function ($key) {
// controllo che la chiave sia proprio quella
$this->assertEquals('article_ID:7', $key);
return true; // Indica che l'argomento è accettabile
})
->once()
->andReturn(true);
}));
->with('redis-store-1') // Nome dello store
->once()
->andReturn(Mockery::mock(\Illuminate\Contracts\Cache\Repository::class, function ($mock) {
// Mockiamo il comportamento del repository cache
$mock->shouldReceive('forget')
->withArgs(function ($key) {
// controllo che la chiave sia proprio quella
$this->assertEquals('article_ID:7', $key);

return true; // Indica che l'argomento è accettabile
})
->once()
->andReturn(true)
;
}))
;


// TAG
// Mock Cache
/*
Cache::shouldReceive('store')
->with('redis-store-1') // Nome dello store
->once()
Expand All @@ -108,12 +220,52 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
->andReturn(true);
}));
}));

*/
// Mock event update
DB::shouldReceive('table->whereIn->update')->once();

DB::shouldReceive('commit')->once();
// Mock DB::statement
DB::shouldReceive('statement')
->atLeast()->once()
->with('SET FOREIGN_KEY_CHECKS=0;')
->andReturn(true);

DB::shouldReceive('statement')
->atLeast()->once()
->with('SET UNIQUE_CHECKS=0;')
->andReturn(true);

// Mock the DB facade
DB::shouldReceive('table')
->once()
->with('cache_invalidation_events')
->andReturnSelf();

DB::shouldReceive('whereIn')
->once()
->andReturnSelf();

DB::shouldReceive('whereIn')
->once()
->andReturnSelf();

DB::shouldReceive('update')
->once()
->andReturn(1); // Simulate the number of rows updated


// Mock DB::statement
DB::shouldReceive('statement')
->atLeast()->once()
->with('SET FOREIGN_KEY_CHECKS=1;')
->andReturn(true);

DB::shouldReceive('statement')
->atLeast()->once()
->with('SET UNIQUE_CHECKS=1;')
->andReturn(true);

//DB::shouldReceive('commit')->once();
Redis::connection('default')->del('shard_lock:0_0');
// Run the command
// Run the command
$this->artisan('supercache:process-invalidation', [
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/PruneCacheInvalidationDataTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Padosoft\SuperCacheInvalidate\Test\Unit;

use Illuminate\Support\Facades\DB;
use Padosoft\SuperCacheInvalidate\Console\PruneCacheInvalidationDataCommand;

Expand Down
Loading

0 comments on commit aa8835d

Please sign in to comment.