author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
25 |
* @bug 4429043 4493595 6332756 6709457 7146506 |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
26 |
* @summary Test FileChannel file locking |
2 | 27 |
*/ |
28 |
||
29 |
import java.io.*; |
|
30 |
import java.nio.channels.*; |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
31 |
import static java.nio.file.StandardOpenOption.*; |
2 | 32 |
|
33 |
/** |
|
34 |
* Testing FileChannel's lock method. |
|
35 |
*/ |
|
36 |
public class Lock { |
|
37 |
||
38 |
public static void main(String[] args) throws Exception { |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
39 |
if (args.length == 2) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
40 |
attemptLock(args[1], args[0].equals("2")); |
2 | 41 |
return; |
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
42 |
} else if (args.length != 0) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
43 |
throw new RuntimeException("Wrong number of parameters."); |
2 | 44 |
} |
45 |
File blah = File.createTempFile("blah", null); |
|
46 |
blah.deleteOnExit(); |
|
47 |
RandomAccessFile raf = new RandomAccessFile(blah, "rw"); |
|
48 |
raf.write(1); |
|
49 |
raf.close(); |
|
50 |
test1(blah, "1"); |
|
51 |
test1(blah, "2"); |
|
52 |
test2(blah, true); |
|
53 |
test2(blah, false); |
|
54 |
test3(blah); |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
55 |
test4(blah); |
2 | 56 |
} |
57 |
||
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
58 |
/** |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
59 |
* Test mutual locking with other process |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
60 |
*/ |
2 | 61 |
static void test1(File blah, String str) throws Exception { |
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
62 |
try (RandomAccessFile fis = new RandomAccessFile(blah, "rw")) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
63 |
FileChannel fc = fis.getChannel(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
64 |
FileLock lock = null; |
2 | 65 |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
66 |
// grab the lock |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
67 |
if (str.equals("1")) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
68 |
lock = fc.lock(0, 10, false); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
69 |
if (lock == null) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
70 |
throw new RuntimeException("Lock should not return null"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
71 |
try { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
72 |
fc.lock(5, 10, false); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
73 |
throw new RuntimeException("Overlapping locks allowed"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
74 |
} catch (OverlappingFileLockException e) {} // correct result |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
75 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
76 |
|
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
77 |
// execute the tamperer |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
78 |
String command = System.getProperty("java.home") + |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
79 |
File.separator + "bin" + File.separator + "java"; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
80 |
String testClasses = System.getProperty("test.classes"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
81 |
if (testClasses != null) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
82 |
command += " -cp " + testClasses; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
83 |
command += " Lock " + str + " " + blah; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
84 |
Process p = Runtime.getRuntime().exec(command); |
2 | 85 |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
86 |
// evaluate System.out of child process |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
87 |
String s; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
88 |
boolean hasOutput = false; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
89 |
InputStreamReader isr; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
90 |
isr = new InputStreamReader(p.getInputStream()); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
91 |
BufferedReader br = new BufferedReader(isr); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
92 |
while ((s = br.readLine()) != null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
93 |
// only throw on Unix as windows over NFS fails... |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
94 |
if ((File.separatorChar == '/') && !s.equals("good")) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
95 |
throw new RuntimeException("Failed: " + s); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
96 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
97 |
hasOutput = true; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
98 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
99 |
|
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
100 |
// evaluate System.err in case of System.out of child process |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
101 |
// was empty |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
102 |
if (!hasOutput) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
103 |
isr = new InputStreamReader(p.getErrorStream()); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
104 |
br = new BufferedReader(isr); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
105 |
if ((s = br.readLine()) != null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
106 |
System.err.println("Error output:"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
107 |
System.err.println(s); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
108 |
while ((s = br.readLine()) != null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
109 |
System.err.println(s); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
110 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
111 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
112 |
throw new RuntimeException("Failed, no output"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
113 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
114 |
|
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
115 |
// clean up, check multiple releases |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
116 |
if (lock != null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
117 |
lock.release(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
118 |
lock.release(); |
2 | 119 |
} |
120 |
} |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
121 |
} |
2 | 122 |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
123 |
/** |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
124 |
* Basic test for FileChannel.lock() and FileChannel.tryLock() |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
125 |
*/ |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
126 |
static void test2(File blah, boolean b) throws Exception { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
127 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
128 |
FileChannel channel = raf.getChannel(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
129 |
FileLock lock; |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
130 |
if (b) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
131 |
lock = channel.lock(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
132 |
else |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
133 |
lock = channel.tryLock(); |
2 | 134 |
lock.release(); |
135 |
} |
|
136 |
} |
|
137 |
||
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
138 |
/** |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
139 |
* Test that overlapping file locking is not possible when using different |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
140 |
* FileChannel objects to the same file path |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
141 |
*/ |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
142 |
static void test3(File blah) throws Exception { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
143 |
try (RandomAccessFile raf1 = new RandomAccessFile(blah, "rw"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
144 |
RandomAccessFile raf2 = new RandomAccessFile(blah, "rw")) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
145 |
{ |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
146 |
FileChannel fc1 = raf1.getChannel(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
147 |
FileChannel fc2 = raf2.getChannel(); |
2 | 148 |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
149 |
// lock via one channel, and then attempt to lock the same file |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
150 |
// using a second channel |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
151 |
FileLock fl1 = fc1.lock(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
152 |
try { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
153 |
fc2.tryLock(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
154 |
throw new RuntimeException("Overlapping locks allowed"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
155 |
} catch (OverlappingFileLockException x) {} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
156 |
try { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
157 |
fc2.lock(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
158 |
throw new RuntimeException("Overlapping locks allowed"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
159 |
} catch (OverlappingFileLockException x) {} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
160 |
|
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
161 |
// release lock and the attempt to lock with the second channel |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
162 |
// should succeed. |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
163 |
fl1.release(); |
2 | 164 |
fc2.lock(); |
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
165 |
try { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
166 |
fc1.lock(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
167 |
throw new RuntimeException("Overlapping locks allowed"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
168 |
} catch (OverlappingFileLockException x) {} |
2 | 169 |
} |
170 |
} |
|
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
171 |
|
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
172 |
/** |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
173 |
* Test file locking when file is opened for append |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
174 |
*/ |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
175 |
static void test4(File blah) throws Exception { |
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
176 |
try (FileOutputStream fos = new FileOutputStream(blah, true)) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
177 |
FileChannel fc = fos.getChannel(); |
7515
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
178 |
fc.tryLock().release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
179 |
fc.tryLock(0L, 1L, false).release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
180 |
fc.lock().release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
181 |
fc.lock(0L, 1L, false).release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
182 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
183 |
try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) { |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
184 |
fc.tryLock().release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
185 |
fc.tryLock(0L, 1L, false).release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
186 |
fc.lock().release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
187 |
fc.lock(0L, 1L, false).release(); |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
188 |
} |
43202796198e
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
5970
diff
changeset
|
189 |
} |
2 | 190 |
|
40566
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
191 |
/** |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
192 |
* Utility method to be run in secondary process which tries to acquire a |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
193 |
* lock on a FileChannel |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
194 |
*/ |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
195 |
static void attemptLock(String fileName, |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
196 |
boolean expectsLock) throws Exception |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
197 |
{ |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
198 |
File f = new File(fileName); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
199 |
try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
200 |
FileChannel fc = raf.getChannel(); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
201 |
if (fc.tryLock(10, 10, false) == null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
202 |
System.out.println("bad: Failed to grab adjacent lock"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
203 |
} |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
204 |
if (fc.tryLock(0, 10, false) == null) { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
205 |
if (expectsLock) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
206 |
System.out.println("bad"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
207 |
else |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
208 |
System.out.println("good"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
209 |
} else { |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
210 |
if (expectsLock) |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
211 |
System.out.println("good"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
212 |
else |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
213 |
System.out.println("bad"); |
cf1f1b3822e7
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
clanger
parents:
7668
diff
changeset
|
214 |
} |
2 | 215 |
} |
216 |
} |
|
217 |
} |