diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-26 14:18:01 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:38:19 +0200 |
commit | fd1ddad98df45bc3c18be7980c1cbe68ce6b219c (patch) | |
tree | e4017d3c979604f40e78cdc305f0ab191aedd4b9 /tests/bookmark/BookmarkFileServiceTest.php | |
parent | 458b6b9918ec27154dd45416947bb93bedb97109 (diff) | |
download | Shaarli-fd1ddad98df45bc3c18be7980c1cbe68ce6b219c.tar.gz Shaarli-fd1ddad98df45bc3c18be7980c1cbe68ce6b219c.tar.zst Shaarli-fd1ddad98df45bc3c18be7980c1cbe68ce6b219c.zip |
Add mutex on datastore I/O operations
To make sure that there is no concurrent operation on the datastore file.
Fixes #1132
Diffstat (limited to 'tests/bookmark/BookmarkFileServiceTest.php')
-rw-r--r-- | tests/bookmark/BookmarkFileServiceTest.php | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index c399822b..6c56dfaa 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php | |||
@@ -6,6 +6,7 @@ | |||
6 | namespace Shaarli\Bookmark; | 6 | namespace Shaarli\Bookmark; |
7 | 7 | ||
8 | use DateTime; | 8 | use DateTime; |
9 | use malkusch\lock\mutex\NoMutex; | ||
9 | use ReferenceLinkDB; | 10 | use ReferenceLinkDB; |
10 | use ReflectionClass; | 11 | use ReflectionClass; |
11 | use Shaarli; | 12 | use Shaarli; |
@@ -52,6 +53,9 @@ class BookmarkFileServiceTest extends TestCase | |||
52 | */ | 53 | */ |
53 | protected $privateLinkDB = null; | 54 | protected $privateLinkDB = null; |
54 | 55 | ||
56 | /** @var NoMutex */ | ||
57 | protected $mutex; | ||
58 | |||
55 | /** | 59 | /** |
56 | * Instantiates public and private LinkDBs with test data | 60 | * Instantiates public and private LinkDBs with test data |
57 | * | 61 | * |
@@ -68,6 +72,8 @@ class BookmarkFileServiceTest extends TestCase | |||
68 | */ | 72 | */ |
69 | protected function setUp(): void | 73 | protected function setUp(): void |
70 | { | 74 | { |
75 | $this->mutex = new NoMutex(); | ||
76 | |||
71 | if (file_exists(self::$testDatastore)) { | 77 | if (file_exists(self::$testDatastore)) { |
72 | unlink(self::$testDatastore); | 78 | unlink(self::$testDatastore); |
73 | } | 79 | } |
@@ -87,8 +93,8 @@ class BookmarkFileServiceTest extends TestCase | |||
87 | $this->refDB = new \ReferenceLinkDB(); | 93 | $this->refDB = new \ReferenceLinkDB(); |
88 | $this->refDB->write(self::$testDatastore); | 94 | $this->refDB->write(self::$testDatastore); |
89 | $this->history = new History('sandbox/history.php'); | 95 | $this->history = new History('sandbox/history.php'); |
90 | $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); | 96 | $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false); |
91 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 97 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
92 | } | 98 | } |
93 | 99 | ||
94 | /** | 100 | /** |
@@ -105,7 +111,7 @@ class BookmarkFileServiceTest extends TestCase | |||
105 | $db = self::getMethod('migrate'); | 111 | $db = self::getMethod('migrate'); |
106 | $db->invokeArgs($this->privateLinkDB, []); | 112 | $db->invokeArgs($this->privateLinkDB, []); |
107 | 113 | ||
108 | $db = new \FakeBookmarkService($this->conf, $this->history, true); | 114 | $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, true); |
109 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | 115 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); |
110 | $this->assertEquals($this->refDB->countLinks(), $db->count()); | 116 | $this->assertEquals($this->refDB->countLinks(), $db->count()); |
111 | } | 117 | } |
@@ -174,7 +180,7 @@ class BookmarkFileServiceTest extends TestCase | |||
174 | $this->assertEquals($updated, $bookmark->getUpdated()); | 180 | $this->assertEquals($updated, $bookmark->getUpdated()); |
175 | 181 | ||
176 | // reload from file | 182 | // reload from file |
177 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 183 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
178 | 184 | ||
179 | $bookmark = $this->privateLinkDB->get(43); | 185 | $bookmark = $this->privateLinkDB->get(43); |
180 | $this->assertEquals(43, $bookmark->getId()); | 186 | $this->assertEquals(43, $bookmark->getId()); |
@@ -212,7 +218,7 @@ class BookmarkFileServiceTest extends TestCase | |||
212 | $this->assertNull($bookmark->getUpdated()); | 218 | $this->assertNull($bookmark->getUpdated()); |
213 | 219 | ||
214 | // reload from file | 220 | // reload from file |
215 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 221 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
216 | 222 | ||
217 | $bookmark = $this->privateLinkDB->get(43); | 223 | $bookmark = $this->privateLinkDB->get(43); |
218 | $this->assertEquals(43, $bookmark->getId()); | 224 | $this->assertEquals(43, $bookmark->getId()); |
@@ -242,7 +248,7 @@ class BookmarkFileServiceTest extends TestCase | |||
242 | $this->assertEquals(43, $bookmark->getId()); | 248 | $this->assertEquals(43, $bookmark->getId()); |
243 | 249 | ||
244 | // reload from file | 250 | // reload from file |
245 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 251 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
246 | 252 | ||
247 | $this->privateLinkDB->get(43); | 253 | $this->privateLinkDB->get(43); |
248 | } | 254 | } |
@@ -314,7 +320,7 @@ class BookmarkFileServiceTest extends TestCase | |||
314 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | 320 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); |
315 | 321 | ||
316 | // reload from file | 322 | // reload from file |
317 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 323 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
318 | 324 | ||
319 | $bookmark = $this->privateLinkDB->get(42); | 325 | $bookmark = $this->privateLinkDB->get(42); |
320 | $this->assertEquals(42, $bookmark->getId()); | 326 | $this->assertEquals(42, $bookmark->getId()); |
@@ -355,7 +361,7 @@ class BookmarkFileServiceTest extends TestCase | |||
355 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | 361 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); |
356 | 362 | ||
357 | // reload from file | 363 | // reload from file |
358 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 364 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
359 | 365 | ||
360 | $bookmark = $this->privateLinkDB->get(42); | 366 | $bookmark = $this->privateLinkDB->get(42); |
361 | $this->assertEquals(42, $bookmark->getId()); | 367 | $this->assertEquals(42, $bookmark->getId()); |
@@ -388,7 +394,7 @@ class BookmarkFileServiceTest extends TestCase | |||
388 | $this->assertEquals($title, $bookmark->getTitle()); | 394 | $this->assertEquals($title, $bookmark->getTitle()); |
389 | 395 | ||
390 | // reload from file | 396 | // reload from file |
391 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 397 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
392 | 398 | ||
393 | $bookmark = $this->privateLinkDB->get(42); | 399 | $bookmark = $this->privateLinkDB->get(42); |
394 | $this->assertEquals(42, $bookmark->getId()); | 400 | $this->assertEquals(42, $bookmark->getId()); |
@@ -452,7 +458,7 @@ class BookmarkFileServiceTest extends TestCase | |||
452 | $this->assertEquals(43, $bookmark->getId()); | 458 | $this->assertEquals(43, $bookmark->getId()); |
453 | 459 | ||
454 | // reload from file | 460 | // reload from file |
455 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 461 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
456 | 462 | ||
457 | $bookmark = $this->privateLinkDB->get(43); | 463 | $bookmark = $this->privateLinkDB->get(43); |
458 | $this->assertEquals(43, $bookmark->getId()); | 464 | $this->assertEquals(43, $bookmark->getId()); |
@@ -472,7 +478,7 @@ class BookmarkFileServiceTest extends TestCase | |||
472 | $this->assertEquals($title, $bookmark->getTitle()); | 478 | $this->assertEquals($title, $bookmark->getTitle()); |
473 | 479 | ||
474 | // reload from file | 480 | // reload from file |
475 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 481 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
476 | 482 | ||
477 | $bookmark = $this->privateLinkDB->get(42); | 483 | $bookmark = $this->privateLinkDB->get(42); |
478 | $this->assertEquals(42, $bookmark->getId()); | 484 | $this->assertEquals(42, $bookmark->getId()); |
@@ -515,7 +521,7 @@ class BookmarkFileServiceTest extends TestCase | |||
515 | $this->assertEquals($title, $bookmark->getTitle()); | 521 | $this->assertEquals($title, $bookmark->getTitle()); |
516 | 522 | ||
517 | // reload from file | 523 | // reload from file |
518 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 524 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
519 | 525 | ||
520 | $bookmark = $this->privateLinkDB->get(42); | 526 | $bookmark = $this->privateLinkDB->get(42); |
521 | $this->assertEquals(42, $bookmark->getId()); | 527 | $this->assertEquals(42, $bookmark->getId()); |
@@ -541,7 +547,7 @@ class BookmarkFileServiceTest extends TestCase | |||
541 | $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); | 547 | $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); |
542 | 548 | ||
543 | // reload from file | 549 | // reload from file |
544 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | 550 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
545 | 551 | ||
546 | $this->privateLinkDB->get(42); | 552 | $this->privateLinkDB->get(42); |
547 | } | 553 | } |
@@ -645,7 +651,7 @@ class BookmarkFileServiceTest extends TestCase | |||
645 | 651 | ||
646 | $conf = new ConfigManager('tests/utils/config/configJson'); | 652 | $conf = new ConfigManager('tests/utils/config/configJson'); |
647 | $conf->set('resource.datastore', 'null/store.db'); | 653 | $conf->set('resource.datastore', 'null/store.db'); |
648 | new BookmarkFileService($conf, $this->history, true); | 654 | new BookmarkFileService($conf, $this->history, $this->mutex, true); |
649 | } | 655 | } |
650 | 656 | ||
651 | /** | 657 | /** |
@@ -655,7 +661,7 @@ class BookmarkFileServiceTest extends TestCase | |||
655 | { | 661 | { |
656 | unlink(self::$testDatastore); | 662 | unlink(self::$testDatastore); |
657 | $this->assertFileNotExists(self::$testDatastore); | 663 | $this->assertFileNotExists(self::$testDatastore); |
658 | new BookmarkFileService($this->conf, $this->history, true); | 664 | new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
659 | $this->assertFileExists(self::$testDatastore); | 665 | $this->assertFileExists(self::$testDatastore); |
660 | 666 | ||
661 | // ensure the correct data has been written | 667 | // ensure the correct data has been written |
@@ -669,7 +675,7 @@ class BookmarkFileServiceTest extends TestCase | |||
669 | { | 675 | { |
670 | unlink(self::$testDatastore); | 676 | unlink(self::$testDatastore); |
671 | $this->assertFileNotExists(self::$testDatastore); | 677 | $this->assertFileNotExists(self::$testDatastore); |
672 | $db = new \FakeBookmarkService($this->conf, $this->history, false); | 678 | $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, false); |
673 | $this->assertFileNotExists(self::$testDatastore); | 679 | $this->assertFileNotExists(self::$testDatastore); |
674 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | 680 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); |
675 | $this->assertCount(0, $db->getBookmarks()); | 681 | $this->assertCount(0, $db->getBookmarks()); |
@@ -702,13 +708,13 @@ class BookmarkFileServiceTest extends TestCase | |||
702 | */ | 708 | */ |
703 | public function testSave() | 709 | public function testSave() |
704 | { | 710 | { |
705 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | 711 | $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
706 | $dbSize = $testDB->count(); | 712 | $dbSize = $testDB->count(); |
707 | 713 | ||
708 | $bookmark = new Bookmark(); | 714 | $bookmark = new Bookmark(); |
709 | $testDB->add($bookmark); | 715 | $testDB->add($bookmark); |
710 | 716 | ||
711 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | 717 | $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true); |
712 | $this->assertEquals($dbSize + 1, $testDB->count()); | 718 | $this->assertEquals($dbSize + 1, $testDB->count()); |
713 | } | 719 | } |
714 | 720 | ||
@@ -718,7 +724,7 @@ class BookmarkFileServiceTest extends TestCase | |||
718 | public function testCountHiddenPublic() | 724 | public function testCountHiddenPublic() |
719 | { | 725 | { |
720 | $this->conf->set('privacy.hide_public_links', true); | 726 | $this->conf->set('privacy.hide_public_links', true); |
721 | $linkDB = new BookmarkFileService($this->conf, $this->history, false); | 727 | $linkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false); |
722 | 728 | ||
723 | $this->assertEquals(0, $linkDB->count()); | 729 | $this->assertEquals(0, $linkDB->count()); |
724 | } | 730 | } |