Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/ThreadSafeLogTest.cs @ 6536

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 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.PluginInfrastructure;
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10using HeuristicLab.Core;
11using System.Threading.Tasks;
12using System.Threading;
13
14namespace 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.