forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClusterMasterSlaveReplication.test.php
More file actions
90 lines (78 loc) · 3.68 KB
/
ClusterMasterSlaveReplication.test.php
File metadata and controls
90 lines (78 loc) · 3.68 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
ClusterFullReplication.test.php<?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\Cluster\AggregatorInterface;
use Phpfastcache\Cluster\ClusterAggregator;
use Phpfastcache\Cluster\ItemAbstract;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Drivers\Failfiles\Driver as FailFilesDriver;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Tests\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/mock/Autoload.php';
$testHelper = new TestHelper('Master/Slave Replication Cluster');
CacheManager::addCustomDriver('Failfiles', FailFilesDriver::class);
$clusterAggregator = new ClusterAggregator('test_10');
$unwantedPool = CacheManager::getInstance('Redis');
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Failfiles'));
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Sqlite'));
$clusterAggregator->aggregateDriver($unwantedPool);
try {
$cluster = $clusterAggregator->getCluster(AggregatorInterface::STRATEGY_MASTER_SLAVE);
$testHelper->assertFail('The Master/Slave cluster did not thrown an exception with more than 2 pools aggregated.');
} catch (PhpfastcacheInvalidArgumentException $e) {
$testHelper->assertPass('The Master/Slave cluster thrown an exception with more than 2 pools aggregated.');
}
$clusterAggregator->disaggregateDriver($unwantedPool);
$cluster = $clusterAggregator->getCluster(AggregatorInterface::STRATEGY_MASTER_SLAVE);
$testPasses = false;
$cluster->getEventManager()->onCacheReplicationSlaveFallback(
static function (ExtendedCacheItemPoolInterface $pool, string $actionName) use (&$testPasses) {
if ($actionName === 'getItem') {
$testPasses = true;
}
}
);
$cacheItem = $cluster->getItem('test-test');
if ($testPasses && $cacheItem instanceof ItemAbstract) {
$testHelper->assertPass('The Master/Slave cluster successfully switched to slave cluster after backend I/O error.');
} else {
$testHelper->assertFail('The Master/Slave cluster failed to switch to slave cluster after backend I/O error.');
}
unset($unwantedPool, $cacheItem, $testPasses, $cluster, $clusterAggregator);
CacheManager::clearInstances();
$clusterAggregator = new ClusterAggregator('test_20');
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Redis'));
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Files'));
$cluster = $clusterAggregator->getCluster(AggregatorInterface::STRATEGY_MASTER_SLAVE);
$cluster->clear();
$cacheKey = 'cache_' . \bin2hex(\random_bytes(12));
$cacheValue = 'cache_' . \random_int(1000, 999999);
$cacheItem = $cluster->getItem($cacheKey);
$cacheItem->set($cacheValue);
$cacheItem->expiresAfter(600);
if ($cluster->save($cacheItem)) {
$testHelper->assertPass('The Master/Slave cluster successfully saved an item.');
} else {
$testHelper->assertFail('The Master/Slave cluster failed to save an item.');
}
unset($clusterAggregator, $cluster, $cacheItem);
CacheManager::clearInstances();
$clusterAggregator = new ClusterAggregator('test_20');
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Redis'));
$clusterAggregator->aggregateDriver(CacheManager::getInstance('Files'));
$cluster = $clusterAggregator->getCluster(AggregatorInterface::STRATEGY_MASTER_SLAVE);
$cluster->clear();
$testHelper->runCRUDTests($cluster);
$testHelper->terminateTest();