DBLockingProviderTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. private $connection;
  40. /**
  41. * @var \OCP\AppFramework\Utility\ITimeFactory
  42. */
  43. private $timeFactory;
  44. private $currentTime;
  45. public function setUp() {
  46. $this->currentTime = time();
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->timeFactory->expects($this->any())
  49. ->method('getTime')
  50. ->will($this->returnCallback(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, \OC::$server->getLogger(), $this->timeFactory, 3600);
  61. }
  62. public function tearDown() {
  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->fetchColumn();
  82. }
  83. }