1
0

DBLockingProviderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  25. * Class DBLockingProvider
  26. *
  27. * @group DB
  28. *
  29. * @package Test\Lock
  30. */
  31. class DBLockingProviderTest extends LockingProvider {
  32. /**
  33. * @var \OC\Lock\DBLockingProvider
  34. */
  35. protected $instance;
  36. /**
  37. * @var \OCP\IDBConnection
  38. */
  39. protected $connection;
  40. /**
  41. * @var \OCP\AppFramework\Utility\ITimeFactory
  42. */
  43. protected $timeFactory;
  44. protected $currentTime;
  45. protected function setUp(): void {
  46. $this->currentTime = time();
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->timeFactory->expects($this->any())
  49. ->method('getTime')
  50. ->willReturnCallback(function () {
  51. return $this->currentTime;
  52. });
  53. parent::setUp();
  54. }
  55. /**
  56. * @return \OCP\Lock\ILockingProvider
  57. */
  58. protected function getInstance() {
  59. $this->connection = \OC::$server->getDatabaseConnection();
  60. return new \OC\Lock\DBLockingProvider($this->connection, $this->timeFactory, 3600);
  61. }
  62. protected function tearDown(): void {
  63. $this->connection->executeQuery('DELETE FROM `*PREFIX*file_locks`');
  64. parent::tearDown();
  65. }
  66. public function testCleanEmptyLocks() {
  67. $this->currentTime = 100;
  68. $this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
  69. $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
  70. $this->currentTime = 200;
  71. $this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
  72. $this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED);
  73. $this->currentTime = 150 + 3600;
  74. $this->assertEquals(3, $this->getLockEntryCount());
  75. $this->instance->cleanExpiredLocks();
  76. $this->assertEquals(2, $this->getLockEntryCount());
  77. }
  78. private function getLockEntryCount() {
  79. $query = $this->connection->prepare('SELECT count(*) FROM `*PREFIX*file_locks`');
  80. $query->execute();
  81. return $query->fetchOne();
  82. }
  83. protected function getLockValue($key) {
  84. $query = $this->connection->getQueryBuilder();
  85. $query->select('lock')
  86. ->from('file_locks')
  87. ->where($query->expr()->eq('key', $query->createNamedParameter($key)));
  88. $result = $query->execute();
  89. $rows = $result->fetchOne();
  90. $result->closeCursor();
  91. return $rows;
  92. }
  93. public function testDoubleShared() {
  94. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  95. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  96. $this->assertEquals(1, $this->getLockValue('foo'));
  97. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  98. $this->assertEquals(1, $this->getLockValue('foo'));
  99. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  100. $this->assertEquals(1, $this->getLockValue('foo'));
  101. $this->instance->releaseAll();
  102. $this->assertEquals(0, $this->getLockValue('foo'));
  103. }
  104. }