1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }