PHPUnit and CacheManager::getInstance() errors
Closed this issue · 24 comments
I'm running a series of URL tests (simply asserting that the response is successful). After the fourth page request, I get the error (which causes the test to fail):
[Files] Calling many times CacheManager::getInstance() for already
instanced drivers is a bad practice and have a significant impact on performances.Any thoughts on getInstance calls that are happening behind the scenes? My test setup simply:
$client = self::createClient();
$client->request('GET', $url);Huh ? How does this code calls pfc on behind ?
My only guess is that you have a Symfony service re-declaration oO.
This method is exactly here to store the cache instances...
phpfastcache-bundle/Service/Cache.php
Line 85 in 2fd3b46
It looks like it might be related to shared processes within PhpUnit. For instance, this fails (after the fourth call):
$client = self::createClient();
$client->request('GET', $url);This succeeds (indefinite amount of calls)
$client = self::createClient();
$client->insulate();
$client->request('GET', $url);Does the bundle automatically call CacheManager::getInstance()? I'm not calling it directly.
It is declared via a service:
Maybe I have to make this property static to avoid cache instance re-declaration:
phpfastcache-bundle/Service/Cache.php
Line 44 in 2fd3b46
I'm not sure. It appears PhpUnit is reusing / sharing processes from previous requests. That may not be an issue in a normal request/response scenario.
Can you try to make this property static and tell me if it change something ?
(Don't forgot to replace $this->cacheInstances occurrences by self::$cacheInstances
I might need to poke at this a bit. Swapping out $cacheInstances with a static reference threw an error: Undefined index: filecache
Huh ? Show me your modified files please :)
I'm pretty sure that moving this property to a static context will solve the issue caused by shared processes.
It should looks like this:
https://gist.github.com/Geolim4/3bd649b922ce873af96e37c41df59189
That worked. I must have missed a static conversion in there somewhere. With this update, I don't need to insulate the PHPUnit processes ($this->client->insulate();). Thanks!
Also I will create a class factory to avoid to create multiple instance of this service:
https://symfony.com/doc/current/service_container/factories.html
Thanks you.
Makes sense!
Try this and tell me if it's working:
https://gist.github.com/Geolim4/1fd919db4411db76e016f42c596c69e3
I just tested it and it's looking ok on my side.
If it's ok I'll push immediately on the v 1.x, 2.x then publish the releases.
Cheers,
Georges
Sorry, I'll run the patch tonight, and let you know.
I got a fatal error: Call to protected phpFastCache\Bundle\Service\Cache::__construct() from context 'appDevDebugProjectContainer'
You have something wrong in your tests man. It should not instantiate this class directly !
Not sure what you mean. I ran the patch, opened up the app, and got that error. I can poke at it. Maybe cache needs to be cleared, etc.. I didn't run any tests.
Yeah I suggest you to clear all the caches. The factory has been configured in the services.yml and the singleton is well-implemented. Tested on my side and ran a random PhpUnit test and got no error :(
Hello @bjunc,
I’m closing this issue for now because of (inactivity / outdated code / …).
You can always reopen it though! :)
Please (update the issue / add comment / clarify …).
Regards,
Georges.L
I have a similar problem with PHPUnit. I solved the throwing of this exception
Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice...
by configuring the processIsolation="true" property in my configuration file for PHPUnit (`phpunit.xml.dist'):
<phpunit bootstrap="./tests/bootstrap.php" processIsolation="true" verbose="true" strict="false" colors="true"> ...
Hello :)
2.0.1 has been released:
https://github.com/PHPSocialNetwork/phpfastcache-bundle/releases/tag/2.0.1
This release fixes your issue @bjunc
Also thanks for the tips @ColonelBlimp
Cheers,
Georges