1 19 20 21 package org.openide.text; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Position ; 28 import javax.swing.text.StyledDocument ; 29 30 import junit.textui.TestRunner; 31 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.junit.NbTestSuite; 34 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.LocalFileSystem; 37 import org.openide.text.CloneableEditorSupport; 38 39 40 58 public class ReloadDeadlockTest extends NbTestCase { 59 60 61 public ReloadDeadlockTest(String s) { 62 super(s); 63 } 64 65 66 public void testDeadlock() throws Exception { 67 System.out.println("START of deadlock test: see issue #12557."); 68 69 LocalFileSystem lfs = new LocalFileSystem(); 70 71 File rootDir; 72 73 int i = 1; 74 do { 75 rootDir = new File (getWorkDir(), "TestRootDir"+(i++)); 76 } while(rootDir.exists() && !rootDir.isDirectory()); 77 78 System.err.println("root exists="+rootDir.exists()); 79 80 if(!rootDir.exists()) { 81 System.err.println("Created rootDir="+rootDir.mkdir()); 82 } 83 84 lfs.setRootDirectory(rootDir); 85 86 FileObject root = lfs.getRoot(); 87 88 FileObject fo = root.getFileObject("test", "txt"); 89 90 if(fo == null) { 91 fo = root.createData("test", "txt"); 92 } 93 94 final SimpleCESHidden.Env env = new SimpleCESHidden.Env(fo); 95 final CloneableEditorSupport ces = new SimpleCESHidden(env); 96 env.setSupport(ces); 97 98 final StyledDocument doc = ces.openDocument(); 99 100 System.err.println("Creating PositionRef at 0 offset."); 101 final PositionRef posRef = ces.createPositionRef(0, Position.Bias.Forward); 102 103 System.err.println("\nT1="+Thread.currentThread()); System.err.println("T1: Acquiring doc write lock."); 105 106 final Thread th2 = new Thread (new Runnable () { 107 public void run() { 108 109 System.err.println("\nT2=" + Thread.currentThread() 110 + "T2: Starting to reload doc.\n" 111 + "T2: Acquiring locks in order: 1st CES lock, 2nd doc write lock.\n" 112 + "T2: Thus will acquire CES lock and wait on doc write lock."); 113 ces.reloadDocument().waitFinished(); 114 115 System.err.println("T2: document reloaded"); 116 } 117 }); 118 119 120 NbDocument.runAtomic(doc, new Runnable () { 121 public void run() { 122 System.err.println("T1: doc write lock acquired."); 123 124 th2.start(); 126 127 try { 128 Thread.currentThread().sleep(3000); 129 } catch(InterruptedException ie) { 130 System.err.println("T1: interrupted"); 131 ie.printStackTrace(); 132 } 133 134 System.err.println("\nT1: trying to get Position from PositionRef.\n" 135 + "T1: Acquring CES lock. It simulates the issue condition," 136 + " this thread already holds doc write lock.\n" 137 + "T1: After this the deadlock should occure!!"); 138 139 try { 140 Position pos = posRef.getPosition(); 141 142 doc.insertString(pos.getOffset(), "New String Added", null); 143 144 System.err.println("T1: Position="+pos); 145 146 System.err.println("T1: Document after insert="+doc.getText(pos.getOffset(), doc.getLength())); 147 148 } catch(IOException ioe) { 149 ioe.printStackTrace(); 150 } catch(BadLocationException ble) { 151 ble.printStackTrace(); 152 } 153 154 System.err.println("T1: new string inserted, releasing doc write lock"); 155 } 156 }); 157 158 System.err.println("T1 waits for reloading thread (T2)."); 159 th2.join(); 160 161 System.out.println("END of deadlock test, see issue #12557."); 162 163 System.err.println("Document after reload=\n" + doc.getText(0, doc.getLength())); 164 165 rootDir.delete(); 166 } 167 168 } 169 | Popular Tags |