Пример #1
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.OrTimeout();

                    Assert.False(tcsService.StartedMethod.Task.IsCompleted);
                }
            }
        }
Пример #2
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.OrTimeout();

                await client.Connected.OrTimeout();

                await tcsService.EndMethod.Task.OrTimeout();

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

                await tcsService.EndMethod.Task.OrTimeout();

                tcsService.Reset();

                Assert.Null(message.Error);

                client.Dispose();

                await connectionHandlerTask.OrTimeout();

                await tcsService.EndMethod.Task.OrTimeout();
            }
        }
Пример #3
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();
                }
            }
        }
Пример #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.OrTimeout();

                    await tcsService2.StartedMethod.Task.OrTimeout();

                    await client.Connected.OrTimeout();

                    await tcsService1.EndMethod.Task.OrTimeout();

                    await tcsService2.EndMethod.Task.OrTimeout();

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

                    await tcsService1.EndMethod.Task.OrTimeout();

                    await tcsService2.EndMethod.Task.OrTimeout();

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

                    Assert.Null(message.Error);

                    client.Dispose();

                    await connectionHandlerTask.OrTimeout();

                    await tcsService1.EndMethod.Task.OrTimeout();

                    await tcsService2.EndMethod.Task.OrTimeout();
                }
            }
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #8
0
        public async Task FiltersWithIAsyncDisposableAreDisposed()
        {
            using (StartVerifiableLog())
            {
                var tcsService      = new TcsService();
                var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
                {
                    services.AddSignalR(options =>
                    {
                        options.EnableDetailedErrors = true;
                        options.AddFilter <AsyncDisposableFilter>();
                    });

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

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

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

                    // OnConnectedAsync creates and destroys the filter
                    await tcsService.StartedMethod.Task.DefaultTimeout();

                    tcsService.Reset();

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

                    Assert.Equal("Hello", message.Result);
                    await tcsService.StartedMethod.Task.DefaultTimeout();

                    tcsService.Reset();

                    client.Dispose();

                    // OnDisconnectedAsync creates and destroys the filter
                    await tcsService.StartedMethod.Task.DefaultTimeout();

                    await connectionHandlerTask.DefaultTimeout();
                }
            }
        }
Пример #9
0
 public LongRunningHub(TcsService tcsService)
 {
     _tcsService = tcsService;
 }
Пример #10
0
 public AsyncDisposableFilter(TcsService tcsService)
 {
     _tcsService = tcsService;
 }
Пример #11
0
 public VerifyMethodFilter(TcsService tcsService)
 {
     _service = tcsService;
 }