1 package org.codehaus.plexus.component.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.codehaus.plexus.PlexusContainer;
20 import org.codehaus.plexus.PlexusTestCase;
21
22
23
24
25 public class SlowComponentClassicSingletonComponentManagerTest extends PlexusTestCase {
26 public void testThreads1() throws Exception {
27 test(1);
28 }
29
30
31
32
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
41 for (int i = 0; i < count; i++) {
42 components[i] = new ComponentLookupThread(getContainer());
43 }
44
45 for (int i = 0; i < count; i++) {
46 components[i].start();
47 }
48
49
50 for (int i = 0; i < count; i++) {
51 components[i].join(10000);
52 }
53
54
55 SlowComponent masterComponent = lookup(SlowComponent.class);
56
57
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
74
75 setPriority(MAX_PRIORITY);
76 this.container = container;
77 }
78
79 public void run() {
80 try {
81
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 }