Adds a user spam integration test.

This is mainly to test server load as well as SQLite async support.
This commit is contained in:
Jordan Brown
2020-03-22 20:38:35 -04:00
parent 6a474239ea
commit f5589b02a1
2 changed files with 61 additions and 0 deletions
@@ -142,6 +142,7 @@ namespace Tgstation.Server.Tests
await Assert.ThrowsExceptionAsync<UnauthorizedException>(() => 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);
}
}
+60
View File
@@ -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<Task<User>> tasks = new List<Task<User>>();
// 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!");
}
}
}