jnotify - anything else to use instead of Thread.Sleep when file is detected

170 views Asked by At

I am really needing some help on this.

I have adopted the JNOTIFY approach to detecting any new files in a directory. When the file arrives the Listener informs that a new file is in the location.

  @BeforeTest(alwaysRun=true)
public void Polling() throws Exception {
    ListenToNotifications.checkFolderPickup();
}

I have attempted this where I addded a call to my Setup function in order to call my setup function after the file is detected.

            //snippet from Listener Class from checkFolderPickup();
            public void fileCreated(int wd, String rootPath, String name) {
       print("New File just created " + rootPath + " : " + name);
        Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
        try {
            BaseTest.setup();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

My question is //Thread.sleep(1000000) i feel this is not a safe approach and I wanted to know if there is any other approach that I could possibly use instead of a Thread.Sleep, because this function will have to be executed once each time a new file is available and the old file will be deleted eventually and so on, I cannot make the Sleep to short , it will just ignore and continue with Base.Setup()

public static void checkFolderPickup() throws Exception {
...removed other code
    
    boolean watchSubtree = true;
    int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
    //Thread.sleep(1000000);
    Thread.sleep(20000);
    boolean res = JNotify.removeWatch(watchID);
    if (!res) {
        // invalid watch ID specified.
    }

}

I basically need my framework to keep polling that directory and each time it will execute the base setup process and follow a workflow, delete the file then poll again and so on.

Can anyone please advise?

2

There are 2 answers

20
PDHide On

You don't need any other modules , you can use custom expected condition:## using:

import java.io.File;

define the method inside any pageobject class:

private ExpectedCondition<Boolean> newfilepresent() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            File f = new File("D:\\workplace"); 
            return f.listFiles().length>1;
        }
        
        @Override
        public String toString() {
          return String.format("wait for new file to be present within the time specified");
        }
    };
}

we created a custom expected condition method now use it as:

and in code wait like:

wait.until(pageobject.filepresent());

Output:

Failed:

enter image description here

Passed

enter image description here

0
Omry Yadan On

Once you register a watch on a directory with JNotify, it will continue to deliver events for files in that directory. You should not remove the watch if you wish to continue to receive events for that directory.