View Javadoc
1   package org.codehaus.plexus.component.manager;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.PlexusContainer;
20  import org.codehaus.plexus.PlexusTestCase;
21  
22  /**
23   * @author Ben Walding
24   * @version $Id$
25   */
26  public class SlowComponentClassicSingletonComponentManagerTest
27      extends PlexusTestCase
28  {
29      public void testThreads1()
30          throws Exception
31      {
32          test( 1 );
33      }
34  
35      /**
36       * Tests that multiple concurrent threads don't acquire different components.
37       */
38      public void testThreads1000()
39          throws Exception
40      {
41          test( 1000 );
42      }
43  
44      private void test( int count )
45          throws Exception
46      {
47          ComponentLookupThread components[] = new ComponentLookupThread[ count ];
48          //Create them
49          for ( int i = 0; i < count; i++ )
50          {
51              components[ i ] = new ComponentLookupThread( getContainer() );
52          }
53          //Start them
54          for ( int i = 0; i < count; i++ )
55          {
56              components[i].start();
57          }
58  
59          //Wait for them to finish
60          for ( int i = 0; i < count; i++ )
61          {
62              components[i].join( 10000 );
63          }
64  
65          //Get master component
66          SlowComponent masterComponent = lookup( SlowComponent.class );
67  
68          //Verify them
69          for ( int i = 0; i < count; i++ )
70          {
71              assertSame( i + ":" + components[i].getComponent() + " == " + masterComponent,
72                          masterComponent,
73                          components[i].getComponent() );
74          }
75      }
76  
77      class ComponentLookupThread
78          extends Thread
79      {
80          final PlexusContainer container;
81  
82          private SlowComponent component;
83  
84          public ComponentLookupThread( PlexusContainer container )
85          {
86              /*
87               * NOTE: A high priority seems to increase the likelihood of exhibiting missing synchronization.
88               */
89              setPriority( MAX_PRIORITY );
90              this.container = container;
91          }
92  
93          public void run()
94          {
95              try
96              {
97  //            DefaultPlexusContainer.setLookupRealm( lookupRealm );
98                  SlowComponent tmpComponent = container.lookup( SlowComponent.class );
99  
100                 synchronized ( this )
101                 {
102                     this.component = tmpComponent;
103                 }
104             }
105             catch ( Exception e )
106             {
107                 container.getLookupRealm().display();
108                 e.printStackTrace();
109             }
110         }
111 
112         public SlowComponent getComponent()
113         {
114             synchronized ( this )
115             {
116                 return component;
117             }
118         }
119     }    
120 }