Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.googlecode.junittoolbox;
  
  import  javax.annotation.Nonnull;
  import java.util.List;
Helper class to wait for asynchronous operations. Usage example:
     private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS)
                                                 .pollEvery(100, MILLISECONDS);
     @Test
     public void test_auto_complete() throws Exception {
         // Enter "cheese" into auto complete field ...
         ...
         wait.until(new RunnableAssert("'cheesecake' is displayed in auto-complete <div>") {
             @Override
             public void run() throws Exception {
                 WebElement autoCompleteDiv = driver.findElement(By.id("auto-complete"));
                 assertThat(autoCompleteDiv, isVisible());
                 assertThat(autoCompleteDiv, containsText("cheesecake"));
             }
         });
     }
 
 
 public class PollingWait {
 
     private long timeoutMillis = 30000;
     private long pollIntervalMillis = 50;

    
Default: 30 seconds.
 
     public PollingWait timeoutAfter(long timeAmount, @Nonnull TimeUnit timeUnit) {
         if (timeAmount <= 0) {
             throw new IllegalArgumentException("Invalid timeAmount: " + timeAmount + " -- must be greater than 0");
         }
          = timeUnit.toMillis(timeAmount);
         return this;
     }

    
Default: 50 milliseconds.
 
     public PollingWait pollEvery(long timeAmount, @Nonnull TimeUnit timeUnit) {
         if (timeAmount <= 0) {
             throw new IllegalArgumentException("Invalid timeAmount: " + timeAmount + " -- must be greater than 0");
         }
          = timeUnit.toMillis(timeAmount);
         return this;
     }

    
Repetitively executes the given runnableAssert until it succeeds without throwing an Error or Exception or until the configured timeout is reached, in which case an AssertionError will be thrown. Calls Thread.sleep before each retry using the configured interval to free the CPU for other threads/processes.
 
     public void until(@Nonnull RunnableAssert runnableAssert) {
         List<Throwableerrors = new ArrayList<Throwable>();
         long startTime = System.currentTimeMillis();
         long timeoutReached = startTime + ;
         boolean success = false;
         do {
             try {
                 runnableAssert.run();
                 success = true;
             } catch (Throwable t) {
                 if (errors.size() > 0 && startTime > timeoutReached) {
                     final StringBuilder sb = new StringBuilder();
                     sb.append(runnableAssert);
                     sb.append(" did not succeed within ");
                     appendNiceDuration(sb);
                     appendErrors(sberrorst);
                     throw new AssertionError(sb.toString());
                 }
                 if (errors.size() < 2) {
                     errors.add(t);
                 }
                 long sleepTime =  - (System.currentTimeMillis() - startTime);
                 if (sleepTime > 0) {
                     sleep(sleepTime);
                 }
                 startTime = System.currentTimeMillis();
             }
         } while (!success);
     }
 
     private void appendNiceDuration(StringBuilder sblong millis) {
         if (millis % 60000 == 0 && millis > 60000) {
             sb.append(millis / 60000).append(" minutes");
             return;
         }
        if (millis % 1000 == 0 && millis > 1000) {
            sb.append(millis / 1000).append(" seconds");
            return;
        }
        sb.append(millis).append(" ms");
    }
    private static final String EXCEPTION_SEPARATOR = "\n\t______________________________________________________________________\n";
    private void appendErrors(StringBuilder sbList<ThrowableerrorsThrowable lastError) {
        sb.append();
        sb.append("\t1st error: ");
        StringWriter sw = new StringWriter();
        errors.get(0).printStackTrace(new PrintWriter(sw));
        sb.append(sw.toString().replace("\n""\n\t").trim());
        if (errors.size() >= 2) {
            sb.append();
            sb.append("\t2nd error: ");
            sw = new StringWriter();
            errors.get(1).printStackTrace(new PrintWriter(sw));
            sb.append(sw.toString().replace("\n""\n\t").trim());
        }
        sb.append();
        sb.append("\tlast error: ");
        sw = new StringWriter();
        lastError.printStackTrace(new PrintWriter(sw));
        sb.append(sw.toString().replace("\n""\n\t").trim());
        sb.append();
    }

    
Internal method, package private for testing.
    void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException("Got interrupted."e);
        }
    }
New to GrepCode? Check out our FAQ X