Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.8K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Configuration;
using Nelibur.Sword.Threading.ThreadPools;
using Nelibur.Sword.Threading.ThreadPools.TaskQueueControllers;
using Xunit;
using Xunit.Extensions;

namespace UnitTests.Nelibur.Sword.Threading.ThreadPools
{
    public sealed class TinyThreadPoolTests
    {
        [Theory]
        [InlineData("")]
        [InlineData(" ")]
        [InlineData(null)]
        public void Create_InvalidName_ThrowException(string name)
        {
            Assert.Throws<ConfigurationErrorsException>(() => TinyThreadPool.Create(x => { x.Name = name; }));
        }

        [Fact]
        public void Create_InvalidTaskQueueController_ThrowException()
        {
            Assert.Throws<ConfigurationErrorsException>(() => TinyThreadPool.Create(x => { x.TaskQueueController = null; }));
        }

        [Fact]
        public void Create_InvalidTaskQueue_ThrowException()
        {
            Assert.Throws<ArgumentNullException>(() => TinyThreadPool.Create(x => { x.TaskQueueController = new DefaultTaskQueueController(null); }));
        }

        [Theory]
        [InlineData(0, 1)]
        [InlineData(-1, 1)]
        [InlineData(2, 1)]
        [InlineData(0, 0)]
        public void Create_InvalidThreadRange_ThrowException(int minThreads, int maxThreads)
        {
            Assert.Throws<ConfigurationErrorsException>(() => TinyThreadPool.Create(x =>
            {
                x.MinThreads = minThreads;
                x.MaxThreads = maxThreads;
            }));
        }

        [Fact]
        public void Create_NewThreadPool_Success()
        {
            const string Name = "MyThreadPool";
            const int MinThreads = 4;
            const int MaxThreads = 6;
            const MultiThreadingCapacity MultiThreadingCapacity = MultiThreadingCapacity.Global;

            ITinyThreadPool threadPool = TinyThreadPool.Create(x =>
            {
                x.Name = Name;
                x.MinThreads = MinThreads;
                x.MaxThreads = MaxThreads;
                x.MultiThreadingCapacity = MultiThreadingCapacity;
            });

            Assert.Equal(MultiThreadingCapacity, threadPool.MultiThreadingCapacity);
            Assert.Equal(MinThreads, threadPool.MinThreads);
            Assert.Equal(MaxThreads, threadPool.MaxThreads);
            Assert.Equal(Name, threadPool.Name);
            Console.WriteLine(threadPool.ToString());
        }

        [Fact]
        public void Default_NewThreadPool_Success()
        {
            ITinyThreadPool threadPool = TinyThreadPool.Default;
            Assert.Equal(MultiThreadingCapacity.PerProcessor, threadPool.MultiThreadingCapacity);
            Assert.Equal(Environment.ProcessorCount * 1, threadPool.MinThreads);
            Assert.Equal(Environment.ProcessorCount * 5, threadPool.MaxThreads);
            Assert.Equal("TinyThreadPool", threadPool.Name);
            threadPool.Dispose();
            threadPool.Dispose();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions