Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84ada0c

Browse files
Merge branch '4.1'
* 4.1: [FrameworkBundle] decouple some cache-warmer's test from internal details bug #27405 [Cache] TagAwareAdapter should not corrupt memcached connection in ascii mode Remove released semaphore
2 parents 1b2bd8f + 28af1a6 commit 84ada0c

File tree

6 files changed

+96
-33
lines changed

6 files changed

+96
-33
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer;
1515
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1616
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17+
use Symfony\Component\Cache\Adapter\NullAdapter;
18+
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
1719
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
1820
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
1921
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
@@ -41,12 +43,10 @@ public function testWarmUp()
4143

4244
$this->assertFileExists($file);
4345

44-
$values = require $file;
46+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
4547

46-
$this->assertInternalType('array', $values);
47-
$this->assertCount(2, $values);
48-
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person', $values);
49-
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author', $values);
48+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
49+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
5050

5151
$values = $fallbackPool->getValues();
5252

@@ -72,11 +72,6 @@ public function testWarmUpWithoutLoader()
7272

7373
$this->assertFileExists($file);
7474

75-
$values = require $file;
76-
77-
$this->assertInternalType('array', $values);
78-
$this->assertCount(0, $values);
79-
8075
$values = $fallbackPool->getValues();
8176

8277
$this->assertInternalType('array', $values);

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ValidatorCacheWarmer;
1515
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1616
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17+
use Symfony\Component\Cache\Adapter\NullAdapter;
18+
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
19+
use Symfony\Component\Validator\Mapping\ClassMetadata;
1720
use Symfony\Component\Validator\ValidatorBuilder;
1821

1922
class ValidatorCacheWarmerTest extends TestCase
@@ -36,12 +39,10 @@ public function testWarmUp()
3639

3740
$this->assertFileExists($file);
3841

39-
$values = require $file;
42+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
4043

41-
$this->assertInternalType('array', $values);
42-
$this->assertCount(2, $values);
43-
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person', $values);
44-
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author', $values);
44+
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
45+
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
4546

4647
$values = $fallbackPool->getValues();
4748

@@ -67,14 +68,12 @@ public function testWarmUpWithAnnotations()
6768

6869
$this->assertFileExists($file);
6970

70-
$values = require $file;
71+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
7172

72-
$this->assertInternalType('array', $values);
73-
$this->assertCount(1, $values);
74-
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);
73+
$item = $arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category');
74+
$this->assertTrue($item->isHit());
7575

76-
// Simple check to make sure that at least one constraint is actually cached, in this case the "id" property Type.
77-
$this->assertContains('"int"', $values['Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category']);
76+
$this->assertInstanceOf(ClassMetadata::class, $item->get());
7877

7978
$values = $fallbackPool->getValues();
8079

@@ -98,11 +97,6 @@ public function testWarmUpWithoutLoader()
9897

9998
$this->assertFileExists($file);
10099

101-
$values = require $file;
102-
103-
$this->assertInternalType('array', $values);
104-
$this->assertCount(0, $values);
105-
106100
$values = $fallbackPool->getValues();
107101

108102
$this->assertInternalType('array', $values);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Simple;
13+
14+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
15+
use Symfony\Component\Cache\Simple\MemcachedCache;
16+
17+
class MemcachedCacheTextModeTest extends MemcachedCacheTest
18+
{
19+
public function createSimpleCache($defaultLifetime = 0)
20+
{
21+
$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
22+
23+
return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
24+
}
25+
}

src/Symfony/Component/Cache/Traits/MemcachedTrait.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ protected function doSave(array $values, $lifetime)
198198
$lifetime += time();
199199
}
200200

201-
return $this->checkResultCode($this->getClient()->setMulti($values, $lifetime));
201+
$encodedValues = array();
202+
foreach ($values as $key => $value) {
203+
$encodedValues[rawurlencode($key)] = $value;
204+
}
205+
206+
return $this->checkResultCode($this->getClient()->setMulti($encodedValues, $lifetime));
202207
}
203208

204209
/**
@@ -208,7 +213,16 @@ protected function doFetch(array $ids)
208213
{
209214
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
210215
try {
211-
return $this->checkResultCode($this->getClient()->getMulti($ids));
216+
$encodedIds = array_map('rawurlencode', $ids);
217+
218+
$encodedResult = $this->checkResultCode($this->getClient()->getMulti($encodedIds));
219+
220+
$result = array();
221+
foreach ($encodedResult as $key => $value) {
222+
$result[rawurldecode($key)] = $value;
223+
}
224+
225+
return $result;
212226
} catch (\Error $e) {
213227
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
214228
} finally {
@@ -221,7 +235,7 @@ protected function doFetch(array $ids)
221235
*/
222236
protected function doHave($id)
223237
{
224-
return false !== $this->getClient()->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
238+
return false !== $this->getClient()->get(rawurlencode($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
225239
}
226240

227241
/**
@@ -230,7 +244,8 @@ protected function doHave($id)
230244
protected function doDelete(array $ids)
231245
{
232246
$ok = true;
233-
foreach ($this->checkResultCode($this->getClient()->deleteMulti($ids)) as $result) {
247+
$encodedIds = array_map('rawurlencode', $ids);
248+
foreach ($this->checkResultCode($this->getClient()->deleteMulti($encodedIds)) as $result) {
234249
if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
235250
$ok = false;
236251
}

src/Symfony/Component/Lock/Store/SemaphoreStore.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ private function lock(Key $key, $blocking)
6464
return;
6565
}
6666

67-
$resource = sem_get(crc32($key));
68-
$acquired = sem_acquire($resource, !$blocking);
67+
$keyId = crc32($key);
68+
$resource = sem_get($keyId);
69+
$acquired = @sem_acquire($resource, !$blocking);
70+
71+
while ($blocking && !$acquired) {
72+
$resource = sem_get($keyId);
73+
$acquired = @sem_acquire($resource);
74+
}
6975

7076
if (!$acquired) {
7177
throw new LockConflictedException();
@@ -86,7 +92,7 @@ public function delete(Key $key)
8692

8793
$resource = $key->getState(__CLASS__);
8894

89-
sem_release($resource);
95+
sem_remove($resource);
9096

9197
$key->removeState(__CLASS__);
9298
}

src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Key;
1415
use Symfony\Component\Lock\Store\SemaphoreStore;
1516

1617
/**
@@ -29,4 +30,31 @@ protected function getStore()
2930
{
3031
return new SemaphoreStore();
3132
}
33+
34+
public function testResourceRemoval()
35+
{
36+
$initialCount = $this->getOpenedSemaphores();
37+
$store = new SemaphoreStore();
38+
$key = new Key(uniqid(__METHOD__, true));
39+
$store->waitAndSave($key);
40+
41+
$this->assertGreaterThan($initialCount, $this->getOpenedSemaphores(), 'Semaphores should have been created');
42+
43+
$store->delete($key);
44+
$this->assertEquals($initialCount, $this->getOpenedSemaphores(), 'All semaphores should be removed');
45+
}
46+
47+
private function getOpenedSemaphores()
48+
{
49+
$lines = explode(PHP_EOL, trim(`ipcs -su`));
50+
if ('------ Semaphore Status --------' !== $lines[0]) {
51+
throw new \Exception('Failed to extract list of opend semaphores. Expect a Semaphore status, got '.implode(PHP_EOL, $lines));
52+
}
53+
list($key, $value) = explode(' = ', $lines[1]);
54+
if ('used arrays' !== $key) {
55+
throw new \Exception('Failed to extract list of opend semaphores. Expect a used arrays key, got '.implode(PHP_EOL, $lines));
56+
}
57+
58+
return (int) $value;
59+
}
3260
}

0 commit comments

Comments
 (0)