public async Task SendAsync_MultipleParallelNotifications_ClientShouldReceive() { // Arrange var count = Dummy.CreateRandomInt(100) + 1; var notifications = Enumerable.Range(0, count) .Select(i => { var notification = Dummy.CreateNotification(Event.Consumed); notification.Id = EnvelopeId.NewId(); return(notification); }) .ToList(); var(clientTransport, serverTransport) = await GetAndOpenTargetsAsync(); clientTransport = new SynchronizedTransportDecorator(clientTransport); serverTransport = new SynchronizedTransportDecorator(serverTransport); // Act var sendTasks = new List <Task>(); foreach (var notification in notifications) { sendTasks.Add( Task.Run(() => serverTransport.SendAsync(notification, CancellationToken), CancellationToken)); } var receiveTasks = new List <Task <Envelope> >(); while (count-- > 0) { receiveTasks.Add( Task.Run(() => clientTransport.ReceiveAsync(CancellationToken), CancellationToken)); } await Task.WhenAll(sendTasks.Concat(receiveTasks)); var actuals = receiveTasks.Select(t => t.Result).ToList(); // Assert actuals.Count.ShouldBe(notifications.Count); foreach (var notification in notifications) { var actualEnvelope = actuals.FirstOrDefault(e => e.Id == notification.Id); actualEnvelope.ShouldNotBeNull(); var actualNotification = actualEnvelope.ShouldBeOfType <Notification>(); CompareNotifications(notification, actualNotification); } }
public async Task ReceiveAsync_MultipleBiggerThanBufferSizeMessageEnvelope_ServerShouldReceive() { // Arrange BufferSize = 64; ServerMaxBufferSize = ClientMaxBufferSize = BufferSize * 10; var count = Dummy.CreateRandomInt(100); var(clientTransport, serverTransport) = await GetAndOpenTargetsAsync(); var synchronizedserverTransport = new SynchronizedTransportDecorator(serverTransport); var synchronizedClient = new SynchronizedTransportDecorator(clientTransport); var messages = new Message[count]; for (int i = 0; i < count; i++) { messages[i] = Dummy.CreateMessage(Dummy.CreateRandomString(BufferSize * (3 + i % 2))); } var actuals = new Envelope[count]; // Act for (int i = 0; i < count; i++) { await synchronizedClient.SendAsync(messages[i], CancellationToken); actuals[i] = await synchronizedserverTransport.ReceiveAsync(CancellationToken); } // Assert for (int i = 0; i < count; i++) { var actual = actuals[i]; var message = messages[i]; actual.ShouldNotBeNull(); var actualMessage = actual.ShouldBeOfType <Message>(); CompareMessages(message, actualMessage); } var jsonBuffer = GetJsonBuffer(serverTransport); jsonBuffer.Buffer.Length.ShouldBe(BufferSize); }
private async Task SetupAsync() { TransportListener = CreateTransportListener(); await TransportListener.StartAsync(CancellationToken); var serverTcpTransportTask = TransportListener.AcceptTransportAsync(CancellationToken); ClientTransport = CreateClientTransport(); await ClientTransport.OpenAsync(Uri, CancellationToken); SynchronizedClientTransport = new SynchronizedTransportDecorator(ClientTransport); ServerTransport = await serverTcpTransportTask; await ServerTransport.OpenAsync(Uri, CancellationToken); SynchronizedServerTransport = new SynchronizedTransportDecorator(ServerTransport); Message = Dummy.CreateMessage(Dummy.CreateTextContent()); }