diff --git a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs
index 300e8035..ac90c75e 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs
@@ -23,4 +23,9 @@ public class UserAbstraction(Config _config, SpacebarClientProviderService _clie
return client;
}
+
+ private static AuthenticatedSpacebarClient? _authenticatedSpacebarClient;
+ public async Task<AuthenticatedSpacebarClient> GetSharedUser() {
+ return _authenticatedSpacebarClient ??= await GetFreshUser();
+ }
}
\ No newline at end of file
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Config.cs b/extra/admin-api/Tests/Spacebar.Tests/Config.cs
index d1aa9de0..7419caa2 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Config.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Config.cs
@@ -8,4 +8,5 @@ public class Config {
}
public string TestInstance { get; set; }
+ public int RegisterConcurrentCount { get; set; }
}
\ No newline at end of file
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs b/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs
new file mode 100644
index 00000000..53113f97
--- /dev/null
+++ b/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs
@@ -0,0 +1,11 @@
+using Xunit.Microsoft.DependencyInjection.Abstracts;
+
+namespace Spacebar.Tests.Extensions;
+
+public static class TestBedFixtureExtensions {
+ extension(TestBedFixture tbf) {
+ public T GetRequiredService<T>(ITestOutputHelper toh) {
+ return tbf.GetService<T>(toh) ?? throw new InvalidOperationException($"Failed to get service: {typeof(T).FullName}");
+ }
+ }
+}
\ No newline at end of file
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs
index f927d7c5..ca632228 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs
@@ -7,18 +7,14 @@ using Spacebar.Sdk.Core;
using Spacebar.Tests.Extensions;
using Spacebar.Tests.Fixtures;
using Xunit.Microsoft.DependencyInjection.Abstracts;
+using Xunit.Sdk;
namespace Spacebar.Tests.Tests;
public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
-
- private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
- throw new InvalidOperationException(
- $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
-
- private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
+ private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
+ private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
+ private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
[Fact]
public async Task RegisterUser() {
@@ -29,11 +25,13 @@ public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture
DateOfBirth = new(),
Consent = true
});
+ await Assert.HttpSuccess(res);
}
[Fact]
- public async Task ConcurrentRegister50Users() {
- var tasks = Enumerable.Range(0, 50).Select(async _ => {
+ public async Task RegisterUsersConcurrent() {
+ testOutputHelper.WriteLine($"Registering {_config.RegisterConcurrentCount} users concurrently...");
+ var tasks = Enumerable.Range(0, _config.RegisterConcurrentCount).Select(async _ => {
var sw = Stopwatch.StartNew();
var rr = new RegisterRequest() {
Email = $"{Guid.NewGuid().ToString()}@{Guid.NewGuid().ToString()}.tld",
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs
index 56254a62..22c25558 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs
@@ -6,10 +6,8 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class BasicWellKnownTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
-
- private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(Config)}");
+ private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
+ private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
[Fact]
public async Task ValidateTestConfig() {
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs
index cdee86b4..cd852263 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs
@@ -8,60 +8,50 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
-public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
+public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
+ private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
+ private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
+ private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
+ private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
- private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
- throw new InvalidOperationException(
- $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
+ private static AuthenticatedSpacebarClient Client { get; set; } = null!;
+ private static Guild? Guild { get; set; }
- private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
-
- private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
-
- [Fact]
- public async Task CreateChannel() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
+ public async ValueTask InitializeAsync() {
+ testOutputHelper.WriteLine("Running InitializeAsync");
+ // All these tests can share a single client
+ Client = await _userAbstraction.GetSharedUser();
+ // ...and a guild
+ Guild ??= await Client.CreateGuild(new() {
Name = "Test guild"
});
-
- Assert.Equal("Test guild", guild.Name);
+ }
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
+ [Fact]
+ public async Task CreateChannel() {
+ Assert.Equal("Test guild", Guild!.Name);
+ var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
-
+
Assert.Equal("test", channel.Name);
}
[Fact]
public async Task GetChannel() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
- Name = "Test guild"
- });
-
- Assert.Equal("Test guild", guild.Name);
-
- // await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
+ Assert.Equal("Test guild", Guild!.Name);
+ var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
-
Assert.Equal("test", channel.Name);
- // await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake
- var res = await client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken);
+ var res = await Client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken);
await Assert.HttpSuccess(res);
-
+
var channelResp = await res.Content.ReadFromJsonAsync<Channel>(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(channel.Name, channelResp!.Name);
Assert.Equal(channel.Id, channelResp!.Id);
-
}
}
\ No newline at end of file
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs
index 88490e0d..24d2a17c 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs
@@ -7,21 +7,14 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
-
- private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
- throw new InvalidOperationException(
- $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
-
- private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
-
- private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
+ private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
+ private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
+ private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
+ private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
[Fact]
public async Task CreateGuild() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
+ var client = await _userAbstraction.GetSharedUser();
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
@@ -31,7 +24,7 @@ public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
[Fact]
public async Task GetChannels() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
+ var client = await _userAbstraction.GetSharedUser();
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs
index 64f80d0a..49c9da57 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs
@@ -6,7 +6,7 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests.Meta;
public class UserAbstractionTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly UserAbstraction _config = fixture.GetService<UserAbstraction>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(UserAbstraction)}");
+ private readonly UserAbstraction _config = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
[Fact]
public async Task CanGetUser() {
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs
index 93a4d38a..43863237 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs
+++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs
@@ -7,61 +7,58 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
-public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
- private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
+public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
+ private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
+ private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
+ private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
+ private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
- private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
- throw new InvalidOperationException(
- $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
+ private static AuthenticatedSpacebarClient Client = null!;
+ private static SpacebarClientGuild? Guild;
- private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
+ private static SpacebarClientChannel? Channel = null!;
- private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
- throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
-
- [Fact]
- public async Task CreateWebhook() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
- Name = "Test guild"
- });
-
- Assert.Equal("Test guild", guild.Name);
+ public async ValueTask InitializeAsync() {
+ Client = await _userAbstraction.GetSharedUser();
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
- Name = "test",
- Type = 0
- });
-
- Assert.Equal("test", channel.Name);
+ if (Guild is null)
+ await Client.CreateGuild(new() {
+ Name = "Test guild"
+ }).ContinueWith(g => {
+ Assert.Equal("Test guild", g.Result.Name);
+ Guild = Client.GetGuild(g.Result.Id);
+ });
- var cChannel = client.GetChannel(channel.Id);
- var wh = await cChannel.CreateWebhookAsync(new() {
+ if (Channel is null)
+ await Guild!.CreateChannelAsync(new() {
+ Name = "test",
+ Type = 0
+ }).ContinueWith(c => {
+ Assert.Equal("test", c.Result.Name);
+ Channel = Client.GetChannel(c.Result.Id);
+ });
+ }
+
+ [Fact]
+ public async Task CreateWebhook() {
+ var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
}
-
+
[Fact]
public async Task CreateMultipleWebhooks() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
- Name = "Test guild"
- });
-
- Assert.Equal("Test guild", guild.Name);
-
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
+ var channel = await Guild!.CreateChannelAsync(new() {
Name = "test",
Type = 0
});
-
+
Assert.Equal("test", channel.Name);
- var cChannel = client.GetChannel(channel.Id);
+ var cChannel = Client.GetChannel(channel.Id);
var count = Random.Shared.Next(10);
testOutputHelper.WriteLine($"Creating {count} webhooks...");
@@ -76,22 +73,7 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
[Fact]
public async Task SendWebhookMessageWithWait() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
- Name = "Test guild"
- });
-
- Assert.Equal("Test guild", guild.Name);
-
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
- Name = "test",
- Type = 0
- });
-
- Assert.Equal("test", channel.Name);
-
- var cChannel = client.GetChannel(channel.Id);
- var wh = await cChannel.CreateWebhookAsync(new() {
+ var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
@@ -102,25 +84,10 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
{ "content", "meow" }
});
}
-
+
[Fact]
public async Task SendWebhookMessage() {
- var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
- var guild = await client.CreateGuild(new() {
- Name = "Test guild"
- });
-
- Assert.Equal("Test guild", guild.Name);
-
- var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
- Name = "test",
- Type = 0
- });
-
- Assert.Equal("test", channel.Name);
-
- var cChannel = client.GetChannel(channel.Id);
- var wh = await cChannel.CreateWebhookAsync(new() {
+ var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
diff --git a/extra/admin-api/Tests/Spacebar.Tests/appsettings.json b/extra/admin-api/Tests/Spacebar.Tests/appsettings.json
index d1d45a93..fa03afc7 100644
--- a/extra/admin-api/Tests/Spacebar.Tests/appsettings.json
+++ b/extra/admin-api/Tests/Spacebar.Tests/appsettings.json
@@ -1,5 +1,6 @@
{
"Configuration": {
- "TestInstance": "http://localhost:3001"
+ "TestInstance": "http://localhost:3001",
+ "RegisterConcurrentCount": 15
}
}
\ No newline at end of file
|