Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F101870715
PhutilFileLockTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Feb 14, 14:34
Size
5 KB
Mime Type
text/x-php
Expires
Sun, Feb 16, 14:34 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24245244
Attached To
rPHU libphutil
PhutilFileLockTestCase.php
View Options
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @group testcase
*/
final
class
PhutilFileLockTestCase
extends
PhutilTestCase
{
public
function
testLockTesting
()
{
// We should be able to acquire locks.
$file
=
new
TempFile
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
}
public
function
testLockHolding
()
{
// When a process is holding a lock, other processes should be unable
// to acquire it.
$file
=
new
TempFile
();
$hold
=
$this
->
holdLock
(
$file
);
$this
->
assertEqual
(
false
,
$this
->
lockTest
(
$file
));
$hold
->
resolveKill
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
}
public
function
testInProcessLocking
()
{
// Other processes should be unable to lock a file if we hold the lock.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$lock
->
lock
();
$this
->
assertEqual
(
false
,
$this
->
lockTest
(
$file
));
$lock
->
unlock
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
}
public
function
testInProcessHolding
()
{
// We should be unable to lock a file if another process is holding the
// lock.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$hold
=
$this
->
holdLock
(
$file
);
$caught
=
null
;
try
{
$lock
->
lock
();
}
catch
(
PhutilLockException
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
$caught
instanceof
PhutilLockException
));
$hold
->
resolveKill
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
$lock
->
lock
();
$lock
->
unlock
();
}
public
function
testRelock
()
{
// Trying to lock a file twice should throw an exception.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$lock
->
lock
();
$caught
=
null
;
try
{
$lock
->
lock
();
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
$caught
instanceof
Exception
));
}
public
function
testExcessiveUnlock
()
{
// Trying to unlock a file twice should throw an exception.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$lock
->
lock
();
$lock
->
unlock
();
$caught
=
null
;
try
{
$lock
->
unlock
();
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
$caught
instanceof
Exception
));
}
public
function
testUnlockAll
()
{
// unlockAll() should release all locks.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$lock
->
lock
();
$this
->
assertEqual
(
false
,
$this
->
lockTest
(
$file
));
PhutilFileLock
::
unlockAll
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
// Calling this again shouldn't do anything bad.
PhutilFileLock
::
unlockAll
();
$this
->
assertEqual
(
true
,
$this
->
lockTest
(
$file
));
$lock
->
lock
();
$lock
->
unlock
();
}
public
function
testIsLocked
()
{
// isLocked() should report lock status accurately.
$file
=
new
TempFile
();
$lock
=
PhutilFileLock
::
newForPath
(
$file
);
$this
->
assertEqual
(
false
,
$lock
->
isLocked
());
$lock
->
lock
();
$this
->
assertEqual
(
true
,
$lock
->
isLocked
());
$lock
->
unlock
();
$this
->
assertEqual
(
false
,
$lock
->
isLocked
());
}
private
function
lockTest
(
$file
)
{
list
(
$err
)
=
$this
->
buildLockFuture
(
'--test'
,
$file
)->
resolve
();
return
(
$err
==
0
);
}
private
function
holdLock
(
$file
)
{
$future
=
$this
->
buildLockFuture
(
'--hold'
,
$file
);
// We can't return until we're sure the subprocess has had time to acquire
// the lock. Since actually testing for the lock would be kind of silly
// and guarantee that we loop forever if the locking primitive broke,
// watch stdout for a *claim* that it has acquired the lock instead.
// Make sure we don't loop forever, no matter how bad things get.
$future
->
setTimeout
(
30
);
$buf
=
''
;
while
(!
$future
->
isReady
())
{
list
(
$stdout
)
=
$future
->
read
();
$buf
.=
$stdout
;
if
(
strpos
(
$buf
,
'LOCK ACQUIRED'
)
!==
false
)
{
return
$future
;
}
}
throw
new
Exception
(
"Unable to hold lock in external process!"
);
}
private
function
buildLockFuture
(
$flags
,
$file
)
{
$root
=
dirname
(
phutil_get_library_root
(
'phutil'
));
$bin
=
$root
.
'/scripts/utils/lock.php'
;
$future
=
new
ExecFuture
(
'%s %C %s'
,
$bin
,
$flags
,
$file
);
$future
->
start
();
return
$future
;
}
}
Event Timeline
Log In to Comment