Skip to content

Commit 0d20877

Browse files
author
Andy Adamczak
committed
Added multi-threaded tests.
1 parent 167d7cb commit 0d20877

File tree

5 files changed

+250
-69
lines changed

5 files changed

+250
-69
lines changed

PureMVC/Tests/Core/ControllerTest.cs

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Your reuse is governed by the Creative Commons Attribution 3.0 License
55
*/
66
using System;
7+
using System.Threading;
8+
using System.Collections.Generic;
79

810
using NUnitLite;
911
using NUnit.Framework;
@@ -43,6 +45,7 @@ public static ITest Suite
4345
ts.AddTest(new ControllerTest("TestRegisterAndRemoveCommand"));
4446
ts.AddTest(new ControllerTest("TestHasCommand"));
4547
ts.AddTest(new ControllerTest("TestReregisterAndExecuteCommand"));
48+
ts.AddTest(new ControllerTest("TestMultiThreadedOperations"));
4649

4750
return ts;
4851
}
@@ -81,11 +84,12 @@ public void TestRegisterAndExecuteCommand()
8184
{
8285
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
8386
IController controller = Controller.Instance;
84-
controller.RegisterCommand("ControllerTest", typeof(ControllerTestCommand));
87+
string name = "ControllerTest" + Thread.CurrentThread.Name;
88+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
8589

8690
// Create a 'ControllerTest' note
8791
ControllerTestVO vo = new ControllerTestVO(12);
88-
INotification note = new Notification("ControllerTest", vo);
92+
INotification note = new Notification(name, vo);
8993

9094
// Tell the controller to execute the Command associated with the note
9195
// the ControllerTestCommand invoked will multiply the vo.input value
@@ -108,11 +112,12 @@ public void TestRegisterAndRemoveCommand()
108112

109113
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
110114
IController controller = Controller.Instance;
111-
controller.RegisterCommand("ControllerRemoveTest", typeof(ControllerTestCommand));
115+
string name = "ControllerRemoveTest" + Thread.CurrentThread.Name;
116+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
112117

113118
// Create a 'ControllerTest' note
114119
ControllerTestVO vo = new ControllerTestVO(12);
115-
INotification note = new Notification("ControllerRemoveTest", vo);
120+
INotification note = new Notification(name, vo);
116121

117122
// Tell the controller to execute the Command associated with the note
118123
// the ControllerTestCommand invoked will multiply the vo.input value
@@ -126,7 +131,7 @@ public void TestRegisterAndRemoveCommand()
126131
vo.result = 0;
127132

128133
// Remove the Command from the Controller
129-
controller.RemoveCommand("ControllerRemoveTest");
134+
controller.RemoveCommand(name);
130135

131136
// Tell the controller to execute the Command associated with the
132137
// note. This time, it should not be registered, and our vo result
@@ -144,16 +149,17 @@ public void TestRegisterAndRemoveCommand()
144149
public void TestHasCommand() {
145150
// register the ControllerTestCommand to handle 'hasCommandTest' notes
146151
IController controller = Controller.Instance;
147-
controller.RegisterCommand("hasCommandTest", typeof(ControllerTestCommand));
152+
string name = "HasCommandTest" + Thread.CurrentThread.Name;
153+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
148154

149155
// test that hasCommand returns true for hasCommandTest notifications
150-
Assert.True(controller.HasCommand("hasCommandTest") == true, "Expecting controller.HasCommand('hasCommandTest') == true");
156+
Assert.True(controller.HasCommand(name) == true, "Expecting controller.HasCommand(name) == true");
151157

152158
// Remove the Command from the Controller
153-
controller.RemoveCommand("hasCommandTest");
159+
controller.RemoveCommand(name);
154160

155161
// test that hasCommand returns false for hasCommandTest notifications
156-
Assert.True(controller.HasCommand("hasCommandTest") == false, "Expecting controller.HasCommand('hasCommandTest') == false");
162+
Assert.True(controller.HasCommand(name) == false, "Expecting controller.HasCommand(name) == false");
157163
}
158164

159165
/**
@@ -172,17 +178,18 @@ public void TestReregisterAndExecuteCommand() {
172178

173179
// Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes
174180
IController controller = Controller.Instance;
175-
controller.RegisterCommand("ControllerTest2", typeof(ControllerTestCommand2));
181+
string name = "ControllerTest2" + Thread.CurrentThread.Name;
182+
controller.RegisterCommand(name, typeof(ControllerTestCommand2));
176183

177184
// Remove the Command from the Controller
178-
controller.RemoveCommand("ControllerTest2");
185+
controller.RemoveCommand(name);
179186

180187
// Re-register the Command with the Controller
181-
controller.RegisterCommand("ControllerTest2", typeof(ControllerTestCommand2));
188+
controller.RegisterCommand(name, typeof(ControllerTestCommand2));
182189

183190
// Create a 'ControllerTest2' note
184191
ControllerTestVO vo = new ControllerTestVO(12);
185-
Notification note = new Notification( "ControllerTest2", vo );
192+
Notification note = new Notification(name, vo);
186193

187194
// retrieve a reference to the View.
188195
IView view = View.Instance;
@@ -199,7 +206,50 @@ public void TestReregisterAndExecuteCommand() {
199206

200207
// if the command is executed twice the value will be 48
201208
Assert.True(vo.result == 48, "Expecting vo.result == 48");
202-
203209
}
204-
}
210+
211+
/// <summary>
212+
/// Test all of the function above using many threads at once.
213+
/// </summary>
214+
public void TestMultiThreadedOperations()
215+
{
216+
count = 20;
217+
IList<Thread> threads = new List<Thread>();
218+
219+
for (int i = 0; i < count; i++) {
220+
Thread t = new Thread(new ThreadStart(MultiThreadedTestFunction));
221+
t.Name = "ControllerTest" + i;
222+
threads.Add(t);
223+
}
224+
225+
foreach (Thread t in threads)
226+
{
227+
t.Start();
228+
}
229+
230+
while (true)
231+
{
232+
if (count <= 0) break;
233+
Thread.Sleep(100);
234+
}
235+
}
236+
237+
private int count = 0;
238+
239+
private int threadIterationCount = 10000;
240+
241+
private void MultiThreadedTestFunction()
242+
{
243+
for (int i = 0; i < threadIterationCount; i++)
244+
{
245+
// All we need to do is test the registration and removal of commands.
246+
TestRegisterAndExecuteCommand();
247+
TestRegisterAndRemoveCommand();
248+
TestHasCommand();
249+
TestReregisterAndExecuteCommand();
250+
}
251+
252+
count--;
253+
}
254+
}
205255
}

PureMVC/Tests/Core/ModelTest.cs

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Your reuse is governed by the Creative Commons Attribution 3.0 License
55
*/
66
using System;
77
using System.Collections.Generic;
8+
using System.Threading;
89

910
using NUnitLite;
1011
using NUnit.Framework;
@@ -44,6 +45,7 @@ public static ITest Suite
4445
ts.AddTest(new ModelTest("TestRegisterAndRemoveProxy"));
4546
ts.AddTest(new ModelTest("TestHasProxy"));
4647
ts.AddTest(new ModelTest("TestOnRegisterAndOnRemove"));
48+
ts.AddTest(new ModelTest("TestMultiThreadedOperations"));
4749

4850
return ts;
4951
}
@@ -75,8 +77,9 @@ public void TestRegisterAndRetrieveProxy()
7577
{
7678
// register a proxy and retrieve it.
7779
IModel model = Model.Instance;
78-
model.RegisterProxy(new Proxy("colors", new List<string>(new string[] { "red", "green", "blue" })));
79-
IProxy proxy = model.RetrieveProxy("colors");
80+
string name = "colors" + Thread.CurrentThread.Name;
81+
model.RegisterProxy(new Proxy(name, new List<string>(new string[] { "red", "green", "blue" })));
82+
IProxy proxy = model.RetrieveProxy(name);
8083
List<string> data = (List<string>) proxy.Data;
8184

8285
// test assertions
@@ -95,13 +98,14 @@ public void TestRegisterAndRemoveProxy()
9598
{
9699
// register a proxy, remove it, then try to retrieve it
97100
IModel model = Model.Instance;
98-
model.RegisterProxy(new Proxy("sizes", new List<int>(new int[] { 7, 13, 21 })));
99-
100-
IProxy removedProxy = model.RemoveProxy("sizes");
101+
string name = "sizes" + Thread.CurrentThread.Name;
102+
model.RegisterProxy(new Proxy(name, new List<int>(new int[] { 7, 13, 21 })));
103+
104+
IProxy removedProxy = model.RemoveProxy(name);
101105

102-
Assert.True(removedProxy.ProxyName == "sizes", "Expecting removedProxy.ProxyName == 'sizes'");
106+
Assert.True(removedProxy.ProxyName == name, "Expecting removedProxy.ProxyName == name");
103107

104-
IProxy proxy = model.RetrieveProxy("sizes");
108+
IProxy proxy = model.RetrieveProxy(name);
105109

106110
// test assertions
107111
Assert.Null(proxy, "Expecting proxy is null");
@@ -114,19 +118,20 @@ public void TestHasProxy() {
114118

115119
// register a proxy
116120
IModel model = Model.Instance;
117-
IProxy proxy = new Proxy("aces", new List<string>(new string[] { "clubs", "spades", "hearts", "diamonds" }));
121+
string name = "aces" + Thread.CurrentThread.Name;
122+
IProxy proxy = new Proxy(name, new List<string>(new string[] { "clubs", "spades", "hearts", "diamonds" }));
118123
model.RegisterProxy(proxy);
119124

120125
// assert that the model.hasProxy method returns true
121126
// for that proxy name
122-
Assert.True(model.HasProxy("aces") == true, "Expecting model.hasProxy('aces') == true");
127+
Assert.True(model.HasProxy(name) == true, "Expecting model.hasProxy(name) == true");
123128

124129
// remove the proxy
125-
model.RemoveProxy("aces");
130+
model.RemoveProxy(name);
126131

127132
// assert that the model.hasProxy method returns false
128133
// for that proxy name
129-
Assert.True(model.HasProxy("aces") == false, "Expecting model.hasProxy('aces') == false");
134+
Assert.True(model.HasProxy(name) == false, "Expecting model.hasProxy(name) == false");
130135
}
131136

132137
/**
@@ -150,5 +155,49 @@ public void TestOnRegisterAndOnRemove() {
150155
// assert that onRemove was called, and the proxy responded by setting its data accordingly
151156
Assert.True(proxy.Data.ToString() == ModelTestProxy.ON_REMOVE_CALLED, "Expecting proxy.Data.ToString() == ModelTestProxy.ON_REMOVE_CALLED");
152157
}
153-
}
158+
159+
/// <summary>
160+
/// Test all of the function above using many threads at once.
161+
/// </summary>
162+
public void TestMultiThreadedOperations()
163+
{
164+
count = 20;
165+
IList<Thread> threads = new List<Thread>();
166+
167+
for (int i = 0; i < count; i++)
168+
{
169+
Thread t = new Thread(new ThreadStart(MultiThreadedTestFunction));
170+
t.Name = "ControllerTest" + i;
171+
threads.Add(t);
172+
}
173+
174+
foreach (Thread t in threads)
175+
{
176+
t.Start();
177+
}
178+
179+
while (true)
180+
{
181+
if (count <= 0) break;
182+
Thread.Sleep(100);
183+
}
184+
}
185+
186+
private int count = 0;
187+
188+
private int threadIterationCount = 10000;
189+
190+
private void MultiThreadedTestFunction()
191+
{
192+
for (int i = 0; i < threadIterationCount; i++)
193+
{
194+
// All we need to do is test the registration and removal of proxies.
195+
TestRegisterAndRetrieveProxy();
196+
TestRegisterAndRemoveProxy();
197+
TestHasProxy();
198+
}
199+
200+
count--;
201+
}
202+
}
154203
}

0 commit comments

Comments
 (0)