From 50f95cbbc0737fe3807223385d98283ce4eaaffa Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Mon, 7 Dec 2015 15:17:21 +0100 Subject: [PATCH] Added some tests for ThreadCount --- .../KestrelServerInformationTests.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs diff --git a/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs new file mode 100644 index 0000000000..5a167adf27 --- /dev/null +++ b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs @@ -0,0 +1,51 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Server.Kestrel; +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace Microsoft.AspNet.Server.KestrelTests +{ + public class KestrelServerInformationTests + { + [Fact] + public void SetThreadCountUsingConfiguration() + { + const int expected = 42; + + var values = new Dictionary + { + { "server.threadCount", expected.ToString() } + }; + + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + var information = new KestrelServerInformation(configuration); + + Assert.Equal(expected, information.ThreadCount); + } + + [Fact] + public void SetThreadCountUsingProcessorCount() + { + // Ideally we'd mock Environment.ProcessorCount to test edge cases. + var expected = Clamp(Environment.ProcessorCount >> 1, 1, 16); + + var configuration = new ConfigurationBuilder().Build(); + + var information = new KestrelServerInformation(configuration); + + Assert.Equal(expected, information.ThreadCount); + } + + private static int Clamp(int value, int min, int max) + { + return value < min ? min : value > max ? max : value; + } + } +} \ No newline at end of file