1
0

DBLockingProviderTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Lock;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\Lock\ILockingProvider;
  10. /**
  11. * Class DBLockingProvider
  12. *
  13. * @group DB
  14. *
  15. * @package Test\Lock
  16. */
  17. class DBLockingProviderTest extends LockingProvider {
  18. /**
  19. * @var \OC\Lock\DBLockingProvider
  20. */
  21. protected $instance;
  22. /**
  23. * @var \OCP\IDBConnection
  24. */
  25. protected $connection;
  26. /**
  27. * @var \OCP\AppFramework\Utility\ITimeFactory
  28. */
  29. protected $timeFactory;
  30. protected $currentTime;
  31. protected function setUp(): void {
  32. $this->currentTime = time();
  33. $this->timeFactory = $this->createMock(ITimeFactory::class);
  34. $this->timeFactory->expects($this->any())
  35. ->method('getTime')
  36. ->willReturnCallback(function () {
  37. return $this->currentTime;
  38. });
  39. parent::setUp();
  40. }
  41. /**
  42. * @return \OCP\Lock\ILockingProvider
  43. */
  44. protected function getInstance() {
  45. $this->connection = \OC::$server->getDatabaseConnection();
  46. return new \OC\Lock\DBLockingProvider($this->connection, $this->timeFactory, 3600);
  47. }
  48. protected function tearDown(): void {
  49. $this->connection->executeQuery('DELETE FROM `*PREFIX*file_locks`');
  50. parent::tearDown();
  51. }
  52. public function testCleanEmptyLocks() {
  53. $this->currentTime = 100;
  54. $this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
  55. $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
  56. $this->currentTime = 200;
  57. $this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
  58. $this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED);
  59. $this->currentTime = 150 + 3600;
  60. $this->assertEquals(3, $this->getLockEntryCount());
  61. $this->instance->cleanExpiredLocks();
  62. $this->assertEquals(2, $this->getLockEntryCount());
  63. }
  64. private function getLockEntryCount() {
  65. $query = $this->connection->prepare('SELECT count(*) FROM `*PREFIX*file_locks`');
  66. $query->execute();
  67. return $query->fetchOne();
  68. }
  69. protected function getLockValue($key) {
  70. $query = $this->connection->getQueryBuilder();
  71. $query->select('lock')
  72. ->from('file_locks')
  73. ->where($query->expr()->eq('key', $query->createNamedParameter($key)));
  74. $result = $query->execute();
  75. $rows = $result->fetchOne();
  76. $result->closeCursor();
  77. return $rows;
  78. }
  79. public function testDoubleShared() {
  80. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  81. $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
  82. $this->assertEquals(1, $this->getLockValue('foo'));
  83. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  84. $this->assertEquals(1, $this->getLockValue('foo'));
  85. $this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
  86. $this->assertEquals(1, $this->getLockValue('foo'));
  87. $this->instance->releaseAll();
  88. $this->assertEquals(0, $this->getLockValue('foo'));
  89. }
  90. }