private static void PerformReads(int numReads, ReaderWriterLockedList state)
 {
     for (int i = 0; i < numReads; ++i)
     {
         state.GetAllValues();
     }
 }
 private static void PerformWrites(int numWrites, ReaderWriterLockedList state)
 {
     for (int i = 0; i < numWrites; ++i)
     {
         state.Add(Guid.NewGuid().ToString("D"));
     }
 }
Example #3
0
 private static void PerformWrites(int numWrites, ReaderWriterLockedList state)
 {
     for (int i = 0; i < numWrites; ++i)
     {
         state.Add(Guid.NewGuid().ToString("D"));
     }
 }
        public void Lock_ShouldProtectThreadUnsafeCollectionAgainstUnsafeModification()
        {
            var list = new ReaderWriterLockedList(new DiyReaderWriterLock());
            Task[] tasks = PrepareTasks(4, 20000, list, PerformReads).Concat(PrepareTasks(2, 6000, list, PerformWrites)).ToArray();

            Task.WaitAll(tasks); // will throw AggregateException if any task throws an exception
        }
Example #5
0
 private static void PerformReads(int numReads, ReaderWriterLockedList state)
 {
     for (int i = 0; i < numReads; ++i)
     {
         state.GetAllValues();
     }
 }
        public void Lock_ShouldAllowMultipleSimultaneousReaders()
        {
            var list = new ReaderWriterLockedList(new DiyReaderWriterLock());
            Task[] tasks = PrepareTasks(4, 5000, list, PerformReads).Concat(PrepareTasks(1, 1000, list, PerformWrites)).ToArray();

            Task.WaitAll(tasks);
            Assert.That(list.MaxSimultaneousReaders, Is.GreaterThan(1));
        }
Example #7
0
        public void Lock_ShouldProtectThreadUnsafeCollectionAgainstUnsafeModification()
        {
            var list = new ReaderWriterLockedList(new DiyReaderWriterLock());

            Task[] tasks = PrepareTasks(4, 20000, list, PerformReads).Concat(PrepareTasks(2, 6000, list, PerformWrites)).ToArray();

            Task.WaitAll(tasks); // will throw AggregateException if any task throws an exception
        }
Example #8
0
        public void Lock_ShouldAllowMultipleSimultaneousReaders()
        {
            var list = new ReaderWriterLockedList(new DiyReaderWriterLock());

            Task[] tasks = PrepareTasks(4, 5000, list, PerformReads).Concat(PrepareTasks(1, 1000, list, PerformWrites)).ToArray();

            Task.WaitAll(tasks);
            Assert.That(list.MaxSimultaneousReaders, Is.GreaterThan(1));
        }
        public void IncreaseCapacity()
        {
            var testTarget      = new ReaderWriterLockedList <int>();
            var initialCapacity = testTarget.Capacity;

            for (var i = 0; i < initialCapacity * 2; i++)
            {
                testTarget.Add(i);
            }

            testTarget.Capacity.Should().BeGreaterThan(initialCapacity * 2);
        }
 private IEnumerable<Task> PrepareTasks(int numTasks, int taskSize, ReaderWriterLockedList state, Action<int, ReaderWriterLockedList> action)
 {
     return Enumerable.Range(0, numTasks).Select(i => Task.Factory.StartNew(() => action(taskSize, state)));
 }
Example #11
0
 private IEnumerable <Task> PrepareTasks(int numTasks, int taskSize, ReaderWriterLockedList state, Action <int, ReaderWriterLockedList> action)
 {
     return(Enumerable.Range(0, numTasks).Select(i => Task.Factory.StartNew(() => action(taskSize, state))));
 }
        public void IsReadOnlyIsAlwaysFalse()
        {
            IList <object> testTarget = new ReaderWriterLockedList <object>();

            testTarget.IsReadOnly.Should().BeFalse();
        }