File tree Expand file tree Collapse file tree 3 files changed +38
-6
lines changed
tests/test/src/libdaemonjvm/tests Expand file tree Collapse file tree 3 files changed +38
-6
lines changed Original file line number Diff line number Diff line change @@ -8,27 +8,38 @@ import java.nio.file.attribute.PosixFilePermission
8
8
import java .nio .file .StandardOpenOption
9
9
import scala .collection .JavaConverters ._
10
10
import scala .util .Properties
11
+ import libdaemonjvm .server .LockError
12
+ import java .nio .channels .OverlappingFileLockException
11
13
12
14
final case class LockFiles (
13
15
lockFile : Path ,
14
16
pidFile : Path ,
15
17
socketPaths : SocketPaths
16
18
) {
17
- def withLock [T ](t : => T ): T = {
19
+ def withLock [T ](t : => Either [ LockError , T ] ): Either [ LockError , T ] = {
18
20
if (! Files .exists(lockFile)) {
19
21
Files .createDirectories(lockFile.normalize.getParent)
20
22
Files .write(lockFile, Array .emptyByteArray)
21
23
}
22
- var c : FileChannel = null
23
- var l : FileLock = null
24
+ var c : FileChannel = null
25
+ var l : Either [ OverlappingFileLockException , FileLock ] = null
24
26
try {
25
27
c = FileChannel .open(lockFile, StandardOpenOption .WRITE )
26
- l = c.lock()
27
- t
28
+ l =
29
+ try Right (c.tryLock())
30
+ catch {
31
+ case ex : OverlappingFileLockException =>
32
+ Left (ex)
33
+ }
34
+ l match {
35
+ case Left (ex) => Left (new LockError .Locked (lockFile, ex))
36
+ case Right (null ) => Left (new LockError .Locked (lockFile))
37
+ case Right (_) => t
38
+ }
28
39
}
29
40
finally {
30
41
if (l != null )
31
- try l.release()
42
+ try l.foreach(_. release() )
32
43
catch {
33
44
case _ : ClosedChannelException =>
34
45
case _ : IOException =>
Original file line number Diff line number Diff line change @@ -25,4 +25,6 @@ object LockError {
25
25
extends FatalError (s " Cannot delete $file" , cause)
26
26
final class ZombieFound (val pid : Int , val connectionError : Throwable )
27
27
extends RecoverableError (s " Cannot connect to process $pid" , connectionError)
28
+ final class Locked (val file : Path , cause : Throwable = null )
29
+ extends RecoverableError (s " $file already locked " , cause)
28
30
}
Original file line number Diff line number Diff line change @@ -140,4 +140,23 @@ class LockTests extends munit.FunSuite {
140
140
}
141
141
}
142
142
143
+ test(" locked" ) {
144
+ TestUtil .withTestDir { dir =>
145
+ val files = TestUtil .lockFiles(dir)
146
+ val e = files.withLock {
147
+ TestUtil .tryAcquire(files) { maybeChannel =>
148
+ maybeChannel match {
149
+ case Left (e : LockError .Locked ) =>
150
+ case Left (otherError) =>
151
+ throw new Exception (" Unexpected error type (expected Locked)" , otherError)
152
+ case Right (channel) =>
153
+ sys.error(" Opening new server channel should have failed" )
154
+ }
155
+ }
156
+ Right (())
157
+ }
158
+ expect(e.isRight)
159
+ }
160
+ }
161
+
143
162
}
You can’t perform that action at this time.
0 commit comments