public async Task ShouldSend() { //Arrange var clouldQueueClientMock = new Mock <CloudQueueClient>(MockBehavior.Loose, new Uri("http://localhost"), null, null); var clouldQueueMock = new Mock <CloudQueue>(MockBehavior.Loose, new Uri("http://localhost"), null); var configurationMock = new Mock <IConfiguration>(); var messageQueueOptions = new MessageQueueOptions(); clouldQueueMock.Setup(x => x.AddMessageAsync(It.IsAny <CloudQueueMessage>())) .Returns(Task.FromResult(new CloudQueueMessage(new byte[0]))) .Verifiable(); clouldQueueClientMock.Setup(x => x.GetQueueReference(It.IsAny <string>())) .Returns(clouldQueueMock.Object) .Verifiable(); configurationMock.SetupGet(x => x[It.IsAny <string>()]).Returns(string.Empty); var queue = new MessageQueueAzureStorageQueue(clouldQueueClientMock.Object, messageQueueOptions, new MessageQueueInfo(messageQueueOptions, configurationMock.Object)); var msg = new TestMessage() { Id = Guid.NewGuid().ToString() }; //Act await queue.Send(msg); //Assert clouldQueueClientMock.VerifyAll(); clouldQueueMock.VerifyAll(); }
public async Task WhenReceiveShouldValidateMaxNumberOfMessages() { //Arrange const int maxNumberOfMessages = 0; var clouldQueueClientMock = new Mock <CloudQueueClient>(MockBehavior.Loose, new Uri("http://localhost"), null, null); var clouldQueueMock = new Mock <CloudQueue>(MockBehavior.Loose, new Uri("http://localhost"), null); var configurationMock = new Mock <IConfiguration>(); var messageQueueOptions = new MessageQueueOptions(); var msg = new TestMessage() { Id = Guid.NewGuid().ToString() }; clouldQueueMock.Setup(x => x.GetMessageAsync()) .ReturnsAsync(new CloudQueueMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg)))) .Verifiable(); clouldQueueClientMock.Setup(x => x.GetQueueReference(It.IsAny <string>())) .Returns(clouldQueueMock.Object) .Verifiable(); configurationMock.SetupGet(x => x[It.IsAny <string>()]).Returns(string.Empty); var queue = new MessageQueueAzureStorageQueue(clouldQueueClientMock.Object, messageQueueOptions, new MessageQueueInfo(messageQueueOptions, configurationMock.Object)); //Act Func <Task> act = async() => await queue.Receive <TestMessage>(maxNumberOfMessages) .GetAsyncEnumerator() .MoveNextAsync(); //Assert await Assert.ThrowsAsync <ArgumentException>(act); }
public async Task ShouldReceiveWithCancellationToken() { //Arrange const int delay = 1; var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(delay)).Token; var clouldQueueClientMock = new Mock <CloudQueueClient>(MockBehavior.Loose, new Uri("http://localhost"), null, null); var clouldQueueMock = new Mock <CloudQueue>(MockBehavior.Loose, new Uri("http://localhost"), null); var configurationMock = new Mock <IConfiguration>(); var messageQueueOptions = new MessageQueueOptions(); var msg = new TestMessage() { Id = Guid.NewGuid().ToString() }; clouldQueueMock.Setup(x => x.GetMessagesAsync(It.IsAny <int>(), cancellationToken)) .ReturnsAsync(new CloudQueueMessage[1] { new CloudQueueMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg))) }).Verifiable(); clouldQueueClientMock.Setup(x => x.GetQueueReference(It.IsAny <string>())) .Returns(clouldQueueMock.Object) .Verifiable(); configurationMock.SetupGet(x => x[It.IsAny <string>()]).Returns(string.Empty); var queue = new MessageQueueAzureStorageQueue(clouldQueueClientMock.Object, messageQueueOptions, new MessageQueueInfo(messageQueueOptions, configurationMock.Object)); //Act var qtd = 0; await foreach (var message in queue.Receive <TestMessage>(cancellationToken)) { qtd++; } //Assert clouldQueueClientMock.VerifyAll(); clouldQueueMock.VerifyAll(); Assert.True(qtd > 0); }