CacheTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Core;
  20. use App\Core\Cache;
  21. use App\Util\Common;
  22. use ReflectionClass;
  23. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  24. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  25. class CacheTest extends KernelTestCase
  26. {
  27. private function doTest(array $adapters, $result_pool, $throws = null, $recompute = \INF)
  28. {
  29. static::bootKernel();
  30. // Setup Common::config to have the values in $conf
  31. $conf = ['cache' => ['adapters' => $adapters, 'early_recompute' => $recompute]];
  32. $cb = $this->createMock(ContainerBagInterface::class);
  33. static::assertTrue($cb instanceof ContainerBagInterface);
  34. $cb->method('get')
  35. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  36. Common::setupConfig($cb);
  37. if ($throws != null) {
  38. $this->expectException($throws);
  39. }
  40. Cache::setupCache();
  41. $reflector = new ReflectionClass('App\Core\Cache');
  42. $pools = $reflector->getStaticPropertyValue('pools');
  43. foreach ($result_pool as $name => $type) {
  44. static::assertInstanceOf($type, $pools[$name]);
  45. }
  46. }
  47. public function testConfigurationParsingSingle()
  48. {
  49. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  50. }
  51. public function testConfigurationParsingCluster1()
  52. {
  53. self::doTest(['default' => 'redis://redis;redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], \App\Util\Exception\ConfigurationException::class);
  54. }
  55. // /**
  56. // * This requires extra server configuration, but the code was tested
  57. // * manually and works, so it'll be excluded from automatic tests, for now, at least
  58. // */
  59. // public function testConfigurationParsingCluster2()
  60. // {
  61. // self::doTest(['default' => 'redis://redis:6379;redis://redis:6379'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  62. // }
  63. public function testConfigurationParsingFallBack()
  64. {
  65. self::doTest(['default' => 'redis://redis,filesystem'], ['default' => \Symfony\Component\Cache\Adapter\ChainAdapter::class]);
  66. }
  67. public function testConfigurationParsingMultiple()
  68. {
  69. self::doTest(['default' => 'redis://redis', 'file' => 'filesystem://test'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class, 'file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  70. }
  71. public function testGeneralImplementation()
  72. {
  73. // Need a connection to run the tests
  74. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  75. static::assertSame('value', Cache::get('test', fn ($i) => 'value'));
  76. Cache::set('test', 'other_value');
  77. static::assertSame('other_value', Cache::get('test', fn ($i) => 'value'));
  78. static::assertTrue(Cache::delete('test'));
  79. }
  80. private function _testRedis($recompute = \INF)
  81. {
  82. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], throws: null, recompute: $recompute);
  83. // Redis supports lists directly, uses different implementation
  84. $key = 'test' . time();
  85. static::assertSame([], Cache::getList($key . '0', fn ($i) => []));
  86. static::assertSame(['foo'], Cache::getList($key . '1', fn ($i) => ['foo']));
  87. static::assertSame(['foo', 'bar'], Cache::getList($key, fn ($i) => ['foo', 'bar']));
  88. static::assertSame(['foo', 'bar'], Cache::getList($key, function () { $this->assertFalse('should not be called'); })); // Repeat to test no recompute lrange
  89. Cache::pushList($key, 'quux');
  90. static::assertSame(['quux', 'foo', 'bar'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }));
  91. Cache::pushList($key, 'foobar', max_count: 2);
  92. static::assertSame(['foobar', 'quux'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }));
  93. static::assertTrue(Cache::deleteList($key));
  94. }
  95. public function testRedisImplementation()
  96. {
  97. $this->_testRedis();
  98. }
  99. public function testRedisImplementationNoEarlyRecompute()
  100. {
  101. $this->_testRedis(null);
  102. }
  103. public function testNonRedisImplementation()
  104. {
  105. self::doTest(['file' => 'filesystem://test'], ['file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  106. $key = 'test' . time();
  107. static::assertSame([], Cache::getList($key . '0', fn ($i) => [], pool: 'file'));
  108. static::assertSame(['foo'], Cache::getList($key . '1', fn ($i) => ['foo'], pool: 'file'));
  109. static::assertSame(['foo', 'bar'], Cache::getList($key, fn ($i) => ['foo', 'bar'], pool: 'file'));
  110. static::assertSame(['foo', 'bar'], Cache::getList($key, function () { $this->assertFalse('should not be called'); }, pool: 'file')); // Repeat to test no recompute lrange
  111. Cache::pushList($key, 'quux', pool: 'file');
  112. static::assertSame(['quux', 'foo', 'bar'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }, pool: 'file'));
  113. Cache::pushList($key, 'foobar', max_count: 2, pool: 'file');
  114. static::assertSame(['foobar', 'quux'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }, pool: 'file'));
  115. static::assertTrue(Cache::deleteList($key, pool: 'file'));
  116. }
  117. }