示例#1
0
    private async Task AssertMethodsCalled(IServiceProvider serviceProvider, TcsService tcsService)
    {
        var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

        using (var client = new TestClient())
        {
            var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

            await tcsService.StartedMethod.Task.DefaultTimeout();

            await client.Connected.DefaultTimeout();

            await tcsService.EndMethod.Task.DefaultTimeout();

            tcsService.Reset();
            var message = await client.InvokeAsync(nameof(MethodHub.Echo), "Hello world!").DefaultTimeout();

            await tcsService.EndMethod.Task.DefaultTimeout();

            tcsService.Reset();

            Assert.Null(message.Error);

            client.Dispose();

            await connectionHandlerTask.DefaultTimeout();

            await tcsService.EndMethod.Task.DefaultTimeout();
        }
    }
示例#2
0
 protected virtual void Cleanup()
 {
     TestClient?.Dispose();
     TestServer?.Dispose();
     TestHost?.Dispose();
     DbContext?.Dispose();
 }
示例#3
0
        public void JoinGameWithLobby()
        {
            string roomName = CreateRandomRoomName();

            TestClient client = this.InitClient();

            var request = new OperationRequest {
                OperationCode = (byte)OperationCode.Join, Parameters = new Dictionary <byte, object>()
            };

            request.Parameters.Add((byte)ParameterKey.GameId, roomName);
            request.Parameters.Add((byte)LobbyParameterKeys.LobbyId, "Mainlobby");
            client.SendOperationRequest(request);
            OperationResponse response  = client.WaitForOperationResponse(this.WaitTime);
            EventData         eventArgs = client.WaitForEvent(this.WaitTime);

            // check operation params
            CheckDefaultOperationParameters(response, OperationCode.Join);
            CheckParam(response, ParameterKey.ActorNr, 1);

            // check event params
            CheckDefaultEventParameters(eventArgs, OperationCode.Join, 1);
            CheckEventParamExists(eventArgs, ParameterKey.Actors);

            // cleanup
            client.Close();
            client.Dispose();
        }
示例#4
0
    public async Task HubFilterDoesNotNeedToImplementMethods()
    {
        using (StartVerifiableLog())
        {
            var tcsService      = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR().AddHubOptions <DynamicTestHub>(options =>
                {
                    options.AddFilter(typeof(EmptyFilter));
                });
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <DynamicTestHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                await client.Connected.DefaultTimeout();

                var completion = await client.InvokeAsync(nameof(DynamicTestHub.Echo), "hello");

                Assert.Null(completion.Error);
                Assert.Equal("hello", completion.Result);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();
            }
        }
    }
示例#5
0
        public void JoinGameWithLobby()
        {
            string roomName = CreateRandomRoomName();

            TestClient client = this.InitClient();

            OperationRequest request = new OperationRequest((short)OperationCodes.Join);

            request.Params.Add((short)ParameterKeys.GameId, roomName);
            request.Params.Add((short)LiteLobby.Operations.LobbyParameterKeys.LobbyId, "Mainlobby");
            client.SendOperationRequest(request, 0, true);
            OperationResponse      response  = client.WaitForOperationResponse(this.WaitTime);
            EventReceivedEventArgs eventArgs = client.WaitForEvent(this.WaitTime);

            // check operation params
            CheckDefaultOperationParams(response, OperationCodes.Join);
            CheckParam(response, ParameterKeys.ActorNr, 1);

            // check event params
            CheckDefaultEventParams(eventArgs, OperationCodes.Join, 1);
            CheckEventParamExists(eventArgs, ParameterKeys.Actors);

            // cleanup
            client.Close();
            client.Dispose();
        }
 public void DisposeDoesNothingWhenNotLoggedIn()
 {
     var client = new TestClient();
     Assert.IsFalse(client.LoggedOut);
     client.Dispose();
     Assert.IsFalse(client.LoggedOut);
 }
示例#7
0
    public async Task ConnectionContinuesIfOnConnectedAsyncNotCalledByFilter()
    {
        using (StartVerifiableLog())
        {
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.EnableDetailedErrors = true;
                    options.AddFilter(new SkipNextFilter(skipOnConnected: true));
                });
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                // Verify connection still connected, can't invoke a method if the connection is disconnected
                var message = await client.InvokeAsync("Method");

                Assert.Equal("Failed to invoke 'Method' due to an error on the server. HubException: Method does not exist.", message.Error);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();
            }
        }
    }
示例#8
0
    public async Task FilterCanSkipCallingHubMethod()
    {
        using (StartVerifiableLog())
        {
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.AddFilter(new SkipNextFilter(skipInvoke: true));
                });
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                await client.Connected.DefaultTimeout();

                var message = await client.InvokeAsync(nameof(MethodHub.Echo), "Hello world!").DefaultTimeout();

                Assert.Null(message.Error);
                Assert.Null(message.Result);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();
            }
        }
    }
示例#9
0
    public async Task InstanceFiltersWithIAsyncDisposableAreNotDisposed()
    {
        using (StartVerifiableLog())
        {
            var tcsService      = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.EnableDetailedErrors = true;
                    options.AddFilter(new AsyncDisposableFilter(tcsService));
                });

                services.AddSingleton(tcsService);
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                var message = await client.InvokeAsync("Echo", "Hello");

                Assert.Equal("Hello", message.Result);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();

                Assert.False(tcsService.StartedMethod.Task.IsCompleted);
            }
        }
    }
示例#10
0
    public async Task InvokeFailsWhenFilterCallsNonExistantMethod()
    {
        bool ExpectedErrors(WriteContext writeContext)
        {
            return(writeContext.LoggerName == "Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher" &&
                   writeContext.EventId.Name == "FailedInvokingHubMethod");
        }

        using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors))
        {
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.EnableDetailedErrors = true;
                    options.AddFilter <ChangeMethodFilter>();
                });
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                var message = await client.InvokeAsync("Echo", "Hello");

                Assert.Equal("An unexpected error occurred invoking 'Echo' on the server. HubException: Unknown hub method 'BaseMethod'", message.Error);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();
            }
        }
    }
示例#11
0
    public async Task GlobalFiltersRunBeforeHubSpecificFilters()
    {
        using (StartVerifiableLog())
        {
            var syncPoint1      = SyncPoint.Create(3, out var syncPoints1);
            var syncPoint2      = SyncPoint.Create(3, out var syncPoints2);
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.AddFilter(new SyncPointFilter(syncPoints1));
                })
                .AddHubOptions <MethodHub>(options =>
                {
                    options.AddFilter(new SyncPointFilter(syncPoints2));
                });
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                await syncPoints1[0].WaitForSyncPoint().DefaultTimeout();
                // Second filter wont run yet because first filter is waiting on SyncPoint
                Assert.False(syncPoints2[0].WaitForSyncPoint().IsCompleted);
                syncPoints1[0].Continue();

                await syncPoints2[0].WaitForSyncPoint().DefaultTimeout();
                syncPoints2[0].Continue();
                await client.Connected.DefaultTimeout();

                var invokeTask = client.InvokeAsync(nameof(MethodHub.Echo), "Hello world!");

                await syncPoints1[1].WaitForSyncPoint().DefaultTimeout();
                // Second filter wont run yet because first filter is waiting on SyncPoint
                Assert.False(syncPoints2[1].WaitForSyncPoint().IsCompleted);
                syncPoints1[1].Continue();

                await syncPoints2[1].WaitForSyncPoint().DefaultTimeout();
                syncPoints2[1].Continue();
                var message = await invokeTask.DefaultTimeout();

                Assert.Null(message.Error);

                client.Dispose();

                await syncPoints1[2].WaitForSyncPoint().DefaultTimeout();
                // Second filter wont run yet because first filter is waiting on SyncPoint
                Assert.False(syncPoints2[2].WaitForSyncPoint().IsCompleted);
                syncPoints1[2].Continue();

                await syncPoints2[2].WaitForSyncPoint().DefaultTimeout();
                syncPoints2[2].Continue();

                await connectionHandlerTask.DefaultTimeout();
            }
        }
    }
        public void DisposeDoesNothingWhenNotLoggedIn()
        {
            var client = new TestClient();

            Assert.IsFalse(client.LoggedOut);
            client.Dispose();
            Assert.IsFalse(client.LoggedOut);
        }
 public void DisposeLogsOutWhenLoggedIn()
 {
     var client = new TestClient();
     Assert.IsFalse(client.LoggedOut);
     client.Login(new { });
     client.Dispose();
     Assert.IsTrue(client.LoggedOut);
 }
        public void DisposeLogsOutWhenLoggedIn()
        {
            var client = new TestClient();

            Assert.IsFalse(client.LoggedOut);
            client.Login(new { });
            client.Dispose();
            Assert.IsTrue(client.LoggedOut);
        }
示例#15
0
    public async Task MixingTypeAndInstanceHubSpecificFilters_MethodsAreCalled()
    {
        using (StartVerifiableLog())
        {
            var tcsService1     = new TcsService();
            var tcsService2     = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR()
                .AddHubOptions <MethodHub>(options =>
                {
                    options.AddFilter(new VerifyMethodFilter(tcsService1));
                    options.AddFilter <VerifyMethodFilter>();
                });

                services.AddSingleton(tcsService2);
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                await tcsService1.StartedMethod.Task.DefaultTimeout();

                await tcsService2.StartedMethod.Task.DefaultTimeout();

                await client.Connected.DefaultTimeout();

                await tcsService1.EndMethod.Task.DefaultTimeout();

                await tcsService2.EndMethod.Task.DefaultTimeout();

                tcsService1.Reset();
                tcsService2.Reset();
                var message = await client.InvokeAsync(nameof(MethodHub.Echo), "Hello world!").DefaultTimeout();

                await tcsService1.EndMethod.Task.DefaultTimeout();

                await tcsService2.EndMethod.Task.DefaultTimeout();

                tcsService1.Reset();
                tcsService2.Reset();

                Assert.Null(message.Error);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();

                await tcsService1.EndMethod.Task.DefaultTimeout();

                await tcsService2.EndMethod.Task.DefaultTimeout();
            }
        }
    }
示例#16
0
    public async Task FiltersHaveTransientScopeByDefault()
    {
        using (StartVerifiableLog())
        {
            var counter         = new FilterCounter();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.AddFilter <CounterFilter>();
                });

                services.AddSingleton(counter);
            }, LoggerFactory);

            var connectionHandler = serviceProvider.GetService <HubConnectionHandler <MethodHub> >();

            using (var client = new TestClient())
            {
                var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

                await client.Connected.DefaultTimeout();

                // Filter is transient, so these counts are reset every time the filter is created
                Assert.Equal(1, counter.OnConnectedAsyncCount);
                Assert.Equal(0, counter.InvokeMethodAsyncCount);
                Assert.Equal(0, counter.OnDisconnectedAsyncCount);

                var message = await client.InvokeAsync(nameof(MethodHub.Echo), "Hello world!").DefaultTimeout();

                // Filter is transient, so these counts are reset every time the filter is created
                Assert.Equal(0, counter.OnConnectedAsyncCount);
                Assert.Equal(1, counter.InvokeMethodAsyncCount);
                Assert.Equal(0, counter.OnDisconnectedAsyncCount);

                Assert.Null(message.Error);

                client.Dispose();

                await connectionHandlerTask.DefaultTimeout();

                // Filter is transient, so these counts are reset every time the filter is created
                Assert.Equal(0, counter.OnConnectedAsyncCount);
                Assert.Equal(0, counter.InvokeMethodAsyncCount);
                Assert.Equal(1, counter.OnDisconnectedAsyncCount);
            }
        }
    }
示例#17
0
 protected virtual void TearDown()
 {
     TestClient?.Dispose();
     TestServer?.Dispose();
     WebSocketClient = null;
 }
示例#18
0
 public void Dispose()
 {
     server.Dispose();
     client.Dispose();
     Directory.Delete(rootPath, true);
 }
 public void Dispose()
 {
     TestClient.Dispose();
     _testServer.Dispose();
 }
 public void ShutdownWebAppAndCreateClient()
 {
     _client.Get("/quit").GetAwaiter().GetResult();
     _webApp.StopAndWaitForExit();
     _client.Dispose();
 }
示例#21
0
 public void TearDown()
 {
     // These disposes should free the metric port. If not, the second SetUp call will fail.
     _server.Dispose();
     _client.Dispose();
 }
 public void Dispose()
 {
     server.Dispose();
     client.Dispose();
 }
示例#23
0
 public void Dispose()
 {
     _client.Dispose();
 }