forked from PHPSocialNetwork/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheSlamsProtection.test.php
More file actions
53 lines (44 loc) · 1.74 KB
/
CacheSlamsProtection.test.php
File metadata and controls
53 lines (44 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <[email protected]>
*/
use phpFastCache\CacheManager;
use phpFastCache\Core\Item\ExtendedCacheItemInterface;
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
use phpFastCache\Entities\ItemBatch;
use phpFastCache\EventManager;
use phpFastCache\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../src/autoload.php';
$testHelper = new TestHelper('Cache Slams Protection');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$driverInstance = CacheManager::getInstance($defaultDriver, [
'preventCacheSlams' => true,
'cacheSlamsTimeout' => 20
]);
EventManager::getInstance()->onCacheGetItemInSlamBatch(function(ExtendedCacheItemPoolInterface $itemPool, ItemBatch $driverData, $cacheSlamsSpendSeconds) use ($testHelper){
$testHelper->printText("Looping in batch for {$cacheSlamsSpendSeconds} second(s) with a batch from " . $driverData->getItemDate()->format(\DateTime::W3C));
});
$testHelper->runSubProcess('CacheSlamsProtection');
/**
* Give some time to the
* subprocess to start
* just like a concurrent
* php request
*/
usleep(mt_rand(250000, 800000));
$item = $driverInstance->getItem('TestCacheSlamsProtection');
/**
* @see CacheSlamsProtection.subprocess.php:28
*/
if($item->isHit() && $item->get() === 1337){
$testHelper->printPassText('The batch has expired and returned a non-empty item with expected value: ' . $item->get());
}else{
$testHelper->printFailText('The batch has expired and returned an empty item with expected value: ' . print_r($item->get(), true));
}
/**
* Cleanup the driver
*/
$driverInstance->deleteItem($item->getKey());
$testHelper->terminateTest();