View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
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   * This is a pooled object implementation, that behaves like a connection object.
44   *
45   * @author zoly
46   */
47  @SuppressFBWarnings("PREDICTABLE_RANDOM") //not security related
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 }