1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.engine.process;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.verifyNoMoreInteractions;
31 import static org.mockito.Mockito.when;
32 import net.grinder.common.GrinderProperties;
33 import net.grinder.common.processidentity.WorkerIdentity;
34 import net.grinder.engine.agent.StubAgentIdentity;
35 import net.grinder.script.Barrier;
36 import net.grinder.script.InvalidContextException;
37 import net.grinder.script.SSLControl;
38 import net.grinder.script.Statistics;
39 import net.grinder.script.TestRegistry;
40 import net.grinder.synchronisation.BarrierGroup;
41 import net.grinder.synchronisation.BarrierGroups;
42 import net.grinder.synchronisation.messages.BarrierIdentity;
43 import net.grinder.testutility.Time;
44 import net.grinder.util.Sleeper;
45 import net.grinder.util.SleeperImplementation;
46 import net.grinder.util.StandardTimeAuthority;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.slf4j.Logger;
53
54
55
56
57
58
59
60 public class TestScriptContextImplementation {
61
62 @Mock private ThreadContext m_threadContext;
63 @Mock private Logger m_logger;
64 @Mock private ThreadStarter m_threadStarter;
65 @Mock private ThreadStopper m_threadStopper;
66 @Mock private Statistics m_statistics;
67 @Mock private SSLControl m_sslControl;
68 @Mock private TestRegistry m_testRegistry;
69 @Mock private BarrierGroups m_barrierGroups;
70 @Mock private BarrierGroup m_barrierGroup;
71 @Mock private BarrierIdentity.Factory m_identityGenerator;
72
73 @Before public void setUp() {
74 MockitoAnnotations.initMocks(this);
75
76 when(m_barrierGroup.getName()).thenReturn("MyBarrierGroup");
77 when(m_barrierGroups.getGroup("MyBarrierGroup"))
78 .thenReturn(m_barrierGroup);
79 }
80
81 @Test public void testConstructorAndGetters() throws Exception {
82
83 final GrinderProperties properties = new GrinderProperties();
84
85 final int threadNumber = 99;
86 final int runNumber = 3;
87 final StubThreadContextLocator threadContextLocator =
88 new StubThreadContextLocator();
89 threadContextLocator.set(m_threadContext);
90
91 when(m_threadContext.getThreadNumber()).thenReturn(threadNumber);
92 when(m_threadContext.getRunNumber()).thenReturn(runNumber);
93
94 final Sleeper sleeper = new SleeperImplementation(null, m_logger, 1, 0);
95
96 final StubAgentIdentity agentIdentity =
97 new StubAgentIdentity("Agent");
98 final WorkerIdentity workerIdentity = agentIdentity.createWorkerIdentity();
99 final WorkerIdentity firstWorkerIdentity =
100 agentIdentity.createWorkerIdentity();
101
102 final ScriptContextImplementation scriptContext =
103 new ScriptContextImplementation(
104 workerIdentity,
105 firstWorkerIdentity,
106 threadContextLocator,
107 properties,
108 m_logger,
109 sleeper,
110 m_sslControl,
111 m_statistics,
112 m_testRegistry,
113 m_threadStarter,
114 m_threadStopper,
115 m_barrierGroups,
116 m_identityGenerator);
117
118 assertEquals(workerIdentity.getName(), scriptContext.getProcessName());
119 assertEquals(workerIdentity.getNumber(),
120 scriptContext.getProcessNumber());
121 assertEquals(firstWorkerIdentity.getNumber(),
122 scriptContext.getFirstProcessNumber());
123 assertEquals(threadNumber, scriptContext.getThreadNumber());
124 assertEquals(runNumber, scriptContext.getRunNumber());
125 assertSame(m_logger, scriptContext.getLogger());
126 assertSame(properties, scriptContext.getProperties());
127 assertSame(m_statistics, scriptContext.getStatistics());
128 assertSame(m_sslControl, scriptContext.getSSLControl());
129 assertSame(m_testRegistry, scriptContext.getTestRegistry());
130
131 threadContextLocator.set(null);
132 assertEquals(-1, scriptContext.getThreadNumber());
133 assertEquals(-1, scriptContext.getRunNumber());
134 assertEquals(m_statistics, scriptContext.getStatistics());
135
136 assertEquals(0, scriptContext.getProcessNumber());
137 assertEquals(-1, scriptContext.getAgentNumber());
138
139 agentIdentity.setNumber(10);
140 assertEquals(0, scriptContext.getProcessNumber());
141 assertEquals(10, scriptContext.getAgentNumber());
142
143 verifyNoMoreInteractions(m_threadStarter);
144
145 scriptContext.startWorkerThread();
146 verify(m_threadStarter).startThread(any());
147
148 final Object testRunner = new Object();
149 scriptContext.startWorkerThread(testRunner);
150 verify(m_threadStarter).startThread(testRunner);
151 verifyNoMoreInteractions(m_threadStarter);
152
153 scriptContext.stopWorkerThread(10);
154 verify(m_threadStopper).stopThread(10);
155 verifyNoMoreInteractions(m_threadStopper);
156 }
157
158 @Test public void testSleep() throws Exception {
159
160 final Sleeper sleeper =
161 new SleeperImplementation(new StandardTimeAuthority(),
162 m_logger,
163 1,
164 0);
165
166 final ScriptContextImplementation scriptContext =
167 new ScriptContextImplementation(
168 null, null, null, null, null, sleeper, null, null, null, null,
169 null, null, null);
170
171 assertTrue(
172 new Time(50, 70) {
173 public void doIt() throws Exception { scriptContext.sleep(50); }
174 }.run());
175
176 assertTrue(
177 new Time(40, 70) {
178 public void doIt() throws Exception { scriptContext.sleep(50, 5); }
179 }.run());
180 }
181
182 @Test public void testStopThisWorkerThread() throws Exception {
183 final StubThreadContextLocator threadContextLocator =
184 new StubThreadContextLocator();
185
186 final ScriptContextImplementation scriptContext =
187 new ScriptContextImplementation(
188 null, null, threadContextLocator, null, null, null, null, null, null,
189 null, null, null, null);
190
191 try {
192 scriptContext.stopThisWorkerThread();
193 fail("Expected InvalidContextException");
194 }
195 catch (InvalidContextException e) {
196 }
197
198 threadContextLocator.set(m_threadContext);
199
200 try {
201 scriptContext.stopThisWorkerThread();
202 fail("Expected ShutdownException");
203 }
204 catch (ShutdownException e) {
205 }
206 }
207
208 @Test public void testBarrier() throws Exception {
209 final ScriptContextImplementation scriptContext =
210 new ScriptContextImplementation(
211 null, null, null, null, null, null, null, null, null,
212 null, null, m_barrierGroups, m_identityGenerator);
213
214 final Barrier globalBarrier = scriptContext.barrier("MyBarrierGroup");
215 assertEquals("MyBarrierGroup", globalBarrier.getName());
216
217 verify(m_identityGenerator).next();
218 }
219 }