Last change
on this file since 6536 was
6536,
checked in by gkronber, 13 years ago
|
#1545: reviewed class ThreadSafeLog and removed the LockRecursionPolicy.SupportsRecursion option which is not necessary. Added unit test for the thread safe log.
|
File size:
1.4 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.Text;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.PluginInfrastructure;
|
---|
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
10 | using HeuristicLab.Core;
|
---|
11 | using System.Threading.Tasks;
|
---|
12 | using System.Threading;
|
---|
13 |
|
---|
14 | namespace HeuristicLab_33.Tests {
|
---|
15 | [TestClass]
|
---|
16 | public class ThreadSafeLogTest {
|
---|
17 |
|
---|
18 | [TestMethod]
|
---|
19 | public void ThreadSafeLogThreadSavetyTest() {
|
---|
20 | int count = 100000;
|
---|
21 | if (Environment.ProcessorCount > 2) {
|
---|
22 | Log l = new Log();
|
---|
23 | AddMessagesInParallel(count, l);
|
---|
24 | // with a normal log there are race conditions which should cause problems
|
---|
25 | // actually we might geht lucky here but this should happen only seldomly
|
---|
26 | Assert.AreNotEqual(count, l.Messages.Count());
|
---|
27 | }
|
---|
28 |
|
---|
29 | ThreadSafeLog safeLog = new ThreadSafeLog();
|
---|
30 | AddMessagesInParallel(count, safeLog);
|
---|
31 | // the thread safe log should work like a charm
|
---|
32 | Assert.AreEqual(count, safeLog.Messages.Count());
|
---|
33 | }
|
---|
34 |
|
---|
35 | private void AddMessagesInParallel(int count, ILog l) {
|
---|
36 | Parallel.For(0, count, (i) => {
|
---|
37 | l.LogMessage("Message " + i); // write something
|
---|
38 | l.Messages.Count(); // iterate over all messages
|
---|
39 | });
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.