Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
44ed771a
Commit
44ed771a
authored
Dec 19, 2013
by
catch
Browse files
Issue
#1976172
by chx, andypost: Comment entity acquired and releases the different locks.
parent
ba6591cd
Changes
3
Hide whitespace changes
Inline
Side-by-side
core/modules/comment/lib/Drupal/comment/CommentStorageController.php
View file @
44ed771a
...
...
@@ -19,11 +19,6 @@
*/
class
CommentStorageController
extends
FieldableDatabaseStorageController
implements
CommentStorageControllerInterface
{
/**
* The thread for which a lock was acquired.
*/
protected
$threadLock
=
''
;
/**
* {@inheritdoc}
*/
...
...
core/modules/comment/lib/Drupal/comment/Entity/Comment.php
View file @
44ed771a
...
...
@@ -56,6 +56,11 @@
*/
class
Comment
extends
ContentEntityBase
implements
CommentInterface
{
/**
* The thread for which a lock was acquired.
*/
protected
$threadLock
=
''
;
/**
* The comment ID.
*
...
...
@@ -273,12 +278,13 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
}
}
// Finally, build the thread field for this new comment. To avoid
// race conditions, get a lock on the thread. If aother process already
// race conditions, get a lock on the thread. If a
n
other process already
// has the lock, just move to the next integer.
do
{
$thread
=
$prefix
.
comment_int_to_alphadecimal
(
++
$n
)
.
'/'
;
}
while
(
!
lock
()
->
acquire
(
"comment:
{
$this
->
entity_id
->
value
}
:
$thread
"
));
$this
->
threadLock
=
$thread
;
$lock_name
=
"comment:
{
$this
->
entity_id
->
value
}
:
$thread
"
;
}
while
(
!
\
Drupal
::
lock
()
->
acquire
(
$lock_name
));
$this
->
threadLock
=
$lock_name
;
}
if
(
empty
(
$this
->
created
->
value
))
{
$this
->
created
->
value
=
REQUEST_TIME
;
...
...
@@ -316,7 +322,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
*/
protected
function
releaseThreadLock
()
{
if
(
$this
->
threadLock
)
{
lock
()
->
release
(
$this
->
threadLock
);
\
Drupal
::
lock
()
->
release
(
$this
->
threadLock
);
$this
->
threadLock
=
''
;
}
}
...
...
core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
0 → 100644
View file @
44ed771a
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Entity\CommentTest
*/
namespace
Drupal\comment\Tests\Entity
{
use
Drupal\comment\Entity\Comment
;
use
Drupal\Core\DependencyInjection\ContainerBuilder
;
use
Drupal\Tests\UnitTestCase
;
/**
* Unit tests for the comment entity lock behavior.
*
* @group Drupal
*/
class
CommentLockTest
extends
UnitTestCase
{
/**
* {@inheritdoc}
*/
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Comment locks'
,
'description'
=>
'Test comment acquires and releases the right lock.'
,
'group'
=>
'Comment'
,
);
}
/**
* Test the lock behavior.
*/
public
function
testLocks
()
{
$container
=
new
ContainerBuilder
();
$container
->
set
(
'current_user'
,
$this
->
getMock
(
'Drupal\Core\Session\AccountInterface'
));
$container
->
register
(
'request'
,
'Symfony\Component\HttpFoundation\Request'
);
$lock
=
$this
->
getMock
(
'Drupal\Core\Lock\LockBackendInterface'
);
$cid
=
2
;
$lock_name
=
"comment:
$cid
:./"
;
$lock
->
expects
(
$this
->
at
(
0
))
->
method
(
'acquire'
)
->
with
(
$lock_name
,
30
)
->
will
(
$this
->
returnValue
(
TRUE
));
$lock
->
expects
(
$this
->
at
(
1
))
->
method
(
'release'
)
->
with
(
$lock_name
);
$lock
->
expects
(
$this
->
exactly
(
2
))
->
method
(
$this
->
anything
());
$container
->
set
(
'lock'
,
$lock
);
\
Drupal
::
setContainer
(
$container
);
$methods
=
get_class_methods
(
'Drupal\comment\Entity\Comment'
);
unset
(
$methods
[
array_search
(
'preSave'
,
$methods
)]);
unset
(
$methods
[
array_search
(
'postSave'
,
$methods
)]);
$comment
=
$this
->
getMockBuilder
(
'Drupal\comment\Entity\Comment'
)
->
disableOriginalConstructor
()
->
setMethods
(
$methods
)
->
getMock
();
$comment
->
expects
(
$this
->
once
())
->
method
(
'isNew'
)
->
will
(
$this
->
returnValue
(
TRUE
));
foreach
(
array
(
'status'
,
'pid'
,
'created'
,
'changed'
,
'entity_id'
,
'uid'
,
'thread'
,
'hostname'
)
as
$property
)
{
$comment
->
$property
=
new
\
stdClass
();
}
$comment
->
status
->
value
=
1
;
$comment
->
entity_id
->
value
=
$cid
;
$comment
->
uid
->
target_id
=
3
;
$comment
->
pid
->
target_id
=
42
;
$comment
->
pid
->
entity
=
new
\
stdClass
();
$comment
->
pid
->
entity
->
thread
=
(
object
)
array
(
'value'
=>
''
);
$storage_controller
=
$this
->
getMock
(
'Drupal\comment\CommentStorageControllerInterface'
);
$comment
->
preSave
(
$storage_controller
);
$comment
->
postSave
(
$storage_controller
);
}
/**
* {@inheritdoc}
*/
protected
function
tearDown
()
{
parent
::
tearDown
();
$container
=
new
ContainerBuilder
();
\
Drupal
::
setContainer
(
$container
);
}
}
}
namespace
{
if
(
!
function_exists
(
'comment_int_to_alphadecimal'
))
{
function
comment_int_to_alphadecimal
()
{}
}
if
(
!
function_exists
(
'module_invoke_all'
))
{
function
module_invoke_all
()
{}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment