PoolTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yf
  5. * Date: 2019-01-06
  6. * Time: 22:47
  7. */
  8. namespace EasySwoole\Pool\Tests;
  9. use EasySwoole\Pool\Config;
  10. use EasySwoole\Pool\MagicPool;
  11. use EasySwoole\Pool\Manager;
  12. use EasySwoole\Pool\Tests\Pool;
  13. use PHPUnit\Framework\TestCase;
  14. class PoolTest extends TestCase
  15. {
  16. function __construct(?string $name = null, array $data = [], string $dataName = '')
  17. {
  18. //cli下关闭pool的自动定时检查
  19. parent::__construct($name, $data, $dataName);
  20. }
  21. function testNormalClass()
  22. {
  23. $manager = Manager::getInstance()->register(new Pool(new Config()), 'test');
  24. /**
  25. * @var $obj Pool
  26. */
  27. $obj = $manager->get('test');
  28. $this->assertEquals(PoolObject::class, $obj->getObj()->get());
  29. }
  30. function testNormalClass2()
  31. {
  32. $manager = Manager::getInstance()->register(new MagicPool(function () {
  33. return new PoolObject();
  34. }, new Config()), 'test');
  35. $pool = $manager->get('test');
  36. $obj = $pool->getObj();
  37. $this->assertEquals(PoolObject::class, $obj->get());
  38. $this->assertEquals(true, $pool->recycleObj($obj));
  39. /**
  40. * @var $obj PoolObject
  41. */
  42. $obj = $pool->getObj();
  43. $hash1 = $obj->__objHash;
  44. $this->assertEquals(PoolObject::class, $obj->get());
  45. $pool->recycleObj($obj);
  46. $obj = $pool->getObj();
  47. $hash2 = $obj->__objHash;
  48. $pool->recycleObj($obj);
  49. $this->assertEquals($pool->status()['created'], 1);
  50. $this->assertEquals($hash1, $hash2);
  51. $pool->invoke(function (PoolObject $object) {
  52. $this->assertEquals(PoolObject::class, $object->get());
  53. });
  54. }
  55. function testUnsetObj(){
  56. $manager = Manager::getInstance()->register(new MagicPool(function () {
  57. return new PoolObject(true);
  58. }, new Config()), 'test');
  59. $pool = $manager->get('test');
  60. $obj = $pool->getObj();
  61. $hash = $obj->__objHash;
  62. $this->assertEquals(false,$pool->isInPool($obj));
  63. $pool->recycleObj($obj);
  64. $this->assertEquals(true,$pool->isInPool($obj));
  65. $manager = Manager::getInstance()->register(new MagicPool(function () {
  66. return new PoolObject(false);
  67. }, new Config()), 'test2');
  68. $pool = $manager->get('test2');
  69. $obj = $pool->getObj();
  70. $this->assertEquals(null,$obj);
  71. $status = $pool->status();
  72. $this->assertEquals(0,$status['created']);
  73. $this->assertEquals(0,$status['inuse']);
  74. }
  75. }