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 java.util.concurrent.TimeUnit;
19  import org.hamcrest.Matcher;
20  import org.spf4j.base.TimeSource;
21  
22  /**
23   * @author Zoltan Farkas
24   */
25  abstract class UncaughtExceptionAsserter implements LogAssert, UncaughtExceptionConsumer {
26  
27    private final Matcher<UncaughtExceptionDetail> matcher;
28  
29    private final Object sync;
30  
31    private int seen;
32  
33    private final long timeout;
34    private final TimeUnit unit;
35  
36    UncaughtExceptionAsserter(final long timeout, final TimeUnit unit, final Matcher<UncaughtExceptionDetail> matcher) {
37      this.matcher = matcher;
38      this.seen = 0;
39      this.sync = new Object();
40      this.timeout = timeout;
41      this.unit = unit;
42    }
43  
44    @Override
45    public void assertObservation() {
46      long deadline = TimeSource.nanoTime() + unit.toNanos(timeout);
47      try {
48        synchronized (sync) {
49          while (seen == 0) {
50            long nanosToDeadline = deadline - TimeSource.nanoTime();
51            if (nanosToDeadline <= 0) {
52                throw new AssertionError("Not seen uncaught exception matching " + matcher);
53            }
54            TimeUnit.NANOSECONDS.timedWait(sync, nanosToDeadline);
55          }
56        }
57      } catch (InterruptedException ex) {
58        throw new AssertionError("Assertion interupted " + this, ex);
59      }
60    }
61  
62  
63    @Override
64    public boolean offer(final UncaughtExceptionDetail exDetail) {
65      synchronized (sync) {
66        if (matcher.matches(exDetail)) {
67          seen++;
68          sync.notifyAll();
69          return true;
70        }
71        return false;
72      }
73    }
74  
75    @Override
76    public String toString() {
77      return "UncaughtExceptionAsserter{" + "matcher=" + matcher + '}';
78    }
79  
80  }