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   */
25  public class SlowComponentClassicSingletonComponentManagerTest extends PlexusTestCase {
26      public void testThreads1() throws Exception {
27          test(1);
28      }
29  
30      /**
31       * Tests that multiple concurrent threads don't acquire different components.
32       * @throws Exception in case of an error.
33       */
34      public void testThreads1000() throws Exception {
35          test(1000);
36      }
37  
38      private void test(int count) throws Exception {
39          ComponentLookupThread components[] = new ComponentLookupThread[count];
40          // Create them
41          for (int i = 0; i < count; i++) {
42              components[i] = new ComponentLookupThread(getContainer());
43          }
44          // Start them
45          for (int i = 0; i < count; i++) {
46              components[i].start();
47          }
48  
49          // Wait for them to finish
50          for (int i = 0; i < count; i++) {
51              components[i].join(10000);
52          }
53  
54          // Get master component
55          SlowComponent masterComponent = lookup(SlowComponent.class);
56  
57          // Verify them
58          for (int i = 0; i < count; i++) {
59              assertSame(
60                      i + ":" + components[i].getComponent() + " == " + masterComponent,
61                      masterComponent,
62                      components[i].getComponent());
63          }
64      }
65  
66      class ComponentLookupThread extends Thread {
67          final PlexusContainer container;
68  
69          private SlowComponent component;
70  
71          public ComponentLookupThread(PlexusContainer container) {
72              /*
73               * NOTE: A high priority seems to increase the likelihood of exhibiting missing synchronization.
74               */
75              setPriority(MAX_PRIORITY);
76              this.container = container;
77          }
78  
79          public void run() {
80              try {
81                  //            DefaultPlexusContainer.setLookupRealm( lookupRealm );
82                  SlowComponent tmpComponent = container.lookup(SlowComponent.class);
83  
84                  synchronized (this) {
85                      this.component = tmpComponent;
86                  }
87              } catch (Exception e) {
88                  container.getLookupRealm().display();
89                  e.printStackTrace();
90              }
91          }
92  
93          public SlowComponent getComponent() {
94              synchronized (this) {
95                  return component;
96              }
97          }
98      }
99  }