From 333e896ef8d10de61e075bac403c6e094d0916da Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Tue, 24 Sep 2024 21:30:02 -0400 Subject: [PATCH] User subscription tests --- .../GQL/Subscriptions/SubscribeUsers.graphql | 5 +++++ .../GraphQL/Subscriptions/UserSubscriptions.cs | 6 +----- .../Tgstation.Server.Tests/Live/IMultiServerClient.cs | 11 +++++++++++ .../Tgstation.Server.Tests/Live/MultiServerClient.cs | 3 +++ tests/Tgstation.Server.Tests/Live/TestLiveServer.cs | 5 +++++ tests/Tgstation.Server.Tests/Live/UsersTest.cs | 11 +++++++++++ 6 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/Tgstation.Server.Client.GraphQL/GQL/Subscriptions/SubscribeUsers.graphql diff --git a/src/Tgstation.Server.Client.GraphQL/GQL/Subscriptions/SubscribeUsers.graphql b/src/Tgstation.Server.Client.GraphQL/GQL/Subscriptions/SubscribeUsers.graphql new file mode 100644 index 0000000000..fe437ef62d --- /dev/null +++ b/src/Tgstation.Server.Client.GraphQL/GQL/Subscriptions/SubscribeUsers.graphql @@ -0,0 +1,5 @@ +subscription SubscribeUsers { + userUpdated { + id + } +} diff --git a/src/Tgstation.Server.Host/GraphQL/Subscriptions/UserSubscriptions.cs b/src/Tgstation.Server.Host/GraphQL/Subscriptions/UserSubscriptions.cs index c5b0656e6b..e8f22cc938 100644 --- a/src/Tgstation.Server.Host/GraphQL/Subscriptions/UserSubscriptions.cs +++ b/src/Tgstation.Server.Host/GraphQL/Subscriptions/UserSubscriptions.cs @@ -9,8 +9,6 @@ using HotChocolate.Subscriptions; using HotChocolate.Types; using HotChocolate.Types.Relay; -using Microsoft.Extensions.Hosting; - using Tgstation.Server.Api.Rights; using Tgstation.Server.Host.GraphQL.Types; using Tgstation.Server.Host.Security; @@ -57,13 +55,11 @@ namespace Tgstation.Server.Host.GraphQL.Subscriptions public ValueTask> UserUpdatedStream( [ID(nameof(User))] long? userId, [Service] ITopicEventReceiver receiver, - [Service] IHostApplicationLifetime applicationLifetime, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(receiver); var topic = userId.HasValue ? SpecificUserUpdatedTopic(userId.Value) : UserUpdatedTopic; - var cts = CancellationTokenSource.CreateLinkedTokenSource(applicationLifetime.ApplicationStopping, cancellationToken); - return receiver.SubscribeAsync(topic, cts.Token); + return receiver.SubscribeAsync(topic, cancellationToken); } /// diff --git a/tests/Tgstation.Server.Tests/Live/IMultiServerClient.cs b/tests/Tgstation.Server.Tests/Live/IMultiServerClient.cs index d617c63288..315052e1a2 100644 --- a/tests/Tgstation.Server.Tests/Live/IMultiServerClient.cs +++ b/tests/Tgstation.Server.Tests/Live/IMultiServerClient.cs @@ -19,5 +19,16 @@ namespace Tgstation.Server.Tests.Live Func comparison, CancellationToken cancellationToken) where TGraphQLResult : class; + + /// + /// Subcribes to the GraphQL subscription indicated by . + /// + /// The of the 's . + /// A which initiates a single subscription on a given and returns a resulting in the . + /// The for s. + /// The for the operation. + /// A resulting in the representing the lifetime of the subscription. + ValueTask Subscribe(Func>> operationExecutor, IObserver> observer, CancellationToken cancellationToken) + where TResultData : class; } } diff --git a/tests/Tgstation.Server.Tests/Live/MultiServerClient.cs b/tests/Tgstation.Server.Tests/Live/MultiServerClient.cs index 451ae08c47..5dbf153d78 100644 --- a/tests/Tgstation.Server.Tests/Live/MultiServerClient.cs +++ b/tests/Tgstation.Server.Tests/Live/MultiServerClient.cs @@ -58,5 +58,8 @@ namespace Tgstation.Server.Tests.Live return (restResult, graphQLResult.Data); } + + public ValueTask Subscribe(Func>> operationExecutor, IObserver> observer, CancellationToken cancellationToken) where TResultData : class + => GraphQLClient.Subscribe(operationExecutor, observer, cancellationToken); } } diff --git a/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs b/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs index 6071e51d48..0dccd54a29 100644 --- a/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs +++ b/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs @@ -1466,6 +1466,11 @@ namespace Tgstation.Server.Tests.Live InstanceResponse odInstance, compatInstance; if (!openDreamOnly) { + // force a session refresh if necessary + await firstAdminMultiClient.GraphQLClient.RunQueryEnsureNoErrors( + gql => gql.ReadCurrentUser.ExecuteAsync(cancellationToken), + cancellationToken); + jobsHubTestTask = FailFast(await jobsHubTest.Run(cancellationToken)); // returns Task var rootTest = FailFast(RawRequestTests.Run(restClientFactory, firstAdminRestClient, cancellationToken)); var adminTest = FailFast(new AdministrationTest(firstAdminRestClient.Administration).Run(cancellationToken)); diff --git a/tests/Tgstation.Server.Tests/Live/UsersTest.cs b/tests/Tgstation.Server.Tests/Live/UsersTest.cs index a309e906ec..1ef0a26ef1 100644 --- a/tests/Tgstation.Server.Tests/Live/UsersTest.cs +++ b/tests/Tgstation.Server.Tests/Live/UsersTest.cs @@ -34,12 +34,23 @@ namespace Tgstation.Server.Tests.Live public async ValueTask Run(CancellationToken cancellationToken) { + var observer = new HoldLastObserver>(); + using var subscription = await serverClient.Subscribe( + gql => gql.SubscribeUsers.Watch(), + observer, + cancellationToken); + await ValueTaskExtensions.WhenAll( BasicTests(cancellationToken), TestCreateSysUser(cancellationToken), TestSpamCreation(cancellationToken)); await TestPagination(cancellationToken); + + Assert.IsFalse(observer.Completed); + Assert.AreEqual(0U, observer.ErrorCount); + Assert.AreEqual(new PlatformIdentifier().IsWindows ? 108U : 107U, observer.ResultCount); // sys user + observer.LastValue.EnsureNoErrors(); } async ValueTask BasicTests(CancellationToken cancellationToken)