Changeset 6547


Ignore:
Timestamp:
07/08/11 01:26:46 (10 years ago)
Author:
swagner
Message:

Changed ThreadSafeLogTest as testing the race condition with the unsafe log resulted in strange exceptions sometimes (#1545)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab/3.3/Tests/ThreadSafeLogTest.cs

    r6545 r6547  
    2020#endregion
    2121
    22 using System;
    2322using System.Linq;
    2423using System.Threading.Tasks;
     
    3231    [TestMethod]
    3332    public void ThreadSafeLogThreadSavetyTest() {
    34       int count = 100000;
    35       if (Environment.ProcessorCount > 2) {
    36         Log l = new Log();
    37         AddMessagesInParallel(count, l);
    38         // with a normal log there are race conditions which should cause problems
    39         // actually we might geht lucky here but this should happen only seldomly
    40         Assert.AreNotEqual(count, l.Messages.Count());
    41       }
     33      int count = 10000;
     34      ThreadSafeLog log = new ThreadSafeLog();
    4235
    43       ThreadSafeLog safeLog = new ThreadSafeLog();
    44       AddMessagesInParallel(count, safeLog);
    45       // the thread safe log should work like a charm
    46       Assert.AreEqual(count, safeLog.Messages.Count());
    47     }
     36      Parallel.For(0, count, (i) => {
     37        log.LogMessage("Message " + i); // write something
     38        log.Messages.Count(); // iterate over all messages
     39      });
    4840
    49     private void AddMessagesInParallel(int count, ILog l) {
    50       Parallel.For(0, count, (i) => {
    51         l.LogMessage("Message " + i); // write something
    52         l.Messages.Count(); // iterate over all messages
    53       });
     41      Assert.AreEqual(count, log.Messages.Count());
    5442    }
    5543  }
Note: See TracChangeset for help on using the changeset viewer.