提交 9bfea301 编写于 作者: D dfuchs

8052403: java/util/logging/CheckZombieLockTest.java fails with NoSuchFileException

Summary: CheckZombieLockTest and CheckLockLocationTest should work with different temporary log directories so that they can be run concurrently. This fix changes the name of the log directory used by CheckZombieLockTest.
Reviewed-by: mchung
上级 b9c26b53
...@@ -51,23 +51,23 @@ import java.util.logging.Level; ...@@ -51,23 +51,23 @@ import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
public class CheckZombieLockTest { public class CheckZombieLockTest {
private static final String WRITABLE_DIR = "writable-dir"; private static final String WRITABLE_DIR = "writable-lockfile-dir";
private static volatile boolean supportsLocking = true; private static volatile boolean supportsLocking = true;
static enum TestCase { static enum TestCase {
WRITABLE, // just verifies that we can create a file in our 'writable-dir' WRITABLE, // just verifies that we can create a file in our 'writable-lockfile-dir'
CLOSE, // checks that closing a FileHandler removes its lock file CLOSE, // checks that closing a FileHandler removes its lock file
CREATE_FIRST, // verifies that 'writable-dir' contains no lock, then creates a first FileHandler. CREATE_FIRST, // verifies that 'writable-lockfile-dir' contains no lock, then creates a first FileHandler.
CREATE_NEXT, // verifies that 'writable-dir' contains a single lock, then creates the next FileHandler CREATE_NEXT, // verifies that 'writable-lockfile-dir' contains a single lock, then creates the next FileHandler
REUSE, // verifies that zombie lock files can be reused REUSE, // verifies that zombie lock files can be reused
CLEANUP // removes "writable-dir" CLEANUP // removes "writable-lockfile-dir"
}; };
public static void main(String... args) throws IOException { public static void main(String... args) throws IOException {
// we'll base all file creation attempts on the system temp directory, // we'll base all file creation attempts on the system temp directory,
// %t // %t
File writableDir = setup(); File writableDir = setup();
System.out.println("Writable dir is: "+writableDir.getAbsolutePath()); System.out.println("Writable dir is: " + writableDir.getAbsolutePath());
// we now have one writable directory to work with: // we now have one writable directory to work with:
// writableDir // writableDir
if (args == null || args.length == 0) { if (args == null || args.length == 0) {
...@@ -104,7 +104,7 @@ public class CheckZombieLockTest { ...@@ -104,7 +104,7 @@ public class CheckZombieLockTest {
case REUSE: testFileHandlerReuse(writableDir); break; case REUSE: testFileHandlerReuse(writableDir); break;
// Removes the writableDir // Removes the writableDir
case CLEANUP: delete(writableDir); break; case CLEANUP: delete(writableDir); break;
default: throw new RuntimeException("No such test case: "+arg); default: throw new RuntimeException("No such test case: " + arg);
} }
} }
} }
...@@ -120,7 +120,7 @@ public class CheckZombieLockTest { ...@@ -120,7 +120,7 @@ public class CheckZombieLockTest {
// Test 1: make sure we can create/delete files in the writable dir. // Test 1: make sure we can create/delete files in the writable dir.
final File file = new File(writableDir, "test.txt"); final File file = new File(writableDir, "test.txt");
if (!createFile(file, false)) { if (!createFile(file, false)) {
throw new IOException("Can't create "+file+"\n\tUnable to run test"); throw new IOException("Can't create " + file + "\n\tUnable to run test");
} else { } else {
delete(file); delete(file);
} }
...@@ -159,22 +159,22 @@ public class CheckZombieLockTest { ...@@ -159,22 +159,22 @@ public class CheckZombieLockTest {
private static void testFileHandlerClose(File writableDir) throws IOException { private static void testFileHandlerClose(File writableDir) throws IOException {
File fakeLock = new File(writableDir, "log.log.lck"); File fakeLock = new File(writableDir, "log.log.lck");
if (!createFile(fakeLock, false)) { if (!createFile(fakeLock, false)) {
throw new IOException("Can't create fake lock file: "+fakeLock); throw new IOException("Can't create fake lock file: " + fakeLock);
} }
try { try {
List<File> before = listLocks(writableDir, true); List<File> before = listLocks(writableDir, true);
System.out.println("before: " +before.size() + " locks found"); System.out.println("before: " + before.size() + " locks found");
FileHandler handler = createFileHandler(writableDir); FileHandler handler = createFileHandler(writableDir);
System.out.println("handler created: "+handler); System.out.println("handler created: " + handler);
List<File> after = listLocks(writableDir, true); List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found"); System.out.println("after creating handler: " + after.size() + " locks found");
handler.close(); handler.close();
System.out.println("handler closed: "+handler); System.out.println("handler closed: " + handler);
List<File> afterClose = listLocks(writableDir, true); List<File> afterClose = listLocks(writableDir, true);
System.out.println("after closing handler: " + afterClose.size() + " locks found"); System.out.println("after closing handler: " + afterClose.size() + " locks found");
afterClose.removeAll(before); afterClose.removeAll(before);
if (!afterClose.isEmpty()) { if (!afterClose.isEmpty()) {
throw new RuntimeException("Zombie lock file detected: "+ afterClose); throw new RuntimeException("Zombie lock file detected: " + afterClose);
} }
} finally { } finally {
if (fakeLock.canRead()) delete(fakeLock); if (fakeLock.canRead()) delete(fakeLock);
...@@ -186,21 +186,22 @@ public class CheckZombieLockTest { ...@@ -186,21 +186,22 @@ public class CheckZombieLockTest {
private static void testFileHandlerReuse(File writableDir) throws IOException { private static void testFileHandlerReuse(File writableDir) throws IOException {
List<File> before = listLocks(writableDir, true); List<File> before = listLocks(writableDir, true);
System.out.println("before: " +before.size() + " locks found"); System.out.println("before: " + before.size() + " locks found");
try { try {
if (!before.isEmpty()) { if (!before.isEmpty()) {
throw new RuntimeException("Expected no lock file! Found: "+before); throw new RuntimeException("Expected no lock file! Found: " + before);
} }
} finally { } finally {
before.stream().forEach(CheckZombieLockTest::delete); before.stream().forEach(CheckZombieLockTest::delete);
} }
FileHandler handler1 = createFileHandler(writableDir); FileHandler handler1 = createFileHandler(writableDir);
System.out.println("handler created: "+handler1); System.out.println("handler created: " + handler1);
List<File> after = listLocks(writableDir, true); List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found"); System.out.println("after creating handler: " + after.size() + " locks found");
if (after.size() != 1) { if (after.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found for "+handler1+": "+after); throw new RuntimeException("Unexpected number of lock files found for "
+ handler1 + ": " + after);
} }
final File lock = after.get(0); final File lock = after.get(0);
after.clear(); after.clear();
...@@ -208,33 +209,34 @@ public class CheckZombieLockTest { ...@@ -208,33 +209,34 @@ public class CheckZombieLockTest {
after = listLocks(writableDir, true); after = listLocks(writableDir, true);
System.out.println("after closing handler: " + after.size() + " locks found"); System.out.println("after closing handler: " + after.size() + " locks found");
if (!after.isEmpty()) { if (!after.isEmpty()) {
throw new RuntimeException("Unexpected number of lock files found for "+handler1+": "+after); throw new RuntimeException("Unexpected number of lock files found for "
+ handler1 + ": " + after);
} }
if (!createFile(lock, false)) { if (!createFile(lock, false)) {
throw new IOException("Can't create fake lock file: "+lock); throw new IOException("Can't create fake lock file: " + lock);
} }
try { try {
before = listLocks(writableDir, true); before = listLocks(writableDir, true);
System.out.println("before: " +before.size() + " locks found"); System.out.println("before: " + before.size() + " locks found");
if (before.size() != 1) { if (before.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found: "+before+" expected [" throw new RuntimeException("Unexpected number of lock files found: "
+lock+"]."); + before + " expected [" + lock + "].");
} }
FileHandler handler2 = createFileHandler(writableDir); FileHandler handler2 = createFileHandler(writableDir);
System.out.println("handler created: "+handler2); System.out.println("handler created: " + handler2);
after = listLocks(writableDir, true); after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found"); System.out.println("after creating handler: " + after.size() + " locks found");
after.removeAll(before); after.removeAll(before);
if (!after.isEmpty()) { if (!after.isEmpty()) {
throw new RuntimeException("Unexpected lock file found: "+after throw new RuntimeException("Unexpected lock file found: " + after
+ "\n\t" + lock + " should have been reused"); + "\n\t" + lock + " should have been reused");
} }
handler2.close(); handler2.close();
System.out.println("handler closed: "+handler2); System.out.println("handler closed: " + handler2);
List<File> afterClose = listLocks(writableDir, true); List<File> afterClose = listLocks(writableDir, true);
System.out.println("after closing handler: " + afterClose.size() + " locks found"); System.out.println("after closing handler: " + afterClose.size() + " locks found");
if (!afterClose.isEmpty()) { if (!afterClose.isEmpty()) {
throw new RuntimeException("Zombie lock file detected: "+ afterClose); throw new RuntimeException("Zombie lock file detected: " + afterClose);
} }
if (supportsLocking) { if (supportsLocking) {
...@@ -243,18 +245,19 @@ public class CheckZombieLockTest { ...@@ -243,18 +245,19 @@ public class CheckZombieLockTest {
StandardOpenOption.WRITE); StandardOpenOption.WRITE);
try { try {
if (fc.tryLock() != null) { if (fc.tryLock() != null) {
System.out.println("locked: "+lock); System.out.println("locked: " + lock);
handler2 = createFileHandler(writableDir); handler2 = createFileHandler(writableDir);
System.out.println("handler created: "+handler2); System.out.println("handler created: " + handler2);
after = listLocks(writableDir, true); after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found"); System.out.println("after creating handler: " + after.size()
+ " locks found");
after.removeAll(before); after.removeAll(before);
if (after.size() != 1) { if (after.size() != 1) {
throw new RuntimeException("Unexpected lock files found: "+after throw new RuntimeException("Unexpected lock files found: " + after
+ "\n\t" + lock + " should not have been reused"); + "\n\t" + lock + " should not have been reused");
} }
} else { } else {
throw new RuntimeException("Failed to lock: "+lock); throw new RuntimeException("Failed to lock: " + lock);
} }
} finally { } finally {
delete(lock); delete(lock);
...@@ -271,22 +274,23 @@ public class CheckZombieLockTest { ...@@ -271,22 +274,23 @@ public class CheckZombieLockTest {
private static void testFileHandlerCreate(File writableDir, boolean first) private static void testFileHandlerCreate(File writableDir, boolean first)
throws IOException { throws IOException {
List<File> before = listLocks(writableDir, true); List<File> before = listLocks(writableDir, true);
System.out.println("before: " +before.size() + " locks found"); System.out.println("before: " + before.size() + " locks found");
try { try {
if (first && !before.isEmpty()) { if (first && !before.isEmpty()) {
throw new RuntimeException("Expected no lock file! Found: "+before); throw new RuntimeException("Expected no lock file! Found: " + before);
} else if (!first && before.size() != 1) { } else if (!first && before.size() != 1) {
throw new RuntimeException("Expected a single lock file! Found: "+before); throw new RuntimeException("Expected a single lock file! Found: " + before);
} }
} finally { } finally {
before.stream().forEach(CheckZombieLockTest::delete); before.stream().forEach(CheckZombieLockTest::delete);
} }
FileHandler handler = createFileHandler(writableDir); FileHandler handler = createFileHandler(writableDir);
System.out.println("handler created: "+handler); System.out.println("handler created: " + handler);
List<File> after = listLocks(writableDir, true); List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found"); System.out.println("after creating handler: " + after.size() + " locks found");
if (after.size() != 1) { if (after.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found for "+handler+": "+after); throw new RuntimeException("Unexpected number of lock files found for "
+ handler + ": " + after);
} }
} }
...@@ -305,7 +309,7 @@ public class CheckZombieLockTest { ...@@ -305,7 +309,7 @@ public class CheckZombieLockTest {
tmpDir = System.getProperty("user.home"); tmpDir = System.getProperty("user.home");
} }
File tmpOrHomeDir = new File(tmpDir); File tmpOrHomeDir = new File(tmpDir);
// Create a writable directory here (%t/writable-dir) // Create a writable directory here (%t/writable-lockfile-dir)
File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR); File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
if (!createFile(writableDir, true)) { if (!createFile(writableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create" throw new RuntimeException("Test setup failed: unable to create"
...@@ -314,9 +318,10 @@ public class CheckZombieLockTest { ...@@ -314,9 +318,10 @@ public class CheckZombieLockTest {
} }
// try to determine whether file locking is supported // try to determine whether file locking is supported
final String uniqueFileName = UUID.randomUUID().toString()+".lck";
try { try {
FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(), FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(),
UUID.randomUUID().toString()+".lck"), uniqueFileName),
StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
StandardOpenOption.DELETE_ON_CLOSE); StandardOpenOption.DELETE_ON_CLOSE);
try { try {
...@@ -326,9 +331,11 @@ public class CheckZombieLockTest { ...@@ -326,9 +331,11 @@ public class CheckZombieLockTest {
} finally { } finally {
fc.close(); fc.close();
} }
} catch(Throwable t) { } catch (IOException t) {
// should not happen // should not happen
t.printStackTrace(); System.err.println("Failed to create new file " + uniqueFileName +
" in " + writableDir.getAbsolutePath());
throw new RuntimeException("Test setup failed: unable to run test", t);
} }
return writableDir; return writableDir;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册