1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4882798
  27  * @summary multi-thread test to exercise sync and contention for removes to transformer registry
  28  * @author Gabriel Adauto, Wily Technology
  29  *
  30  * @run build TransformerManagementThreadRemoveTests
  31  * @run shell MakeJAR.sh redefineAgent
  32  * @run main/othervm -javaagent:redefineAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
  33  */
  34 import java.util.*;
  35 
  36 public class TransformerManagementThreadRemoveTests
  37     extends TransformerManagementThreadAddTests
  38 {
  39 
  40     /**
  41      * Constructor for TransformerManagementThreadRemoveTests.
  42      * @param name
  43      */
  44     public TransformerManagementThreadRemoveTests(String name)
  45     {
  46         super(name);
  47     }
  48 
  49     public static void
  50     main (String[] args)
  51         throws Throwable {
  52         ATestCaseScaffold   test = new TransformerManagementThreadRemoveTests(args[0]);
  53         test.runTest();
  54     }
  55 
  56     protected final void
  57     doRunTest()
  58         throws Throwable {
  59         testMultiThreadAddsAndRemoves();
  60     }
  61 
  62     public void
  63     testMultiThreadAddsAndRemoves()
  64     {
  65         int size = TOTAL_THREADS + REMOVE_THREADS;
  66         ArrayList threadList = new ArrayList(size);
  67         for (int i = MIN_TRANS; i <= MAX_TRANS; i++)
  68         {
  69             int index = i - MIN_TRANS;
  70             threadList.add(new TransformerThread("Trans"+prettyNum(index,2), i));
  71         }
  72 
  73         int factor = (int)Math.floor(TOTAL_THREADS / REMOVE_THREADS);
  74         for (int i = 0; i < REMOVE_THREADS; i++)
  75         {
  76             threadList.add(factor * i, new RemoveThread("Remove"+i));
  77         }
  78 
  79         Thread[] threads = (Thread[])threadList.toArray(new Thread[size]);
  80         setExecThread(new ExecuteTransformersThread());
  81         getExecThread().start();
  82         for (int i = threads.length - 1; i >= 0; i--)
  83         {
  84             threads[i].start();
  85         }
  86 
  87         while (!testCompleted())
  88         {
  89             // Effective Java - Item 51: Don't depend on the thread scheduler
  90             // Use sleep() instead of yield().
  91             try {
  92                 Thread.sleep(500);
  93             } catch (InterruptedException ie) {
  94             }
  95         }
  96         assertTrue(finalCheck());
  97 
  98         //printTransformers();
  99     }
 100 
 101     /**
 102      * Method removeTransformer.
 103      */
 104     private void
 105     removeTransformer(Thread t)
 106     {
 107         ThreadTransformer tt = null;
 108 
 109         synchronized (fAddedTransformers)
 110         {
 111             int size = fAddedTransformers.size();
 112             if (size > 0)
 113             {
 114                 int choose = (int)Math.floor(Math.random() * size);
 115                 tt = (ThreadTransformer)fAddedTransformers.remove(choose);
 116             }
 117             //System.out.println("removed("+tt+") size("+size+") chose("+choose+") by("+t+")");
 118         }
 119 
 120         if (tt != null)
 121         {
 122             getInstrumentation().removeTransformer(tt);
 123         }
 124     }
 125 
 126     private class
 127     RemoveThread
 128         extends Thread
 129     {
 130 
 131         RemoveThread(String name)
 132         {
 133             super(name);
 134         }
 135 
 136         public void
 137         run()
 138         {
 139             while (!threadsDone())
 140             {
 141                 removeTransformer(RemoveThread.this);
 142             }
 143         }
 144     }
 145 
 146 }