You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dblockingprovider.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Lock\ILockingProvider;
  23. /**
  24. * Class DBLockingProvider
  25. *
  26. * @group DB
  27. *
  28. * @package Test\Lock
  29. */
  30. class DBLockingProvider extends LockingProvider {
  31. /**
  32. * @var \OC\Lock\DBLockingProvider
  33. */
  34. protected $instance;
  35. /**
  36. * @var \OCP\IDBConnection
  37. */
  38. private $connection;
  39. /**
  40. * @var \OCP\AppFramework\Utility\ITimeFactory
  41. */
  42. private $timeFactory;
  43. private $currentTime;
  44. public function setUp() {
  45. $this->currentTime = time();
  46. $this->timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  47. $this->timeFactory->expects($this->any())
  48. ->method('getTime')
  49. ->will($this->returnCallback(function () {
  50. return $this->currentTime;
  51. }));
  52. parent::setUp();
  53. }
  54. /**
  55. * @return \OCP\Lock\ILockingProvider
  56. */
  57. protected function getInstance() {
  58. $this->connection = \OC::$server->getDatabaseConnection();
  59. return new \OC\Lock\DBLockingProvider($this->connection, \OC::$server->getLogger(), $this->timeFactory, 3600);
  60. }
  61. public function tearDown() {
  62. $this->connection->executeQuery('DELETE FROM `*PREFIX*file_locks`');
  63. parent::tearDown();
  64. }
  65. public function testCleanEmptyLocks() {
  66. $this->currentTime = 100;
  67. $this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
  68. $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
  69. $this->currentTime = 200;
  70. $this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
  71. $this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED);
  72. $this->currentTime = 150 + 3600;
  73. $this->assertEquals(3, $this->getLockEntryCount());
  74. $this->instance->cleanExpiredLocks();
  75. $this->assertEquals(2, $this->getLockEntryCount());
  76. }
  77. private function getLockEntryCount() {
  78. $query = $this->connection->prepare('SELECT count(*) FROM `*PREFIX*file_locks`');
  79. $query->execute();
  80. return $query->fetchColumn();
  81. }
  82. }