DBLockingProviderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Lock;
  22. use OCP\AppFramework\Utility\ITimeFactory;
  23. use OCP\Lock\ILockingProvider;
  24. use Psr\Log\LoggerInterface;
  25. /**
  26. * Class DBLockingProvider
  27. *
  28. * @group DB
  29. *
  30. * @package Test\Lock
  31. */
  32. class DBLockingProviderTest extends LockingProvider {
  33. /**
  34. * @var \OC\Lock\DBLockingProvider
  35. */
  36. protected $instance;
  37. /**
  38. * @var \OCP\IDBConnection
  39. */
  40. protected $connection;
  41. /**
  42. * @var \OCP\AppFramework\Utility\ITimeFactory
  43. */
  44. protected $timeFactory;
  45. protected $currentTime;
  46. protected function setUp(): void {
  47. $this->currentTime = time();
  48. $this->timeFactory = $this->createMock(ITimeFactory::class);
  49. $this->timeFactory->expects($this->any())
  50. ->method('getTime')
  51. ->willReturnCallback(function () {
  52. return $this->currentTime;
  53. });
  54. parent::setUp();
  55. }
  56. /**
  57. * @return \OCP\Lock\ILockingProvider
  58. */
  59. protected function getInstance() {
  60. $this->connection = \OC::$server->getDatabaseConnection();
  61. return new \OC\Lock\DBLockingProvider($this->connection, \OC::$server->get(LoggerInterface::class), $this->timeFactory, 3600);
  62. }
  63. protected function tearDown(): void {
  64. $this->connection->executeQuery('DELETE FROM `*PREFIX*file_locks`');
  65. parent::tearDown();
  66. }
  67. public function testCleanEmptyLocks() {
  68. $this->currentTime = 100;
  69. $this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
  70. $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
  71. $this->currentTime = 200;
  72. $this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
  73. $this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED);
  74. $this->currentTime = 150 + 3600;
  75. $this->assertEquals(3, $this->getLockEntryCount());
  76. $this->instance->cleanExpiredLocks();
  77. $this->assertEquals(2, $this->getLockEntryCount());
  78. }
  79. private function getLockEntryCount() {
  80. $query = $this->connection->prepare('SELECT count(*) FROM `*PREFIX*file_locks`');
  81. $query->execute();
  82. return $query->fetchOne();
  83. }
  84. protected function getLockValue($key) {
  85. $query = $this->connection->getQueryBuilder();
  86. $query->select('lock')
  87. ->from('file_locks')
  88. ->where($query->expr()->eq('key', $query->createNamedParameter($key)));
  89. $result = $query->execute();
  90. $rows = $result->fetchOne();
  91. $result->closeCursor();
  92. return $rows;
  93. }
  94. public function testDoubleShared() {
  95. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  96. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  97. $this->assertEquals(1, $this->getLockValue('foo'));
  98. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  99. $this->assertEquals(1, $this->getLockValue('foo'));
  100. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  101. $this->assertEquals(1, $this->getLockValue('foo'));
  102. $this->instance->releaseAll();
  103. $this->assertEquals(0, $this->getLockValue('foo'));
  104. }
  105. }