Home | History | Annotate | Download | only in verification
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.verification;
      6 
      7 import org.mockito.exceptions.base.MockitoAssertionError;
      8 import org.mockito.internal.verification.api.VerificationData;
      9 import org.mockito.verification.VerificationMode;
     10 
     11 public class VerificationWithTimeoutImpl {
     12 
     13     VerificationMode delegate;
     14     int timeout;
     15     int treshhold;
     16 
     17     public VerificationWithTimeoutImpl(int treshhold, int millis, VerificationMode delegate) {
     18         this.treshhold = treshhold;
     19         this.timeout = millis;
     20         this.delegate = delegate;
     21     }
     22 
     23     public void verify(VerificationData data) {
     24         int soFar = 0;
     25         MockitoAssertionError error = null;
     26         while (soFar <= timeout) {
     27             try {
     28                 delegate.verify(data);
     29                 return;
     30             } catch (MockitoAssertionError e) {
     31                 error = e;
     32                 soFar += treshhold;
     33                 sleep(treshhold);
     34             }
     35         }
     36         if (error != null) {
     37             throw error;
     38         }
     39     }
     40 
     41     void sleep(int sleep) {
     42         try {
     43             Thread.sleep(sleep);
     44         } catch (InterruptedException ie) {
     45             // oups. not much luck.
     46         }
     47     }
     48 
     49     public VerificationMode getDelegate() {
     50         return delegate;
     51     }
     52 
     53     public int getTimeout() {
     54         return timeout;
     55     }
     56 
     57     public int getTreshhold() {
     58         return treshhold;
     59     }
     60 }