LockingUnitTestCase.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. abstract class Redis_Tests_Lock_LockingUnitTestCase extends Redis_Tests_AbstractUnitTestCase
  3. {
  4. /**
  5. * Ensure lock flush at tear down
  6. *
  7. * @var array
  8. */
  9. protected $backends = array();
  10. /**
  11. * Get the lock client class name
  12. *
  13. * @return string
  14. * Lock backend class name or null if cannot spawn it
  15. */
  16. abstract protected function getLockBackendClass();
  17. public function tearDown()
  18. {
  19. if (!empty($this->backends)) {
  20. foreach ($this->backends as $backend) {
  21. $backend->lockReleaseAll();
  22. }
  23. $this->backends = array();
  24. }
  25. parent::tearDown();
  26. }
  27. /**
  28. * Create a new lock backend with a generated lock id
  29. *
  30. * @return Redis_Lock_BackendInterface
  31. */
  32. public function createLockBackend()
  33. {
  34. if (!$this->getLockBackendClass()) {
  35. throw new \Exception("Lock backend class does not exist");
  36. }
  37. $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_LOCK);
  38. return $this->backends[] = new $className(
  39. Redis_Client::getClient(),
  40. Redis_Client::getDefaultPrefix('lock')
  41. );
  42. }
  43. public function testLock()
  44. {
  45. $b1 = $this->createLockBackend();
  46. $b2 = $this->createLockBackend();
  47. $s = $b1->lockAcquire('test1', 20000);
  48. $this->assertTrue($s, "Lock test1 acquired");
  49. $s = $b1->lockAcquire('test1', 20000);
  50. $this->assertTrue($s, "Lock test1 acquired a second time by the same thread");
  51. $s = $b2->lockAcquire('test1', 20000);
  52. $this->assertFalse($s, "Lock test1 could not be acquired by another thread");
  53. $b2->lockRelease('test1');
  54. $s = $b2->lockAcquire('test1');
  55. $this->assertFalse($s, "Lock test1 could not be released by another thread");
  56. $b1->lockRelease('test1');
  57. $s = $b2->lockAcquire('test1');
  58. $this->assertTrue($s, "Lock test1 has been released by the first thread");
  59. }
  60. public function testReleaseAll()
  61. {
  62. $b1 = $this->createLockBackend();
  63. $b2 = $this->createLockBackend();
  64. $b1->lockAcquire('test1', 200);
  65. $b1->lockAcquire('test2', 2000);
  66. $b1->lockAcquire('test3', 20000);
  67. $s = $b2->lockAcquire('test2');
  68. $this->assertFalse($s, "Lock test2 could not be released by another thread");
  69. $s = $b2->lockAcquire('test3');
  70. $this->assertFalse($s, "Lock test4 could not be released by another thread");
  71. $b1->lockReleaseAll();
  72. $s = $b2->lockAcquire('test1');
  73. $this->assertTrue($s, "Lock test1 has been released");
  74. $s = $b2->lockAcquire('test2');
  75. $this->assertTrue($s, "Lock test2 has been released");
  76. $s = $b2->lockAcquire('test3');
  77. $this->assertTrue($s, "Lock test3 has been released");
  78. $b2->lockReleaseAll();
  79. }
  80. public function testConcurentLock()
  81. {
  82. /*
  83. * Code for web test case
  84. *
  85. $this->drupalGet('redis/acquire/test1/1000');
  86. $this->assertText("REDIS_ACQUIRED", "Lock test1 acquired");
  87. $this->drupalGet('redis/acquire/test1/1');
  88. $this->assertText("REDIS_FAILED", "Lock test1 could not be acquired by a second thread");
  89. $this->drupalGet('redis/acquire/test2/1000');
  90. $this->assertText("REDIS_ACQUIRED", "Lock test2 acquired");
  91. $this->drupalGet('redis/acquire/test2/1');
  92. $this->assertText("REDIS_FAILED", "Lock test2 could not be acquired by a second thread");
  93. */
  94. }
  95. }