This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 40738
Collapse All | Expand All

(-)src/org/openide/filesystems/StreamPool.java (-1 / +25 lines)
Lines 51-58 Link Here
51
     * @param fo FileObject that issues is
51
     * @param fo FileObject that issues is
52
     * @param InputStream that should be issued
52
     * @param InputStream that should be issued
53
     * @return subclassed InputStream that is registered as mentioned above */
53
     * @return subclassed InputStream that is registered as mentioned above */
54
    public static synchronized InputStream createInputStream (AbstractFolder fo, InputStream is) {
54
    public static synchronized InputStream createInputStream (AbstractFolder fo, InputStream is) throws InterruptedIOException {
55
        InputStream retVal = new NotifyInputStream (fo, is);
55
        InputStream retVal = new NotifyInputStream (fo, is);
56
        
57
        for (int cnt = 4; ; cnt--) {
58
            if (!get (fo).oStream ().isEmpty ()) {
59
                if (cnt == 0) {
60
                    throw new InterruptedIOException ("Cannot read. There is an open output stream for " + fo); // NOI18N
61
                } else {
62
                    try {
63
                        // wait a while and try again
64
                        StreamPool.class.wait (500);
65
                        continue;
66
                    } catch (InterruptedException ex) {
67
                        // throw the exception as IO one
68
                        throw new InterruptedIOException (ex.getMessage ());
69
                    }
70
                }
71
            } else {
72
                // escape the loop
73
                break;
74
            }
75
        }
76
      
56
        get (fo).iStream ().add (retVal);
77
        get (fo).iStream ().add (retVal);
57
        get (fo.getFileSystem()).iStream ().add (retVal);
78
        get (fo.getFileSystem()).iStream ().add (retVal);
58
        return retVal;
79
        return retVal;
Lines 245-250 Link Here
245
                super.out.flush();
266
                super.out.flush();
246
                super.close ();
267
                super.close ();
247
                closeOutputStream (fo, this, fireFileChanged);
268
                closeOutputStream (fo, this, fireFileChanged);
269
            }
270
            synchronized (StreamPool.class) {
271
                StreamPool.class.notifyAll ();
248
            }
272
            }
249
        }
273
        }
250
        public Exception getException () {
274
        public Exception getException () {
(-)test/unit/src/org/openide/filesystems/FileObjectTestHid.java (+99 lines)
Lines 241-246 Link Here
241
        }
241
        }
242
    }
242
    }
243
    
243
    
244
    
245
    /** Test whether the read is forbiden while somebody is writing
246
     */
247
    public void  testWriteReadExclusion () throws Exception {
248
        assertTrue ("Succeeds to read the content", doWriteReadAtOnce (false));
249
    }
250
    
251
    public void  testWriteReadExclusionDeadlock () throws Exception {
252
        assertFalse ("Receives an exception", doWriteReadAtOnce (true));
253
    }
254
    
255
    /** Starts two threads one tries to write and blocks for a while
256
     * the other tries to read meanwhile. If deadlockInWrite than the write
257
     * is stopped indefinitelly.
258
     * @return false if we got an InterruptedIOException or true is we 
259
     *   succeeded to read fully
260
     */
261
    private boolean doWriteReadAtOnce (final boolean deadlockInWrite) throws Exception {
262
        checkSetUp();
263
        FileObject fold = getTestFolder1(root);
264
        final FileObject fo1 = getTestFile1(fold);
265
        
266
        String testStr = "text";
267
        
268
        try {
269
            writeStr(fo1, testStr);
270
        } catch (IOException iex) {
271
            fsAssert(
272
                "expected move will success on writable FS",
273
                fs.isReadOnly() || fo1.isReadOnly()
274
            );
275
            // to make the conditions pass
276
            return !deadlockInWrite;
277
        }
278
279
        
280
        class DoWorkInOtherThread implements Runnable {
281
            Exception ex;
282
            
283
            public void run () {
284
                try {
285
                    FileLock lock = fo1.lock();
286
                    
287
                    OutputStream os = fo1.getOutputStream (lock);
288
                    os.write (1);
289
                    os.write (2);
290
                    os.flush ();
291
                   
292
                    synchronized (this) {
293
                        notify ();
294
                        // block for a while so the other thread
295
                        // can do the partial read
296
                        wait (deadlockInWrite ? 0 : 1000);
297
                    }
298
                    
299
                    os.write (3);
300
                    os.write (4);
301
                    os.close ();
302
303
                    lock.releaseLock();
304
                } catch (Exception ex) {
305
                    this.ex = ex;
306
                }
307
            }
308
            
309
            public boolean executeDirectly () throws Exception {
310
                RequestProcessor.Task task;
311
                synchronized (this) {
312
                    task = new RequestProcessor ("Writes with delay").post (this);
313
                    wait ();
314
                }
315
                
316
                try {
317
                    InputStream is = fo1.getInputStream ();
318
                    byte[] arr = new byte[200];
319
                    int len = is.read (arr);
320
                    assertEquals ("Read all four bytes", 4, len);
321
                    for (int i = 1; i <= 4; i++) {
322
                        assertEquals (i + " th byte is " + i, i, arr[i - 1]);
323
                    }
324
                    return true;
325
                } catch (InterruptedIOException ex) {
326
                    // InterruptedIOException is fine, means we could not read the stream
327
                    return false;
328
                } finally {
329
                    synchronized (this) {
330
                        // let the writer thread finish
331
                        notifyAll ();
332
                    }
333
                    task.waitFinished ();
334
                }
335
            }
336
        }
337
            
338
339
        DoWorkInOtherThread d = new DoWorkInOtherThread ();
340
        return d.executeDirectly ();
341
    }
342
    
244
    public void  testGetPath1() {
343
    public void  testGetPath1() {
245
        checkSetUp();
344
        checkSetUp();
246
        FileObject fold1 = getTestFolder1(root);
345
        FileObject fold1 = getTestFolder1(root);

Return to bug 40738