Skip to content

Commit 57a10d1

Browse files
2.1.0 nuget package
1 parent 0e7e41f commit 57a10d1

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

src/ObservableComputations.Test/CollectionDispatchingTest.cs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,94 @@ public void TestPropertyDispatchingProperties()
152152
backgroundOcDispatcher.Dispose();
153153
}
154154

155+
byte _initWay = 0;
156+
155157
[Test]
156-
[Repeat(1)]
158+
[Repeat(10)]
157159
[Timeout(1000 * 60 * 60 * 5)]
158160
public void TestCollectionDispatchingTest()
159161
{
162+
160163
OcDispatcher mainOcDispatcher = new OcDispatcher();
161164
mainOcDispatcher.ThreadName = "mainOcDispatcher";
162165
OcDispatcher backgroundOcDispatcher = new OcDispatcher();
163166
backgroundOcDispatcher.ThreadName = "backgroundOcDispatcher";
164167
OcConsumer consumer = new OcConsumer();
165168

166169
ObservableCollection<Item> nums = new ObservableCollection<Item>();
170+
167171
Filtering<Item> filteredNums = nums.Filtering(i =>
168172
i.Num % 3 == 0
169173
|| i.Num2MainToBackgroundDispatching.Value % 5 == 0
170174
|| i.Num2MainToBackgroundScalarDispatching.Value % 5 == 0);
171-
CollectionDispatching<Item> dispatchingfilteredNums = filteredNums
175+
176+
CollectionDispatching<Item> dispatchingfilteredNums;
177+
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
178+
179+
Action func = () =>
180+
{
181+
Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(1, 4)));
182+
dispatchingfilteredNums = filteredNums
183+
.CollectionDispatching(mainOcDispatcher, backgroundOcDispatcher).For(consumer);
184+
185+
dispatchingfilteredNums.CollectionChanged += (sender, args) =>
186+
{
187+
if (Thread.CurrentThread != mainOcDispatcher._thread)
188+
{
189+
throw new Exception("Wrong thread");
190+
}
191+
};
192+
193+
((INotifyPropertyChanged)dispatchingfilteredNums).PropertyChanged += (sender, args) =>
194+
{
195+
if (Thread.CurrentThread != mainOcDispatcher._thread)
196+
{
197+
throw new Exception("Wrong thread");
198+
}
199+
};
200+
201+
filteredNums.CollectionChanged += (sender, args) =>
202+
{
203+
204+
if (Thread.CurrentThread != backgroundOcDispatcher._thread)
205+
{
206+
throw new Exception("Wrong thread");
207+
}
208+
};
209+
210+
((INotifyPropertyChanged)filteredNums).PropertyChanged += (sender, args) =>
211+
{
212+
if (Thread.CurrentThread != backgroundOcDispatcher._thread)
213+
{
214+
throw new Exception("Wrong thread");
215+
}
216+
};
217+
218+
manualResetEvent.Set();
219+
};
220+
221+
if (_initWay == 0)
222+
{
223+
mainOcDispatcher.InvokeAsync(func);
224+
}
225+
else if (_initWay == 1)
226+
{
227+
Thread thread = new Thread(() => func());
228+
thread.Start();
229+
}
230+
else if (_initWay == 2)
231+
{
232+
func();
233+
}
234+
else
235+
{
236+
backgroundOcDispatcher.InvokeAsync(func);
237+
}
238+
239+
_initWay++;
240+
if (_initWay == 4) _initWay = 0;
241+
242+
dispatchingfilteredNums = filteredNums
172243
.CollectionDispatching(mainOcDispatcher, backgroundOcDispatcher).For(consumer);
173244

174245
dispatchingfilteredNums.CollectionChanged += (sender, args) =>
@@ -209,7 +280,7 @@ public void TestCollectionDispatchingTest()
209280
Random stopperRandom = new Random();
210281
Thread stopper = new Thread(() =>
211282
{
212-
Thread.Sleep(TimeSpan.FromSeconds(stopperRandom.Next(2, 20)));
283+
Thread.Sleep(TimeSpan.FromSeconds(stopperRandom.Next(5, 20)));
213284
stop = true;
214285
});
215286

@@ -309,6 +380,7 @@ public void TestCollectionDispatchingTest()
309380

310381
ThreadStart numValueChangerThreadStart = () =>
311382
{
383+
manualResetEvent.WaitOne();
312384
Random random = new Random();
313385
while (!stop)
314386
{
@@ -335,6 +407,7 @@ public void TestCollectionDispatchingTest()
335407

336408
ThreadStart numValueChanger2ThreadStart = () =>
337409
{
410+
manualResetEvent.WaitOne();
338411
Random random = new Random();
339412
while (!stop)
340413
{
@@ -362,6 +435,7 @@ public void TestCollectionDispatchingTest()
362435

363436
ThreadStart num2ValueChangerThreadStart = () =>
364437
{
438+
manualResetEvent.WaitOne();
365439
Random random = new Random();
366440
while (!stop)
367441
{
@@ -405,6 +479,8 @@ public void TestCollectionDispatchingTest()
405479
num2ValueChangerThreads[i].Join();
406480
}
407481

482+
manualResetEvent.Dispose();
483+
408484
//consuminingOcDispatcherInvoker.Join();
409485

410486
mainOcDispatcher.Invoke(() => { });

0 commit comments

Comments
 (0)