Beispiel #1
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();
            }
        }
    }
Beispiel #2
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);
            }
        }
    }
Beispiel #3
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();
        }
    }
Beispiel #4
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();
            }
        }
    }
Beispiel #5
0
    public async Task PerHubFilterByInstance_MethodsAreCalled()
    {
        using (StartVerifiableLog())
        {
            var tcsService      = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR().AddHubOptions <MethodHub>(options =>
                {
                    options.AddFilter(new VerifyMethodFilter(tcsService));
                });
            }, LoggerFactory);

            await AssertMethodsCalled(serviceProvider, tcsService);
        }
    }
Beispiel #6
0
    public async Task GlobalHubFilterByType_MethodsAreCalled()
    {
        using (StartVerifiableLog())
        {
            var tcsService      = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.AddFilter <VerifyMethodFilter>();
                });

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

            await AssertMethodsCalled(serviceProvider, tcsService);
        }
    }
Beispiel #7
0
    public async Task FilterCanBeResolvedFromDI()
    {
        using (StartVerifiableLog())
        {
            var tcsService      = new TcsService();
            var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
            {
                services.AddSignalR(options =>
                {
                    options.AddFilter <VerifyMethodFilter>();
                });

                // If this instance wasn't resolved, then the tcsService.StartedMethod waits would never trigger and fail the test
                services.AddSingleton(new VerifyMethodFilter(tcsService));
            }, LoggerFactory);

            await AssertMethodsCalled(serviceProvider, tcsService);
        }
    }
Beispiel #8
0
 public AsyncDisposableFilter(TcsService tcsService)
 {
     _tcsService = tcsService;
 }
Beispiel #9
0
 public VerifyMethodFilter(TcsService tcsService)
 {
     _service = tcsService;
 }