4
4
Your reuse is governed by the Creative Commons Attribution 3.0 License
5
5
*/
6
6
using System ;
7
+ using System . Threading ;
8
+ using System . Collections . Generic ;
7
9
8
10
using NUnitLite ;
9
11
using NUnit . Framework ;
@@ -43,6 +45,7 @@ public static ITest Suite
43
45
ts . AddTest ( new ControllerTest ( "TestRegisterAndRemoveCommand" ) ) ;
44
46
ts . AddTest ( new ControllerTest ( "TestHasCommand" ) ) ;
45
47
ts . AddTest ( new ControllerTest ( "TestReregisterAndExecuteCommand" ) ) ;
48
+ ts . AddTest ( new ControllerTest ( "TestMultiThreadedOperations" ) ) ;
46
49
47
50
return ts ;
48
51
}
@@ -81,11 +84,12 @@ public void TestRegisterAndExecuteCommand()
81
84
{
82
85
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
83
86
IController controller = Controller . Instance ;
84
- controller . RegisterCommand ( "ControllerTest" , typeof ( ControllerTestCommand ) ) ;
87
+ string name = "ControllerTest" + Thread . CurrentThread . Name ;
88
+ controller . RegisterCommand ( name , typeof ( ControllerTestCommand ) ) ;
85
89
86
90
// Create a 'ControllerTest' note
87
91
ControllerTestVO vo = new ControllerTestVO ( 12 ) ;
88
- INotification note = new Notification ( "ControllerTest" , vo ) ;
92
+ INotification note = new Notification ( name , vo ) ;
89
93
90
94
// Tell the controller to execute the Command associated with the note
91
95
// the ControllerTestCommand invoked will multiply the vo.input value
@@ -108,11 +112,12 @@ public void TestRegisterAndRemoveCommand()
108
112
109
113
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
110
114
IController controller = Controller . Instance ;
111
- controller . RegisterCommand ( "ControllerRemoveTest" , typeof ( ControllerTestCommand ) ) ;
115
+ string name = "ControllerRemoveTest" + Thread . CurrentThread . Name ;
116
+ controller . RegisterCommand ( name , typeof ( ControllerTestCommand ) ) ;
112
117
113
118
// Create a 'ControllerTest' note
114
119
ControllerTestVO vo = new ControllerTestVO ( 12 ) ;
115
- INotification note = new Notification ( "ControllerRemoveTest" , vo ) ;
120
+ INotification note = new Notification ( name , vo ) ;
116
121
117
122
// Tell the controller to execute the Command associated with the note
118
123
// the ControllerTestCommand invoked will multiply the vo.input value
@@ -126,7 +131,7 @@ public void TestRegisterAndRemoveCommand()
126
131
vo . result = 0 ;
127
132
128
133
// Remove the Command from the Controller
129
- controller . RemoveCommand ( "ControllerRemoveTest" ) ;
134
+ controller . RemoveCommand ( name ) ;
130
135
131
136
// Tell the controller to execute the Command associated with the
132
137
// note. This time, it should not be registered, and our vo result
@@ -144,16 +149,17 @@ public void TestRegisterAndRemoveCommand()
144
149
public void TestHasCommand ( ) {
145
150
// register the ControllerTestCommand to handle 'hasCommandTest' notes
146
151
IController controller = Controller . Instance ;
147
- controller . RegisterCommand ( "hasCommandTest" , typeof ( ControllerTestCommand ) ) ;
152
+ string name = "HasCommandTest" + Thread . CurrentThread . Name ;
153
+ controller . RegisterCommand ( name , typeof ( ControllerTestCommand ) ) ;
148
154
149
155
// 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" ) ;
151
157
152
158
// Remove the Command from the Controller
153
- controller . RemoveCommand ( "hasCommandTest" ) ;
159
+ controller . RemoveCommand ( name ) ;
154
160
155
161
// 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" ) ;
157
163
}
158
164
159
165
/**
@@ -172,17 +178,18 @@ public void TestReregisterAndExecuteCommand() {
172
178
173
179
// Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes
174
180
IController controller = Controller . Instance ;
175
- controller . RegisterCommand ( "ControllerTest2" , typeof ( ControllerTestCommand2 ) ) ;
181
+ string name = "ControllerTest2" + Thread . CurrentThread . Name ;
182
+ controller . RegisterCommand ( name , typeof ( ControllerTestCommand2 ) ) ;
176
183
177
184
// Remove the Command from the Controller
178
- controller . RemoveCommand ( "ControllerTest2" ) ;
185
+ controller . RemoveCommand ( name ) ;
179
186
180
187
// Re-register the Command with the Controller
181
- controller . RegisterCommand ( "ControllerTest2" , typeof ( ControllerTestCommand2 ) ) ;
188
+ controller . RegisterCommand ( name , typeof ( ControllerTestCommand2 ) ) ;
182
189
183
190
// Create a 'ControllerTest2' note
184
191
ControllerTestVO vo = new ControllerTestVO ( 12 ) ;
185
- Notification note = new Notification ( "ControllerTest2" , vo ) ;
192
+ Notification note = new Notification ( name , vo ) ;
186
193
187
194
// retrieve a reference to the View.
188
195
IView view = View . Instance ;
@@ -199,7 +206,50 @@ public void TestReregisterAndExecuteCommand() {
199
206
200
207
// if the command is executed twice the value will be 48
201
208
Assert . True ( vo . result == 48 , "Expecting vo.result == 48" ) ;
202
-
203
209
}
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
+ }
205
255
}
0 commit comments