mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-21 11:34:38 +02:00
8164649: Cleanup of test java/nio/channels/FileChannel/Lock.java
Reviewed-by: alanb
This commit is contained in:
parent
9cda798a33
commit
ea29d586dd
1 changed files with 129 additions and 123 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -22,8 +22,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 4429043 4493595 6332756 6709457
|
* @bug 4429043 4493595 6332756 6709457 7146506
|
||||||
* @summary The FileChannel file locking
|
* @summary Test FileChannel file locking
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
@ -33,17 +33,14 @@ import static java.nio.file.StandardOpenOption.*;
|
||||||
/**
|
/**
|
||||||
* Testing FileChannel's lock method.
|
* Testing FileChannel's lock method.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Lock {
|
public class Lock {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
if (args.length > 0) {
|
if (args.length == 2) {
|
||||||
if(args[0].equals("1")) {
|
attemptLock(args[1], args[0].equals("2"));
|
||||||
MadWriter mw = new MadWriter(args[1], false);
|
|
||||||
} else {
|
|
||||||
MadWriter mw = new MadWriter(args[1], true);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
|
} else if (args.length != 0) {
|
||||||
|
throw new RuntimeException("Wrong number of parameters.");
|
||||||
}
|
}
|
||||||
File blah = File.createTempFile("blah", null);
|
File blah = File.createTempFile("blah", null);
|
||||||
blah.deleteOnExit();
|
blah.deleteOnExit();
|
||||||
|
@ -56,41 +53,28 @@ public class Lock {
|
||||||
test2(blah, false);
|
test2(blah, false);
|
||||||
test3(blah);
|
test3(blah);
|
||||||
test4(blah);
|
test4(blah);
|
||||||
blah.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void test2(File blah, boolean b) throws Exception {
|
|
||||||
RandomAccessFile raf = new RandomAccessFile(blah, "rw");
|
|
||||||
FileChannel channel = raf.getChannel();
|
|
||||||
FileLock lock;
|
|
||||||
if (b)
|
|
||||||
lock = channel.lock();
|
|
||||||
else
|
|
||||||
lock = channel.tryLock();
|
|
||||||
lock.release();
|
|
||||||
channel.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test mutual locking with other process
|
||||||
|
*/
|
||||||
static void test1(File blah, String str) throws Exception {
|
static void test1(File blah, String str) throws Exception {
|
||||||
|
try (RandomAccessFile fis = new RandomAccessFile(blah, "rw")) {
|
||||||
// Grab the lock
|
|
||||||
RandomAccessFile fis = new RandomAccessFile(blah, "rw");
|
|
||||||
FileChannel fc = fis.getChannel();
|
FileChannel fc = fis.getChannel();
|
||||||
FileLock lock = null;
|
FileLock lock = null;
|
||||||
|
|
||||||
|
// grab the lock
|
||||||
if (str.equals("1")) {
|
if (str.equals("1")) {
|
||||||
lock = fc.lock(0, 10, false);
|
lock = fc.lock(0, 10, false);
|
||||||
if (lock == null)
|
if (lock == null)
|
||||||
throw new RuntimeException("Lock should not return null");
|
throw new RuntimeException("Lock should not return null");
|
||||||
try {
|
try {
|
||||||
FileLock lock2 = fc.lock(5, 10, false);
|
fc.lock(5, 10, false);
|
||||||
throw new RuntimeException("Overlapping locks allowed");
|
throw new RuntimeException("Overlapping locks allowed");
|
||||||
} catch (OverlappingFileLockException e) {
|
} catch (OverlappingFileLockException e) {} // correct result
|
||||||
// Correct result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec the tamperer
|
// execute the tamperer
|
||||||
String command = System.getProperty("java.home") +
|
String command = System.getProperty("java.home") +
|
||||||
File.separator + "bin" + File.separator + "java";
|
File.separator + "bin" + File.separator + "java";
|
||||||
String testClasses = System.getProperty("test.classes");
|
String testClasses = System.getProperty("test.classes");
|
||||||
|
@ -99,43 +83,68 @@ public class Lock {
|
||||||
command += " Lock " + str + " " + blah;
|
command += " Lock " + str + " " + blah;
|
||||||
Process p = Runtime.getRuntime().exec(command);
|
Process p = Runtime.getRuntime().exec(command);
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader
|
// evaluate System.out of child process
|
||||||
(new InputStreamReader(p.getInputStream()));
|
|
||||||
|
|
||||||
String s;
|
String s;
|
||||||
int count = 0;
|
boolean hasOutput = false;
|
||||||
while ((s = in.readLine()) != null) {
|
InputStreamReader isr;
|
||||||
if (!s.equals("good")) {
|
isr = new InputStreamReader(p.getInputStream());
|
||||||
if (File.separatorChar == '/') {
|
BufferedReader br = new BufferedReader(isr);
|
||||||
// Fails on windows over NFS...
|
while ((s = br.readLine()) != null) {
|
||||||
throw new RuntimeException("Failed: "+s);
|
// only throw on Unix as windows over NFS fails...
|
||||||
|
if ((File.separatorChar == '/') && !s.equals("good")) {
|
||||||
|
throw new RuntimeException("Failed: " + s);
|
||||||
}
|
}
|
||||||
}
|
hasOutput = true;
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count == 0) {
|
// evaluate System.err in case of System.out of child process
|
||||||
in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
// was empty
|
||||||
while ((s = in.readLine()) != null) {
|
if (!hasOutput) {
|
||||||
System.err.println("Error output: " + s);
|
isr = new InputStreamReader(p.getErrorStream());
|
||||||
|
br = new BufferedReader(isr);
|
||||||
|
if ((s = br.readLine()) != null) {
|
||||||
|
System.err.println("Error output:");
|
||||||
|
System.err.println(s);
|
||||||
|
while ((s = br.readLine()) != null) {
|
||||||
|
System.err.println(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Failed, no output");
|
throw new RuntimeException("Failed, no output");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
// clean up, check multiple releases
|
||||||
if (lock != null) {
|
if (lock != null) {
|
||||||
/* Check multiple releases */
|
|
||||||
lock.release();
|
lock.release();
|
||||||
lock.release();
|
lock.release();
|
||||||
}
|
}
|
||||||
fc.close();
|
}
|
||||||
fis.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The overlap check for file locks should be JVM-wide
|
/**
|
||||||
private static void test3(File blah) throws Exception {
|
* Basic test for FileChannel.lock() and FileChannel.tryLock()
|
||||||
FileChannel fc1 = new RandomAccessFile(blah, "rw").getChannel();
|
*/
|
||||||
FileChannel fc2 = new RandomAccessFile(blah, "rw").getChannel();
|
static void test2(File blah, boolean b) throws Exception {
|
||||||
|
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
|
||||||
|
FileChannel channel = raf.getChannel();
|
||||||
|
FileLock lock;
|
||||||
|
if (b)
|
||||||
|
lock = channel.lock();
|
||||||
|
else
|
||||||
|
lock = channel.tryLock();
|
||||||
|
lock.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that overlapping file locking is not possible when using different
|
||||||
|
* FileChannel objects to the same file path
|
||||||
|
*/
|
||||||
|
static void test3(File blah) throws Exception {
|
||||||
|
try (RandomAccessFile raf1 = new RandomAccessFile(blah, "rw");
|
||||||
|
RandomAccessFile raf2 = new RandomAccessFile(blah, "rw"))
|
||||||
|
{
|
||||||
|
FileChannel fc1 = raf1.getChannel();
|
||||||
|
FileChannel fc2 = raf2.getChannel();
|
||||||
|
|
||||||
// lock via one channel, and then attempt to lock the same file
|
// lock via one channel, and then attempt to lock the same file
|
||||||
// using a second channel
|
// using a second channel
|
||||||
|
@ -143,33 +152,29 @@ public class Lock {
|
||||||
try {
|
try {
|
||||||
fc2.tryLock();
|
fc2.tryLock();
|
||||||
throw new RuntimeException("Overlapping locks allowed");
|
throw new RuntimeException("Overlapping locks allowed");
|
||||||
} catch (OverlappingFileLockException x) {
|
} catch (OverlappingFileLockException x) {}
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
fc2.lock();
|
fc2.lock();
|
||||||
throw new RuntimeException("Overlapping locks allowed");
|
throw new RuntimeException("Overlapping locks allowed");
|
||||||
} catch (OverlappingFileLockException x) {
|
} catch (OverlappingFileLockException x) {}
|
||||||
}
|
|
||||||
|
|
||||||
// release lock and the attempt to lock with the second channel
|
// release lock and the attempt to lock with the second channel
|
||||||
// should succeed.
|
// should succeed.
|
||||||
fl1.release();
|
fl1.release();
|
||||||
FileLock fl2 = fc2.lock();
|
fc2.lock();
|
||||||
try {
|
try {
|
||||||
fc1.lock();
|
fc1.lock();
|
||||||
throw new RuntimeException("Overlapping locks allowed");
|
throw new RuntimeException("Overlapping locks allowed");
|
||||||
} catch (OverlappingFileLockException x) {
|
} catch (OverlappingFileLockException x) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fc1.close();
|
|
||||||
fc2.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test file locking when file is opened for append
|
* Test file locking when file is opened for append
|
||||||
*/
|
*/
|
||||||
static void test4(File blah) throws Exception {
|
static void test4(File blah) throws Exception {
|
||||||
try (FileChannel fc = new FileOutputStream(blah, true).getChannel()) {
|
try (FileOutputStream fos = new FileOutputStream(blah, true)) {
|
||||||
|
FileChannel fc = fos.getChannel();
|
||||||
fc.tryLock().release();
|
fc.tryLock().release();
|
||||||
fc.tryLock(0L, 1L, false).release();
|
fc.tryLock(0L, 1L, false).release();
|
||||||
fc.lock().release();
|
fc.lock().release();
|
||||||
|
@ -182,30 +187,31 @@ public class Lock {
|
||||||
fc.lock(0L, 1L, false).release();
|
fc.lock(0L, 1L, false).release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class MadWriter {
|
/**
|
||||||
public MadWriter(String s, boolean b) throws Exception {
|
* Utility method to be run in secondary process which tries to acquire a
|
||||||
File f = new File(s);
|
* lock on a FileChannel
|
||||||
RandomAccessFile fos = new RandomAccessFile(f, "rw");
|
*/
|
||||||
FileChannel fc = fos.getChannel();
|
static void attemptLock(String fileName,
|
||||||
|
boolean expectsLock) throws Exception
|
||||||
|
{
|
||||||
|
File f = new File(fileName);
|
||||||
|
try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
|
||||||
|
FileChannel fc = raf.getChannel();
|
||||||
if (fc.tryLock(10, 10, false) == null) {
|
if (fc.tryLock(10, 10, false) == null) {
|
||||||
System.out.println("bad: Failed to grab adjacent lock");
|
System.out.println("bad: Failed to grab adjacent lock");
|
||||||
}
|
}
|
||||||
FileLock lock = fc.tryLock(0, 10, false);
|
if (fc.tryLock(0, 10, false) == null) {
|
||||||
if (lock == null) {
|
if (expectsLock)
|
||||||
if (b)
|
|
||||||
System.out.println("bad");
|
System.out.println("bad");
|
||||||
else
|
else
|
||||||
System.out.println("good");
|
System.out.println("good");
|
||||||
} else {
|
} else {
|
||||||
if (b)
|
if (expectsLock)
|
||||||
System.out.println("good");
|
System.out.println("good");
|
||||||
else
|
else
|
||||||
System.out.println("bad");
|
System.out.println("bad");
|
||||||
}
|
}
|
||||||
fc.close();
|
|
||||||
fos.close();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue