1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.spf4j.recyclable.impl;
33
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35 import java.io.Closeable;
36 import java.io.IOException;
37 import java.nio.channels.ClosedChannelException;
38 import java.util.concurrent.atomic.AtomicInteger;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47 @SuppressFBWarnings("PREDICTABLE_RANDOM")
48 public final class ExpensiveTestObject implements Closeable {
49
50 private static final Logger LOG = LoggerFactory.getLogger(ExpensiveTestObject.class);
51
52 private static final AtomicInteger OBJ_COUNT = new AtomicInteger();
53
54 private static volatile boolean failAll = false;
55
56 private final long maxIdleMillis;
57 private final int nrUsesToFailAfter;
58 private final long minOperationMillis;
59 private final long maxOperationMillis;
60 private long lastTouchedTimeMillis;
61 private int nrUses;
62 private final String id;
63
64 public static boolean isFAILALL() {
65 return failAll;
66 }
67
68 public static void setFailAll(final boolean failAll) {
69 ExpensiveTestObject.failAll = failAll;
70 }
71
72 @SuppressFBWarnings("STT_TOSTRING_STORED_IN_FIELD")
73 public ExpensiveTestObject(final long maxIdleMillis, final int nrUsesToFailAfter,
74 final long minOperationMillis, final long maxOperationMillis) {
75 this.maxIdleMillis = maxIdleMillis;
76 this.nrUsesToFailAfter = nrUsesToFailAfter;
77 this.minOperationMillis = minOperationMillis;
78 this.maxOperationMillis = maxOperationMillis;
79 lastTouchedTimeMillis = System.currentTimeMillis();
80 nrUses = 0;
81 simulateDoStuff(maxOperationMillis - minOperationMillis);
82 id = "Test Object " + OBJ_COUNT.getAndIncrement();
83 }
84
85 public void doStuff() throws IOException {
86 if (failAll) {
87 throw new IOExceptionImpl("Failall " + id);
88 }
89 long currentTime = System.currentTimeMillis();
90 if (currentTime - lastTouchedTimeMillis > maxIdleMillis) {
91 throw new ClosedChannelException();
92 }
93 if (nrUses > nrUsesToFailAfter) {
94 throw new IOExceptionImpl("Simulated random crap " + id);
95 }
96 simulateDoStuff(maxOperationMillis - minOperationMillis);
97 nrUses++;
98 lastTouchedTimeMillis = System.currentTimeMillis();
99 }
100
101 public void testObject() throws IOException {
102 LOG.debug("Testing object {}", id);
103 long currentTime = System.currentTimeMillis();
104 if (currentTime - lastTouchedTimeMillis > maxIdleMillis) {
105 throw new IOException("Connection closed " + id);
106 }
107 if (nrUses > nrUsesToFailAfter) {
108 throw new IOExceptionImpl("Simulated random crap " + id);
109 }
110 simulateDoStuff(0);
111 nrUses++;
112 lastTouchedTimeMillis = System.currentTimeMillis();
113 }
114
115 @Override
116 public void close() throws IOException {
117 doStuff();
118 }
119
120 @SuppressFBWarnings("MDM_THREAD_YIELD")
121 private void simulateDoStuff(final long time) {
122 long sleepTime = (long) (Math.random() * (time));
123 try {
124 Thread.sleep(minOperationMillis + sleepTime);
125 } catch (InterruptedException ex) {
126 throw new RuntimeException(ex);
127 }
128 }
129
130 @Override
131 public String toString() {
132 return "ExpensiveTestObject{" + "maxIdleMillis=" + maxIdleMillis + ", nrUsesToFailAfter="
133 + nrUsesToFailAfter + ", minOperationMillis=" + minOperationMillis + ", maxOperationMillis="
134 + maxOperationMillis + ", lastTouchedTimeMillis=" + lastTouchedTimeMillis + ", nrUses=" + nrUses
135 + ", id=" + id + '}';
136 }
137
138 private static final class IOExceptionImpl extends IOException {
139
140 IOExceptionImpl(final String message) {
141 super(message);
142 }
143
144 @Override
145 public Throwable fillInStackTrace() {
146 return this;
147 }
148 }
149
150 }