public class SIPEventInterceptorTest extends TestCase{
static int count;
static int stuck = 50;
public void testCallAnalyzerConcurrencyAndLeaks() throws Exception {
ExecutorService ex = Executors.newFixedThreadPool(400);
final CallAnalyzer tp = new CallAnalyzer();
final MetricReference sec = new MetricReference("sec");
MetricReference se1c = new MetricReference("se111c");
tp.configure(sec, new MetricAnalysisConfiguration(10,500,500));
tp.startAnalysis(sec);
tp.startAnalysis(se1c);
Runnable r = new Runnable() {
public void run() {
tp.enter(sec);
try {
if(++count % 10000==0) {
System.out.println("Avg " + tp.getMetricStats(sec).averageTime);
Thread.sleep(1000);
}
Thread.sleep(stuck);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tp.leave(sec);
}
};
for(int q=0; q<200000; q++) {
ex.execute(r);
}
System.out.println("size:" + tp.getNumberOfThreads());
Thread.sleep(5000);
ex.shutdown();
ex.awaitTermination(60, TimeUnit.SECONDS);
ex.shutdownNow();
ex = null;
System.gc();
System.gc();Thread.sleep(1000);
System.out.println("size:" + tp.getNumberOfThreads());
RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = RuntimemxBean.getInputArguments();
for(String arg : arguments) {
System.out.println(arg);
}
assertTrue(Math.abs(tp.getMetricStats(sec).averageTime - stuck)<10); // wrong avg time
//assertEquals(0, tp.getNumberOfThreads()); // Memory leaking
System.out.println("Active threads " + tp.getNumberOfThreads());
}