|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 Broadcom Coporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* James Blackburn (Broadcom Corp.) - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.tests.concurrency; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.io.OutputStream; |
| 16 |
|
| 17 |
import java.io.PrintStream; |
| 18 |
|
| 19 |
import junit.framework.AssertionFailedError; |
| 20 |
import org.eclipse.core.runtime.CoreException; |
| 21 |
import org.eclipse.core.runtime.IStatus; |
| 22 |
|
| 23 |
import java.io.*; |
| 24 |
import junit.framework.TestCase; |
| 25 |
import org.eclipse.core.resources.*; |
| 26 |
import org.eclipse.core.runtime.*; |
| 27 |
import org.eclipse.core.runtime.jobs.Job; |
| 28 |
import org.eclipse.swt.widgets.Display; |
| 29 |
|
| 30 |
/** |
| 31 |
* Test case for resources plugin lock-up |
| 32 |
*/ |
| 33 |
public class Bug_294894 extends TestCase { |
| 34 |
|
| 35 |
/** |
| 36 |
* Fails the test due to the given throwable. |
| 37 |
*/ |
| 38 |
public static void fail(String message, Throwable e) { |
| 39 |
// If the exception is a CoreException with a multistatus |
| 40 |
// then print out the multistatus so we can see all the info. |
| 41 |
if (e instanceof CoreException) { |
| 42 |
IStatus status = ((CoreException) e).getStatus(); |
| 43 |
//if the status does not have an exception, print the stack for this one |
| 44 |
if (status.getException() == null) |
| 45 |
e.printStackTrace(); |
| 46 |
write(status, 0); |
| 47 |
} else |
| 48 |
e.printStackTrace(); |
| 49 |
AssertionFailedError assertFail = new AssertionFailedError(message + ": " + e); |
| 50 |
assertFail.initCause(e); |
| 51 |
throw assertFail; |
| 52 |
} |
| 53 |
|
| 54 |
private static void indent(OutputStream output, int indent) { |
| 55 |
for (int i = 0; i < indent; i++) |
| 56 |
try { |
| 57 |
output.write("\t".getBytes()); |
| 58 |
} catch (IOException e) { |
| 59 |
// ignore |
| 60 |
} |
| 61 |
} |
| 62 |
private static void write(IStatus status, int indent) { |
| 63 |
PrintStream output = System.out; |
| 64 |
indent(output, indent); |
| 65 |
output.println("Severity: " + status.getSeverity()); |
| 66 |
|
| 67 |
indent(output, indent); |
| 68 |
output.println("Plugin ID: " + status.getPlugin()); |
| 69 |
|
| 70 |
indent(output, indent); |
| 71 |
output.println("Code: " + status.getCode()); |
| 72 |
|
| 73 |
indent(output, indent); |
| 74 |
output.println("Message: " + status.getMessage()); |
| 75 |
|
| 76 |
if (status.getException() != null) { |
| 77 |
indent(output, indent); |
| 78 |
output.print("Exception: "); |
| 79 |
status.getException().printStackTrace(output); |
| 80 |
} |
| 81 |
|
| 82 |
if (status.isMultiStatus()) { |
| 83 |
IStatus[] children = status.getChildren(); |
| 84 |
for (int i = 0; i < children.length; i++) |
| 85 |
write(children[i], indent + 1); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public void appendContentsWithException(IProject project) { |
| 90 |
int count = 5; |
| 91 |
final IFile targets[] = new IFile[count]; |
| 92 |
for (int i = 0; i < count; i++) |
| 93 |
targets[i] = project.getFile("file" + i); |
| 94 |
try { |
| 95 |
for (int i = 0; i < count; i++) |
| 96 |
if (!targets[i].exists()) |
| 97 |
targets[i].create(getContents("abc"), false, null); |
| 98 |
} catch (CoreException e) { |
| 99 |
fail("1.0"); |
| 100 |
} |
| 101 |
|
| 102 |
// Run [count] failing append content jobs |
| 103 |
Job[] js = new Job[count]; |
| 104 |
for (int i = 0; i < count; i++) { |
| 105 |
final int j = i; |
| 106 |
js[i] = new Job("bad append") { |
| 107 |
protected IStatus run(IProgressMonitor monitor) { |
| 108 |
try { |
| 109 |
targets[j].appendContents(new InputStream() { |
| 110 |
public int read() throws IOException { |
| 111 |
throw new IOException("Stream closed"); |
| 112 |
} |
| 113 |
}, false, false, null); |
| 114 |
} catch (CoreException e) { |
| 115 |
//expected to fail due to invalid input stream |
| 116 |
} |
| 117 |
return Status.OK_STATUS; |
| 118 |
} |
| 119 |
}; |
| 120 |
js[i].schedule(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public InputStream getContents(String text) { |
| 125 |
return new ByteArrayInputStream(text.getBytes()); |
| 126 |
} |
| 127 |
|
| 128 |
public void testDeadLockFail() throws Exception { |
| 129 |
final String proj = "testProject"; |
| 130 |
|
| 131 |
final IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 132 |
final IProject project = workspace.getRoot().getProject(proj); |
| 133 |
final IFile file = project.getFile("foo"); |
| 134 |
workspace.run(new IWorkspaceRunnable() { |
| 135 |
public void run(IProgressMonitor monitor) throws CoreException { |
| 136 |
project.create(null); |
| 137 |
project.open(null); |
| 138 |
file.create(new ByteArrayInputStream(new byte[] {'a'}), true, null); |
| 139 |
} |
| 140 |
}, null); |
| 141 |
|
| 142 |
for (int i = 0; i < 1000; i++) { |
| 143 |
// Run this in a job holding the appropriate scheduling rule |
| 144 |
Job j = new Job("jobwithfilelock") { |
| 145 |
|
| 146 |
protected IStatus run(IProgressMonitor monitor) { |
| 147 |
|
| 148 |
// Job to deadlock with this: Display -> Resource Lock |
| 149 |
Job j = new Job("Workspace refresh") { |
| 150 |
protected IStatus run(IProgressMonitor monitor) { |
| 151 |
Display.getDefault().syncExec(new Runnable() { |
| 152 |
public void run() { |
| 153 |
try { |
| 154 |
appendContentsWithException(project); |
| 155 |
workspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); |
| 156 |
} catch (Exception e) { |
| 157 |
fail("refresh run", e); |
| 158 |
} |
| 159 |
} |
| 160 |
}); |
| 161 |
return Status.OK_STATUS; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
j.schedule(); |
| 166 |
try { |
| 167 |
Thread.sleep(1); |
| 168 |
} catch (Exception e) { |
| 169 |
fail("Sleep failed", e); |
| 170 |
} |
| 171 |
|
| 172 |
// Grab Display and refresh the file... [Resource -> Display ] |
| 173 |
Display.getDefault().syncExec(new Runnable() { |
| 174 |
public void run() { |
| 175 |
try { |
| 176 |
file.refreshLocal(IResource.DEPTH_ONE, null); |
| 177 |
} catch (Exception e) { |
| 178 |
fail("syncExec fail", e); |
| 179 |
} |
| 180 |
} |
| 181 |
}); |
| 182 |
return Status.OK_STATUS; |
| 183 |
} |
| 184 |
}; |
| 185 |
j.setRule(file); |
| 186 |
j.schedule(); |
| 187 |
j.join(); |
| 188 |
|
| 189 |
// Now try to append to the file |
| 190 |
file.appendContents(new ByteArrayInputStream(new byte[] {'a'}), IResource.FORCE, null); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |