From f5589b02a167837d20f4d8937db98da27779df9f Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sun, 22 Mar 2020 20:38:35 -0400 Subject: [PATCH] Adds a user spam integration test. This is mainly to test server load as well as SQLite async support. --- .../Tgstation.Server.Tests/IntegrationTest.cs | 1 + tests/Tgstation.Server.Tests/UsersTest.cs | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/Tgstation.Server.Tests/UsersTest.cs diff --git a/tests/Tgstation.Server.Tests/IntegrationTest.cs b/tests/Tgstation.Server.Tests/IntegrationTest.cs index 355d25b031..aa305db13d 100644 --- a/tests/Tgstation.Server.Tests/IntegrationTest.cs +++ b/tests/Tgstation.Server.Tests/IntegrationTest.cs @@ -142,6 +142,7 @@ namespace Tgstation.Server.Tests await Assert.ThrowsExceptionAsync(() => badClient.Version(cancellationToken)).ConfigureAwait(false); await new AdministrationTest(adminClient.Administration).Run(cancellationToken).ConfigureAwait(false); + await new UsersTest(adminClient.Users).Run(cancellationToken).ConfigureAwait(false); await new InstanceManagerTest(adminClient.Instances, server.Directory).Run(cancellationToken).ConfigureAwait(false); } } diff --git a/tests/Tgstation.Server.Tests/UsersTest.cs b/tests/Tgstation.Server.Tests/UsersTest.cs new file mode 100644 index 0000000000..bcce0b08e5 --- /dev/null +++ b/tests/Tgstation.Server.Tests/UsersTest.cs @@ -0,0 +1,60 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Tgstation.Server.Api.Models; +using Tgstation.Server.Client; + +namespace Tgstation.Server.Tests +{ + sealed class UsersTest + { + readonly IUsersClient client; + + public UsersTest(IUsersClient client) + { + this.client = client ?? throw new ArgumentNullException(nameof(client)); + } + + public async Task Run(CancellationToken cancellationToken) + { + await TestSpamCreation(cancellationToken).ConfigureAwait(false); + } + + public async Task TestSpamCreation(CancellationToken cancellationToken) + { + ICollection> tasks = new List>(); + + // Careful with this, very easy to overload the thread pool + const int RepeatCount = 100; + + ThreadPool.GetMaxThreads(out var defaultMaxWorker, out var defaultMaxCompletion); + ThreadPool.GetMinThreads(out var defaultMinWorker, out var defaultMinCompletion); + try + { + ThreadPool.SetMinThreads(Math.Min(RepeatCount * 4, defaultMaxWorker), Math.Min(RepeatCount * 4, defaultMaxCompletion)); + for (int i = 0; i < RepeatCount; ++i) + { + tasks.Add( + client.Create( + new UserUpdate + { + Name = $"SpamTestUser_{i}", + Password = "asdfasdjfhauwiehruiy273894234jhndjkwh" + }, + cancellationToken)); + } + + await Task.WhenAll(tasks).ConfigureAwait(false); + } + finally + { + ThreadPool.SetMinThreads(defaultMinWorker, defaultMinCompletion); + } + + Assert.AreEqual(RepeatCount, tasks.Select(task => task.Result.Id).Distinct().Count(), "Did not receive expected number of unique user IDs!"); + } + } +} \ No newline at end of file