comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 134:cc3dc759c989

Added an advisory lock to avoid race conditions during database setup
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 08 May 2019 20:21:13 +0200
parents aceb0174c4ed
children 4cd7e45b671e
comparison
equal deleted inserted replaced
133:b1d7d255fb73 134:cc3dc759c989
511 ASSERT_NE(r1.studyId, r2.studyId); 511 ASSERT_NE(r1.studyId, r2.studyId);
512 ASSERT_NE(r1.seriesId, r2.seriesId); 512 ASSERT_NE(r1.seriesId, r2.seriesId);
513 ASSERT_NE(r1.instanceId, r2.instanceId); 513 ASSERT_NE(r1.instanceId, r2.instanceId);
514 } 514 }
515 #endif 515 #endif
516
517
518 TEST(PostgreSQL, Lock2)
519 {
520 std::auto_ptr<PostgreSQLDatabase> db1(CreateTestDatabase());
521 db1->Open();
522
523 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0
524 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1
525
526 // OK, as this is the same connection
527 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 2
528 ASSERT_TRUE(db1->ReleaseAdvisoryLock(43)); // lock counter = 1
529
530 // Try and release twice the lock
531 ASSERT_TRUE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0
532 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // cannot unlock
533 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1
534
535 {
536 std::auto_ptr<PostgreSQLDatabase> db2(CreateTestDatabase());
537 db2->Open();
538
539 // The "db1" is still actively locking
540 ASSERT_FALSE(db2->AcquireAdvisoryLock(43));
541
542 // Release the "db1" lock
543 ASSERT_TRUE(db1->ReleaseAdvisoryLock(43));
544 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43));
545
546 // "db2" can now acquire the lock, but not "db1"
547 ASSERT_TRUE(db2->AcquireAdvisoryLock(43));
548 ASSERT_FALSE(db1->AcquireAdvisoryLock(43));
549 }
550
551 // "db2" is closed, "db1" can now acquire the lock
552 ASSERT_TRUE(db1->AcquireAdvisoryLock(43));
553 }