View Javadoc
1   /*
2    * Copyright 2018 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.spf4j.test.log;
17  
18  import edu.umd.cs.findbugs.annotations.CleanupObligation;
19  import edu.umd.cs.findbugs.annotations.DischargesObligation;
20  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21  import java.util.concurrent.TimeUnit;
22  import org.spf4j.base.TimeSource;
23  
24  /**
25   * @author Zoltan Farkas
26   */
27  @CleanupObligation
28  public interface ObservationAssert {
29  
30    /**
31     * Assert that a sequence of messages has not been seen.
32     * also unregisters this assertion handler.
33     */
34    @DischargesObligation
35    void assertObservation();
36  
37  
38    @SuppressFBWarnings("MDM_THREAD_YIELD")
39    @DischargesObligation
40    default void assertObservation(final long time, final TimeUnit tu) throws InterruptedException {
41      long deadline = TimeSource.nanoTime() + tu.toNanos(time);
42      AssertionError rae;
43      do  {
44        try {
45          assertObservation();
46          rae = null;
47        } catch (AssertionError ae) {
48          rae = ae;
49        }
50        if (rae == null) {
51          return;
52        }
53        Thread.sleep(100);
54      } while (TimeSource.nanoTime() < deadline);
55      throw rae;
56    }
57  
58  }