blob: 25e775364cbab40265de180c62130ba5c0fe6e50 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.masterfs;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.masterfs.filebasedfs.children.ChildrenSupportTest;
import org.netbeans.modules.masterfs.filebasedfs.fileobjects.TestUtils;
import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory;
import org.netbeans.modules.masterfs.filebasedfs.utils.FileChangedManager;
import org.netbeans.modules.masterfs.filebasedfs.utils.FileChangedManagerTest;
import org.openide.filesystems.FileChangeAdapter;
import org.openide.filesystems.FileEvent;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
public class SlowRefreshAndPriorityIOTest extends NbTestCase {
static {
// Just pre load the classes
FileChangedManagerTest.assertNoLock();
ChildrenSupportTest.assertNoLock();
System.setProperty("org.netbeans.modules.masterfs.watcher.disable", "true");
}
private Logger LOG;
private FileObject testFolder;
public SlowRefreshAndPriorityIOTest(String testName) {
super(testName);
}
@Override
protected Level logLevel() {
return Level.FINE;
}
@Override
protected void setUp() throws Exception {
System.getProperties().put("org.netbeans.modules.masterfs.watcher.disable", "true");
clearWorkDir();
LOG = Logger.getLogger("test." + getName());
Logger.getLogger("org.openide.util.Mutex").setUseParentHandlers(false);
File dir = new File(getWorkDir(), "test");
dir.mkdirs();
testFolder = FileUtil.toFileObject(dir);
assertNotNull("Test folder created", testFolder);
System.setSecurityManager(new FileChangedManager());
}
public void testRefreshCanBeSuspendedByPriorityIO() throws Exception {
long lm = System.currentTimeMillis();
LOG.info("starting testRefreshCanBeSuspended " + lm);
FileObject fileObject1 = testFolder.createData("fileObject1");
assertNotNull("Just to initialize the stamp", lm);
FileObject[] arr = testFolder.getChildren();
assertEquals("One child", 1, arr.length);
assertEquals("Right child", fileObject1, arr[0]);
File file = FileUtil.toFile(fileObject1);
assertNotNull("File found", file);
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1);
arr = null;
fileObject1 = null;
assertGC("File Object can disappear", ref);
class L extends FileChangeAdapter {
int cnt;
FileEvent event;
@Override
public void fileChanged(FileEvent fe) {
LOG.info("file change " + fe.getFile());
cnt++;
event = fe;
}
}
L listener = new L();
testFolder.addRecursiveListener(listener);
Thread.sleep(1000);
FileOutputStream os = new FileOutputStream(file);
os.write(10);
os.close();
if (lm > file.lastModified() - 50) {
fail("New modification time shall be at last 50ms after the original one: " + (file.lastModified() - lm));
}
Object obj = TestUtils.findSlowRefresh(testFolder);
assertNotNull("Refresh attribute found", obj);
assertTrue("It is instance of runnable: " + obj, obj instanceof Runnable);
Runnable r = (Runnable)obj;
class AE extends ActionEvent implements Runnable {
List<FileObject> files = new ArrayList<FileObject>();
boolean boosted;
boolean finished;
int goingIdle;
public AE() {
super("", 0, "");
}
@Override
public void setSource(Object newSource) {
LOG.log(Level.INFO, "Set source called: {0}", newSource);
assertTrue(newSource instanceof Object[]);
Object[] arr = (Object[])newSource;
assertTrue("Three elements at leat ", 3 <= arr.length);
assertTrue("first is int", arr[0] instanceof Integer);
assertTrue("2nd is int", arr[1] instanceof Integer);
assertTrue("3rd is fileobject", arr[2] instanceof FileObject);
files.add((FileObject)arr[2]);
super.setSource(newSource);
}
@Override
public void run() {
FileChangedManagerTest.assertNoLock();
ChildrenSupportTest.assertNoLock();
assertFalse("No naming factory lock", Thread.holdsLock(NamingFactory.class));
goingIdle++;
}
public synchronized void waitBoosted() throws Exception {
while (!boosted) {
wait();
}
}
public synchronized void notifyBoosted() {
boosted = true;
notifyAll();
}
}
final AE counter = new AE();
LOG.info("Posting AE into RP");
// starts 5s of disk checking
RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable() {
@Override
public void run() {
try {
FileChangedManager.priorityIO(new Callable<Void>() {
@Override
public Void call() throws Exception {
counter.notifyBoosted();
try {
Thread.sleep(5000);
counter.finished = true;
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
});
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
}
});
// connect together
r.equals(counter);
LOG.info("Waiting for I/O boost");
counter.waitBoosted();
LOG.info("Starting refresh");
// do the refresh
r.run();
LOG.info("Refresh finished");
assertTrue("Background I/O access needs to stop before we finish our task", counter.finished);
assertEquals("Change notified", 1, listener.cnt);
assertEquals("Right file", file, FileUtil.toFile(listener.event.getFile()));
assertEquals("Right source", file.getParentFile(), FileUtil.toFile((FileObject)listener.event.getSource()));
if (counter.goingIdle == 0) {
fail("The I/O subsystem shall notify the action that it went idle at least once");
}
}
}