Uploaded image for project: 'HBase'
  1. HBase
  2. HBASE-7384

Introducing waitForCondition function into test cases

    XMLWordPrintableJSON

Details

    • Test
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 0.95.0
    • test
    • Reviewed

    Description

      Recently I'm working on flaky test cases and found we have many places using while loop and sleep to wait for a condition to be true. There are several issues in existing ways:

      1) Many similar code doing the same thing
      2) When time out happens, different errors are reported without explicitly indicating a time out situation
      3) When we want to increase the max timeout value to verify if a test case fails due to a not-enough time out value, we have to recompile & redeploy code

      I propose to create a waitForCondition function as a test utility function like the following:

          public interface WaitCheck {
              public boolean Check() ;
          }
      
          public boolean waitForCondition(int timeOutInMilliSeconds, int checkIntervalInMilliSeconds, WaitCheck s)
                  throws InterruptedException {
      
              int multiplier = 1;
              String multiplierProp = System.getProperty("extremeWaitMultiplier");
              if(multiplierProp != null) {
                  multiplier = Integer.parseInt(multiplierProp);
                  if(multiplier < 1) {
                      LOG.warn(String.format("Invalid extremeWaitMultiplier property value:%s. is ignored.", multiplierProp));
                      multiplier = 1;
                  }
              }
      
              int timeElapsed = 0;
              while(timeElapsed < timeOutInMilliSeconds * multiplier) {
                  if(s.Check()) {
                      return true;
                  }
                  Thread.sleep(checkIntervalInMilliSeconds);
                  timeElapsed += checkIntervalInMilliSeconds;
              }
              assertTrue("WaitForCondition failed due to time out(" + timeOutInMilliSeconds + " milliseconds expired)",
                      false);
      
              return false;
          }
      

      By doing the above way, there are several advantages:

      1) Clearly report time out error when such situation happens
      2) Use System property extremeWaitMultiplier to increase max time out dynamically for a quick verification
      3) Standardize current wait situations

      Pleas let me know what your thoughts on this.

      Thanks,
      -Jeffrey

      Attachments

        1. Waiter.java
          6 kB
          Alejandro Abdelnur
        2. hbase-7384.patch
          15 kB
          Jeffrey Zhong
        3. hbase-7384_2.4.patch
          16 kB
          Jeffrey Zhong
        4. hbase-7384_1.0.patch
          15 kB
          Jeffrey Zhong

        Activity

          People

            jeffreyz Jeffrey Zhong
            jeffreyz Jeffrey Zhong
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: