jdk/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java
changeset 2 90ce3da70b43
child 271 696ff142e6e6
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2004 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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  * @ignore Disabled until race condition which hangs test can be fixed.
       
    31  * @run build TransformerManagementThreadRemoveTests
       
    32  * @run shell MakeJAR.sh basicAgent
       
    33  * @run main/othervm -javaagent:basicAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
       
    34  */
       
    35 import java.util.*;
       
    36 
       
    37 public class TransformerManagementThreadRemoveTests
       
    38     extends TransformerManagementThreadAddTests
       
    39 {
       
    40 
       
    41     /**
       
    42      * Constructor for TransformerManagementThreadRemoveTests.
       
    43      * @param name
       
    44      */
       
    45     public TransformerManagementThreadRemoveTests(String name)
       
    46     {
       
    47         super(name);
       
    48     }
       
    49 
       
    50     public static void
       
    51     main (String[] args)
       
    52         throws Throwable {
       
    53         ATestCaseScaffold   test = new TransformerManagementThreadRemoveTests(args[0]);
       
    54         test.runTest();
       
    55     }
       
    56 
       
    57     protected final void
       
    58     doRunTest()
       
    59         throws Throwable {
       
    60         testMultiThreadAddsAndRemoves();
       
    61     }
       
    62 
       
    63     public void
       
    64     testMultiThreadAddsAndRemoves()
       
    65     {
       
    66         int size = TOTAL_THREADS + REMOVE_THREADS;
       
    67         ArrayList threadList = new ArrayList(size);
       
    68         for (int i = MIN_TRANS; i <= MAX_TRANS; i++)
       
    69         {
       
    70             int index = i - MIN_TRANS;
       
    71             threadList.add(new TransformerThread("Trans"+prettyNum(index,2), i));
       
    72         }
       
    73 
       
    74         int factor = (int)Math.floor(TOTAL_THREADS / REMOVE_THREADS);
       
    75         for (int i = 0; i < REMOVE_THREADS; i++)
       
    76         {
       
    77             threadList.add(factor * i, new RemoveThread("Remove"+i));
       
    78         }
       
    79 
       
    80         Thread[] threads = (Thread[])threadList.toArray(new Thread[size]);
       
    81         setExecThread(new ExecuteTransformersThread());
       
    82         getExecThread().start();
       
    83         for (int i = threads.length - 1; i >= 0; i--)
       
    84         {
       
    85             threads[i].start();
       
    86         }
       
    87 
       
    88         while (!testCompleted())
       
    89         {
       
    90             Thread.currentThread().yield();
       
    91         }
       
    92         assertTrue(finalCheck());
       
    93 
       
    94         //printTransformers();
       
    95     }
       
    96 
       
    97     /**
       
    98      * Method removeTransformer.
       
    99      */
       
   100     private void
       
   101     removeTransformer(Thread t)
       
   102     {
       
   103         ThreadTransformer tt = null;
       
   104 
       
   105         synchronized (fAddedTransformers)
       
   106         {
       
   107             int size = fAddedTransformers.size();
       
   108             if (size > 0)
       
   109             {
       
   110                 int choose = (int)Math.floor(Math.random() * size);
       
   111                 tt = (ThreadTransformer)fAddedTransformers.remove(choose);
       
   112             }
       
   113             //System.out.println("removed("+tt+") size("+size+") chose("+choose+") by("+t+")");
       
   114         }
       
   115 
       
   116         if (tt != null)
       
   117         {
       
   118             getInstrumentation().removeTransformer(tt);
       
   119         }
       
   120     }
       
   121 
       
   122     private class
       
   123     RemoveThread
       
   124         extends Thread
       
   125     {
       
   126 
       
   127         RemoveThread(String name)
       
   128         {
       
   129             super(name);
       
   130         }
       
   131 
       
   132         public void
       
   133         run()
       
   134         {
       
   135             while (!threadsDone())
       
   136             {
       
   137                 removeTransformer(RemoveThread.this);
       
   138             }
       
   139         }
       
   140     }
       
   141 
       
   142 }