Part 2 - Add unit tests for Microsoft.Bot.Streaming library (#5840)
Co-authored-by: sw-joelmut <joel.mut@southworks.com>
This commit is contained in:
Родитель
db098e7655
Коммит
64a2df8a24
|
@ -295,6 +295,35 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.Equal(0, readCount2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void DoneProducing_Data_WillCauseZeroRead_And_End()
|
||||
{
|
||||
const int expectedReadCount = 100;
|
||||
var producerBuffer = new byte[100];
|
||||
var consumerBuffer = new byte[expectedReadCount];
|
||||
var readCount = 0;
|
||||
|
||||
var streamManager = new StreamManager(e => { });
|
||||
var id = Guid.NewGuid();
|
||||
|
||||
var assembler = streamManager.GetPayloadAssembler(id);
|
||||
assembler.ContentLength = 0;
|
||||
|
||||
var random = new Random();
|
||||
random.NextBytes(producerBuffer);
|
||||
|
||||
var stream = new PayloadStream(assembler);
|
||||
|
||||
await stream.WriteAsync(producerBuffer, 0, producerBuffer.Length);
|
||||
stream.DoneProducing();
|
||||
|
||||
readCount = stream.Read(consumerBuffer, 0, expectedReadCount);
|
||||
Assert.Equal(100, readCount);
|
||||
|
||||
readCount = stream.Read(consumerBuffer, readCount, expectedReadCount);
|
||||
Assert.Equal(0, readCount);
|
||||
}
|
||||
|
||||
private async Task ProducerConsumerMultithreadedTest(
|
||||
int producerTotalCount,
|
||||
int producerChunkCount,
|
||||
|
|
|
@ -24,8 +24,10 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
var id = Guid.NewGuid();
|
||||
var assembler = new PayloadStreamAssembler(null, id);
|
||||
var c = new ContentStream(id, assembler);
|
||||
var stream = assembler.GetPayloadAsStream();
|
||||
|
||||
Assert.Equal(id, c.Id);
|
||||
Assert.Equal(stream, c.Stream);
|
||||
|
||||
c.Cancel();
|
||||
}
|
||||
|
@ -44,5 +46,20 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
|
||||
c.Cancel();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContentStream_Length()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
var assembler = new PayloadStreamAssembler(null, id);
|
||||
var content = new ContentStream(id, assembler);
|
||||
var length = 3;
|
||||
|
||||
content.Length = 3;
|
||||
|
||||
Assert.Equal(length, content.Length);
|
||||
|
||||
content.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,5 +54,76 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
writeStream.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NamedPipeClient_ctor_With_Empty_BaseName()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new NamedPipeClient(string.Empty));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NamedPipeClient_SendAsync_With_No_Message()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var pipe = new NamedPipeClient(pipeName);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => pipe.SendAsync(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NamedPipeClient_SendAsync_With_No_Connected_Client()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var pipe = new NamedPipeClient(pipeName);
|
||||
var message = new StreamingRequest();
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => pipe.SendAsync(message));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NamedPipeServer_IsConnected()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var requestHandler = new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName);
|
||||
var pipe = new NamedPipeServer(pipeName, requestHandler);
|
||||
|
||||
Assert.False(pipe.IsConnected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NamedPipeServer_ctor_With_Empty_BaseName()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var requestHandler = new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName);
|
||||
Assert.Throws<ArgumentNullException>(() => new NamedPipeServer(string.Empty, requestHandler));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NamedPipeServer_ctor_With_No_RequestHandler()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
Assert.Throws<ArgumentNullException>(() => new NamedPipeServer(pipeName, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NamedPipeServer_SendAsync_With_No_Message()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var requestHandler = new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName);
|
||||
var pipe = new NamedPipeServer(pipeName, requestHandler);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => pipe.SendAsync(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NamedPipeServer_SendAsync_With_No_Connected_Client()
|
||||
{
|
||||
var pipeName = Guid.NewGuid().ToString().Substring(0, 18);
|
||||
var requestHandler = new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName);
|
||||
var pipe = new NamedPipeServer(pipeName, requestHandler);
|
||||
var message = new StreamingRequest();
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => pipe.SendAsync(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,6 +78,15 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void ReceiveAsync_With_Null_Stream()
|
||||
{
|
||||
var pipe = new NamedPipeTransport(null);
|
||||
var result = await pipe.ReceiveAsync(new byte[0], 0, 0);
|
||||
|
||||
Assert.Equal(0, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClosedStream_Not_IsConnected()
|
||||
{
|
||||
|
|
|
@ -3,10 +3,13 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Bot.Schema;
|
||||
using Microsoft.Bot.Streaming.Payloads;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
|
@ -30,6 +33,46 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.Null(r.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsJson_Streams()
|
||||
{
|
||||
var activity = new Activity { Type = ActivityTypes.Message };
|
||||
var stringInput = JsonConvert.SerializeObject(activity);
|
||||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(stringInput));
|
||||
var mockContentStream = new Mock<IContentStream>();
|
||||
mockContentStream.Setup(e => e.Stream).Returns(stream);
|
||||
|
||||
var request = new ReceiveRequest
|
||||
{
|
||||
Streams = new List<IContentStream> { mockContentStream.Object }
|
||||
};
|
||||
var result = request.ReadBodyAsJson<Activity>();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(activity.Type, result.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsJson_Streams_Zero()
|
||||
{
|
||||
var request = new ReceiveRequest();
|
||||
var result = request.ReadBodyAsJson<dynamic>();
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsString_Streams_Zero()
|
||||
{
|
||||
var request = new ReceiveRequest();
|
||||
var result = request.ReadBodyAsString();
|
||||
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_NullProperties()
|
||||
{
|
||||
|
@ -132,6 +175,14 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.Equal(s, r.Streams[0].Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_Create_WithEmptyMethod()
|
||||
{
|
||||
var request = StreamingRequest.CreateRequest(string.Empty);
|
||||
|
||||
Assert.Null(request);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RequestExtensions_SetBodyString_Success()
|
||||
{
|
||||
|
@ -166,6 +217,26 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestExtensions_SetBodyObject_Null_Does_Not_Throw()
|
||||
{
|
||||
Exception ex = null;
|
||||
|
||||
var r = new StreamingRequest();
|
||||
try
|
||||
{
|
||||
r.SetBody((object)null);
|
||||
}
|
||||
catch (Exception caughtEx)
|
||||
{
|
||||
ex = caughtEx;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Assert.Null(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RequestExtensions_SetBody_Success()
|
||||
{
|
||||
|
|
|
@ -3,11 +3,14 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Bot.Schema;
|
||||
using Microsoft.Bot.Streaming.Payloads;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
|
@ -161,6 +164,26 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResponseExtensions_SetBodyObject_Null_Does_Not_Throw()
|
||||
{
|
||||
var r = new StreamingResponse();
|
||||
Exception ex = null;
|
||||
|
||||
try
|
||||
{
|
||||
r.SetBody(null as object);
|
||||
}
|
||||
catch (Exception caughtEx)
|
||||
{
|
||||
ex = caughtEx;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Assert.Null(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResponseExtensions_SetBody_Success()
|
||||
{
|
||||
|
@ -207,5 +230,58 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
|
||||
Assert.Equal(string.Empty, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsJson_Streams()
|
||||
{
|
||||
var activity = new Activity { Type = ActivityTypes.Message };
|
||||
var stringInput = JsonConvert.SerializeObject(activity);
|
||||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(stringInput));
|
||||
var mockContentStream = new Mock<IContentStream>();
|
||||
mockContentStream.Setup(e => e.Stream).Returns(stream);
|
||||
|
||||
var response = new ReceiveResponse
|
||||
{
|
||||
Streams = new List<IContentStream> { mockContentStream.Object }
|
||||
};
|
||||
var result = response.ReadBodyAsJson<Activity>();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(activity.Type, result.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsJson_Streams_Zero()
|
||||
{
|
||||
var response = new ReceiveResponse
|
||||
{
|
||||
StatusCode = 3,
|
||||
};
|
||||
var result = response.ReadBodyAsJson<dynamic>();
|
||||
|
||||
Assert.Null(result);
|
||||
Assert.Equal(3, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void ReceiveExtensions_ReadBodyAsString_Streams()
|
||||
{
|
||||
const string stringInput = "message";
|
||||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(stringInput));
|
||||
var mockContentStream = new Mock<IContentStream>();
|
||||
mockContentStream.Setup(e => e.Stream).Returns(stream);
|
||||
|
||||
var response = new ReceiveResponse
|
||||
{
|
||||
Streams = new List<IContentStream> { mockContentStream.Object }
|
||||
};
|
||||
var result = response.ReadBodyAsString();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(stringInput, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
@ -82,6 +83,52 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.Equal(4, transport.Buffers.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendResponseAsync()
|
||||
{
|
||||
var sender = new PayloadSender();
|
||||
var transport = new MockTransportSender();
|
||||
sender.Connect(transport);
|
||||
var ops = new SendOperations(sender);
|
||||
|
||||
var content = new StringContent("/a/b", Encoding.ASCII);
|
||||
var response = StreamingResponse.CreateResponse(HttpStatusCode.OK, content);
|
||||
|
||||
await ops.SendResponseAsync(Guid.NewGuid(), response);
|
||||
|
||||
Assert.Equal(4, transport.Buffers.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendCancelAllAsync()
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
var sender = new MockPayloadSender();
|
||||
var ops = new SendOperations(sender);
|
||||
|
||||
await ops.SendCancelAllAsync(guid);
|
||||
|
||||
var header = sender.SentHeaders.FirstOrDefault();
|
||||
Assert.Equal(guid, header.Id);
|
||||
Assert.Equal(PayloadTypes.CancelAll, header.Type);
|
||||
Assert.True(header.End);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendCancelStreamAsync()
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
var sender = new MockPayloadSender();
|
||||
var ops = new SendOperations(sender);
|
||||
|
||||
await ops.SendCancelStreamAsync(guid);
|
||||
|
||||
var header = sender.SentHeaders.FirstOrDefault();
|
||||
Assert.Equal(guid, header.Id);
|
||||
Assert.Equal(PayloadTypes.CancelStream, header.Type);
|
||||
Assert.True(header.End);
|
||||
}
|
||||
|
||||
// Creates a stream that throws if read from after Disposal. Otherwise returns a buffer of byte data
|
||||
private Stream GetMockedStream(int length)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Bot.Streaming.PayloadTransport;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Bot.Streaming.UnitTests
|
||||
{
|
||||
public class TransportDisconnectedExceptionTests
|
||||
{
|
||||
[Fact]
|
||||
|
||||
public void TransportDisconnectedException_ctor()
|
||||
{
|
||||
var exception = new TransportDisconnectedException();
|
||||
|
||||
Assert.NotNull(exception.Reason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
public void TransportDisconnectedException_ctor_With_InnerException()
|
||||
{
|
||||
var innerException = new Exception("inner-exception");
|
||||
var exception = new TransportDisconnectedException("exception", innerException);
|
||||
|
||||
Assert.Equal(innerException, exception.InnerException);
|
||||
Assert.Equal("exception", exception.Reason);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -59,6 +59,44 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.False(isConnectedAfterDisconnect);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebSocketServer_ctor_With_No_Socket()
|
||||
{
|
||||
var requestHandlerMock = new Mock<RequestHandler>();
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new WebSocketServer(null, requestHandlerMock.Object));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebSocketServer_ctor_With_No_RequestHandler()
|
||||
{
|
||||
var requestHandlerMock = new Mock<RequestHandler>();
|
||||
var socketMock = new Mock<WebSocket>();
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new WebSocketServer(socketMock.Object, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WebSocketServer_SendAsync_With_No_Message()
|
||||
{
|
||||
var socketMock = new Mock<WebSocket>();
|
||||
var requestHandlerMock = new Mock<RequestHandler>();
|
||||
var server = new WebSocketServer(socketMock.Object, requestHandlerMock.Object);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => server.SendAsync(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WebSocketServer_SendAsync_With_No_Connected_Client()
|
||||
{
|
||||
var socketMock = new Mock<WebSocket>();
|
||||
var requestHandlerMock = new Mock<RequestHandler>();
|
||||
var server = new WebSocketServer(socketMock.Object, requestHandlerMock.Object);
|
||||
var message = new StreamingRequest();
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync(message));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WebSocketClient_ThrowsOnEmptyUrl()
|
||||
{
|
||||
|
@ -76,6 +114,23 @@ namespace Microsoft.Bot.Streaming.UnitTests
|
|||
Assert.IsType<ArgumentNullException>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WebSocketClient_SendAsync_With_No_Message()
|
||||
{
|
||||
var client = new WebSocketClient("url");
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.SendAsync(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WebSocketClient_SendAsync_With_No_Connected_Client()
|
||||
{
|
||||
var client = new WebSocketClient("url");
|
||||
var message = new StreamingRequest();
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => client.SendAsync(message));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WebSocketClient_AcceptsAnyUrl()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче