public async void MessageSentToSubscribedGroup(string messageText) { var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane, RedisBackplane>(); o.Configure <ConfigurationOptions>(options => { options.EndPoints.Add(REDIS_URI); }); }); var backplane = (RedisBackplane)serviceProvider.GetRequiredService <IBackplane>(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await backplane.Subscribe("some-group", connection.Id); await backplane.SendMessageGroupAsync("some-group", new Message() { MessageType = MessageType.Text, Data = messageText }); await Task.Delay(1000); var message = await ReadMessageFromSocketAsync(webSocket); Assert.Equal(MessageType.Text, message.MessageType); Assert.Equal(messageText, message.Data); }
public async Task Additional_Middleware_Should_Be_Given_The_Mutated_Context() { var middleware = new List <MorseL.Sockets.Middleware.IMiddleware> { new TestMiddleware("First"), new TestMiddleware("Second"), new TestMiddleware("Third") }; string originalContents = "test stream data"; string contents = null; var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(originalContents)); var mockChannel = new Mock <IChannel>(); var connection = new Connection("connectionId", mockChannel.Object); var connectionContext = new MorseL.Sockets.Middleware.ConnectionContext(connection, inputStream); var socket = new LinkedFakeSocket(); ILoggerFactory loggerFactory = new LoggerFactory(); var webSocketChannel = new WebSocketChannel(socket, middleware, loggerFactory); var delegator = webSocketChannel.BuildMiddlewareDelegate(middleware.GetEnumerator(), async stream => { using (var memStream = new MemoryStream()) { await stream.CopyToAsync(memStream); contents = Encoding.UTF8.GetString(memStream.ToArray()); } }); await delegator.Invoke(connectionContext); string expectedResults = $"SENTThird:SENTSecond:SENTFirst:{originalContents}"; Assert.Equal(expectedResults, contents); }
public async void HubActivatorReleasedWhenExceptionThrownInOnConnectedAsync() { var serviceProvider = CreateServiceProvider(s => s.AddSingleton(typeof(IHubActivator <,>), typeof(DefaultHubActivator <,>))); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <BadHub> >(); var hubActivator = serviceProvider.GetRequiredService <IHubActivator <BadHub, IClientInvoker> > (); var webSocket = new LinkedFakeSocket(); await Assert.ThrowsAnyAsync <Exception>(() => actualHub.OnConnected(webSocket, new DefaultHttpContext())); Assert.True(((DefaultHubActivator <BadHub, IClientInvoker>)hubActivator)._disposed); }
[InlineData("{}")] // Valid JSON, invalid message // (Note: valid message will work and return invocation error result) public async void CannotCallInvalidInvocationRequest(string message) { var serviceProvider = CreateServiceProvider(s => s.Configure <Extensions.MorseLOptions>(o => o.ThrowOnInvalidMessage = true)); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); var exception = await Assert.ThrowsAsync <MorseLException>(() => SendMessageToSocketAsync(actualHub, connection, message)); Assert.Equal($"Invalid message received \"{message}\" from {connection.Id}", exception.Message); }
public async Task SendAsyncMiddlewareIsCalledOnArbitrarySendAsync(string text) { var socket = new LinkedFakeSocket(); ILoggerFactory loggerFactory = new LoggerFactory(); var webSocketChannel = new WebSocketChannel(socket, new[] { new Base64Middleware() }, loggerFactory); await webSocketChannel.SendAsync(new MemoryStream(Encoding.UTF8.GetBytes(text))); var encodedMessage = await socket.ReadToEndAsync(); var message = Encoding.UTF8.GetString(Convert.FromBase64String(encodedMessage)); Assert.Equal(message, text); }
public async void ClientConnectCallsBackplaneOnClientConnected() { var backplane = new TestBackplane(); var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane>(backplane); }); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var exception = await Assert.ThrowsAnyAsync <NotImplementedException>( () => CreateHubConnectionFromSocket(actualHub, webSocket)); Assert.Equal(nameof(TestBackplane.OnClientConnectedAsync), exception.Message); }
public async void SubscriptionRemovedOnUnsubscribe() { var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane, RedisBackplane>(); o.Configure <ConfigurationOptions>(options => { options.EndPoints.Add(REDIS_URI); }); }); var backplane = (RedisBackplane)serviceProvider.GetRequiredService <IBackplane>(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); // Subscribe to n groups await backplane.Subscribe("some-group", connection.Id); await backplane.Subscribe("some-other-group", connection.Id); // Make sure our group list contains them Assert.Contains("some-group", backplane.Groups.Keys); Assert.Contains("some-other-group", backplane.Groups.Keys); // Make sure out subscription list contains them Assert.Contains(connection.Id, backplane.Subscriptions.Keys); Assert.Contains("some-group", backplane.Subscriptions[connection.Id].Keys); Assert.Contains("some-other-group", backplane.Subscriptions[connection.Id].Keys); // Unsubscribe from one group await backplane.Unsubscribe("some-group", connection.Id); // Validate that group has been removed Assert.DoesNotContain("some-group", backplane.Groups.Keys); Assert.DoesNotContain("some-group", backplane.Subscriptions[connection.Id].Keys); // Make sure the other group is still subscribed Assert.Contains("some-other-group", backplane.Groups.Keys); Assert.Contains(connection.Id, backplane.Subscriptions.Keys); Assert.Contains("some-other-group", backplane.Subscriptions[connection.Id].Keys); // Unsubscribe from the final group await backplane.Unsubscribe("some-other-group", connection.Id); // Validate that both groups have been unsubscribed and individual collections are gone Assert.DoesNotContain("some-group", backplane.Groups.Keys); Assert.DoesNotContain("some-other-group", backplane.Groups.Keys); Assert.DoesNotContain(connection.Id, backplane.Subscriptions.Keys); }
public async void CanCallReturnIntAsyncMethodOnHub() { var serviceProvider = CreateServiceProvider(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await SendMessageToSocketAsync(actualHub, connection, nameof(TestHub.IntMethodAsync), null); var result = await ReadInvocationResultFromSocket <int>(webSocket); Assert.NotNull(result); Assert.Equal(result.Result, TestHub.IntResult); }
public async void SubscriptionAddedOnSubscribe() { var backplane = new DefaultBackplane(); var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane>(backplane); }); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await backplane.Subscribe("some-group", connection.Id); Assert.Contains("some-group", backplane.Groups.Keys); }
public async void CannotCallInvalidMethodName(string methodName, params object[] arguments) { var serviceProvider = CreateServiceProvider(s => s.Configure <Extensions.MorseLOptions>(o => o.ThrowOnMissingHubMethodRequest = true)); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); var expectedMethodName = string.IsNullOrWhiteSpace(methodName) ? "[Invalid Method Name]" : methodName; var expectedArgumentList = arguments?.Length > 0 ? String.Join(", ", arguments) : "[No Parameters]"; var exception = await Assert.ThrowsAsync <MorseLException>(() => SendMessageToSocketAsync(actualHub, connection, methodName, arguments)); Assert.Equal($"Invalid method request received from {connection.Id}; method is \"{expectedMethodName}({expectedArgumentList})\"", exception.Message); }
public async void ClientConnectAndDisconnectCleansUpBackplaneEventHandlers() { var backplane = new DefaultBackplane(); var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane>(backplane); }); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await actualHub.OnDisconnected(webSocket, null); Assert.Equal(0, backplane.OnMessageCount); }
public async Task SendingDisconnectClientAsync_DisconnectsClient() { var backplane = new DefaultBackplane(); var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane>(backplane); }); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await backplane.DisconnectClientAsync(connection.Id); Assert.True(webSocket.CloseCalled); }
public async Task SendAsyncMiddlewareIsCalledOnArbitrarySendMessageAsync(string text) { var socket = new LinkedFakeSocket(); ILoggerFactory loggerFactory = new LoggerFactory(); var webSocketChannel = new WebSocketChannel(socket, new [] { new Base64Middleware() }, loggerFactory); await webSocketChannel.SendMessageAsync(new Message { Data = text, MessageType = MessageType.Text }); var encodedMessage = await socket.ReadToEndAsync(); var message = MessageSerializer.Deserialize <Message>(Encoding.UTF8.GetString(Convert.FromBase64String(encodedMessage))); Assert.Equal(message.Data, text); }
public async Task SendAsyncMiddlewareCalledInOrderOnArbitrarySendAsync(string text) { var middlewares = new List <IMiddleware>(); for (int i = 0; i < 10; i++) { middlewares.Add(new IncrementalMiddleware()); } var socket = new LinkedFakeSocket(); ILoggerFactory loggerFactory = new LoggerFactory(); var webSocketChannel = new WebSocketChannel(socket, middlewares, loggerFactory); await webSocketChannel.SendAsync(new MemoryStream(Encoding.UTF8.GetBytes(text))); foreach (var m in middlewares) { var middleware = (IncrementalMiddleware)m; Assert.Equal(middleware.Id, middleware.CalledAt); } }
public async void ConnectionSubscriptionRemovedOnNormalDisconnect() { var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane, RedisBackplane>(); o.Configure <ConfigurationOptions>(options => { options.EndPoints.Add(REDIS_URI); }); }); var backplane = (RedisBackplane)serviceProvider.GetRequiredService <IBackplane>(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await actualHub.OnDisconnected(webSocket, null); Assert.DoesNotContain(connection.Id, backplane.Connections.Keys); }
public async void SubscriptionAddedOnSubscribe() { var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane, RedisBackplane>(); o.Configure <ConfigurationOptions>(options => { options.EndPoints.Add(REDIS_URI); }); }); var backplane = (RedisBackplane)serviceProvider.GetRequiredService <IBackplane>(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await backplane.Subscribe("some-group", connection.Id); Assert.Contains("some-group", backplane.Groups.Keys); }
public async void ClientConnectAndDisconnectCleansUpBackplaneEventHandlers() { var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane, RedisBackplane>(); o.Configure <ConfigurationOptions>(options => { options.EndPoints.Add(REDIS_URI); }); }); var backplane = (RedisBackplane)serviceProvider.GetRequiredService <IBackplane>(); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); Assert.Contains(connection.Id, backplane.Connections.Keys); await actualHub.OnDisconnected(webSocket, null); Assert.Equal(0, backplane.OnMessageCount); }
public async void MessageSentToSubscribedGroup(string messageText) { var backplane = new DefaultBackplane(); var serviceProvider = CreateServiceProvider(o => { o.AddSingleton <IBackplane>(backplane); }); var actualHub = serviceProvider.GetRequiredService <HubWebSocketHandler <TestHub> >(); var webSocket = new LinkedFakeSocket(); var connection = await CreateHubConnectionFromSocket(actualHub, webSocket); await backplane.Subscribe("some-group", connection.Id); await backplane.SendMessageGroupAsync("some-group", new Message() { MessageType = MessageType.Text, Data = messageText }); var message = await ReadMessageFromSocketAsync(webSocket); Assert.Equal(MessageType.Text, message.MessageType); Assert.Equal(messageText, message.Data); }
private async Task <Connection> CreateHubConnectionFromSocket(HubWebSocketHandler <TestHub> actualHub, LinkedFakeSocket webSocket) { var connection = await actualHub.OnConnected(webSocket, new DefaultHttpContext()); // Receive the connection message var connectMessage = await ReadMessageFromSocketAsync(webSocket); Assert.NotNull(connectMessage); Assert.NotNull(connectMessage.Data); Assert.NotEqual(Guid.Empty, Guid.Parse(connectMessage.Data)); return(connection); }