1
2
3
4
5
6
7 package org.mule.tck.probe;
8
9
10
11
12
13 public class PollingProber implements Prober
14 {
15
16 public static final long DEFAULT_TIMEOUT = 1000;
17 public static final long DEFAULT_POLLING_INTERVAL = 100;
18
19 private final long timeoutMillis;
20 private final long pollDelayMillis;
21
22 public PollingProber() {
23 this(DEFAULT_TIMEOUT, DEFAULT_POLLING_INTERVAL);
24 }
25
26 public PollingProber(long timeoutMillis, long pollDelayMillis)
27 {
28 this.timeoutMillis = timeoutMillis;
29 this.pollDelayMillis = pollDelayMillis;
30 }
31
32 public void check(Probe probe)
33 {
34 if (!poll(probe))
35 {
36 throw new AssertionError(probe.describeFailure());
37 }
38 }
39
40 private boolean poll(Probe probe)
41 {
42 Timeout timeout = new Timeout(timeoutMillis);
43
44 while (true)
45 {
46 if (probe.isSatisfied())
47 {
48 return true;
49 }
50 else if (timeout.hasTimedOut())
51 {
52 return false;
53 }
54 else
55 {
56 waitFor(pollDelayMillis);
57 }
58 }
59 }
60
61 private void waitFor(long duration)
62 {
63 try
64 {
65 Thread.sleep(duration);
66 }
67 catch (InterruptedException e)
68 {
69 throw new IllegalStateException("unexpected interrupt", e);
70 }
71 }
72 }