forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheSlamsProtection.test.php
More file actions
65 lines (56 loc) · 2.23 KB
/
CacheSlamsProtection.test.php
File metadata and controls
65 lines (56 loc) · 2.23 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
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <contact@geolim4.com>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Entities\ItemBatch;
use Phpfastcache\EventManager;
use Phpfastcache\Tests\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Cache Slams Protection');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$driverInstance = CacheManager::getInstance($defaultDriver, new ConfigurationOption([
'preventCacheSlams' => true,
'cacheSlamsTimeout' => 20
]));
if (!$testHelper->isHHVM()) {
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->assertPass('The batch has expired and returned a non-empty item with expected value: ' . $item->get());
} else {
$testHelper->assertFail('The batch has expired and returned an empty item with expected value: ' . print_r($item->get(), true));
}
/**
* Cleanup the driver
*/
$driverInstance->deleteItem($item->getKey());
} else {
$testHelper->assertSkip('Test ignored on HHVM builds due to sub-process issues with C.I.');
}
$testHelper->terminateTest();