示例#1
0
        public void AddMessageAsync_small_message()
        {
            // Arrange
            var queueName         = "myqueue";
            var mockQueue         = GetMockQueue(queueName);
            var mockQueueClient   = GetMockQueueClient(mockQueue);
            var mockBlobContainer = GetMockBlobContainer();
            var mockBlobClient    = GetMockBlobClient(mockBlobContainer);
            var storageAccount    = GetMockStorageAccount(mockBlobClient, mockQueueClient);

            mockQueue
            .Setup(c => c.AddMessageAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

            // Act
            var queueManager = new QueueManager(queueName, storageAccount.Object);

            queueManager.AddMessageAsync("Hello world!").Wait();

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();
        }
示例#2
0
        public void AddMessageAsync_large_message_is_compressed()
        {
            // Arrange
            var queueName         = "myqueue";
            var mockQueue         = Misc.GetMockQueue(queueName);
            var mockQueueClient   = Misc.GetMockQueueClient(mockQueue);
            var mockBlobContainer = Misc.GetMockBlobContainer();
            var mockBlobClient    = Misc.GetMockBlobClient(mockBlobContainer);
            var storageAccount    = Misc.GetMockStorageAccount(mockBlobClient, mockQueueClient);

            mockQueue
            .Setup(c => c.AddMessageAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

            // Act
            var largeContentWillBeCompressed = new String('z', (int)CloudQueueMessage.MaxMessageSize * 2);
            var queueManager = new QueueManager(queueName, storageAccount.Object);

            queueManager.AddMessageAsync(largeContentWillBeCompressed).Wait();

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();
        }
示例#3
0
        // This unit test verifies that a message based on a POCO class can be serialized/deserialized.
        // This was not working in v1.3.0 and was fixed in v1.4.0
        public void Add_and_get_POCO_message()
        {
            // Arrange
            var queueName         = "myqueue";
            var mockQueue         = GetMockQueue(queueName);
            var mockQueueClient   = GetMockQueueClient(mockQueue);
            var mockBlobContainer = GetMockBlobContainer();
            var mockBlobClient    = GetMockBlobClient(mockBlobContainer);
            var storageAccount    = GetMockStorageAccount(mockBlobClient, mockQueueClient);
            var queuedMessage     = (CloudQueueMessage)null;

            var sampleMessage = new SampleMessageType
            {
                DateProp   = new DateTime(2016, 10, 8, 1, 2, 3, DateTimeKind.Utc),
                GuidProp   = Guid.NewGuid(),
                IntProp    = 123,
                StringProp = "Hello World"
            };

            mockQueue
            .Setup(c => c.AddMessageAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Callback((CloudQueueMessage m, TimeSpan? ttl, TimeSpan? v, QueueRequestOptions o, OperationContext c, CancellationToken t) =>
            {
                queuedMessage = m;
            })
            .Returns(Task.FromResult(true))
            .Verifiable();

            mockQueue
            .Setup(c => c.GetMessageAsync(It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(() => { return(Task.FromResult(queuedMessage)); })
            .Verifiable();

            // Act
            var queueManager = new QueueManager(queueName, storageAccount.Object);

            queueManager.AddMessageAsync(sampleMessage).Wait();
            var result = queueManager.GetMessageAsync().Result;

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();

            result.ShouldNotBeNull();
            result.Content.GetType().ShouldBe(typeof(SampleMessageType));

            var content = (SampleMessageType)result.Content;

            content.DateProp.ShouldBe(sampleMessage.DateProp);
            content.GuidProp.ShouldBe(sampleMessage.GuidProp);
            content.IntProp.ShouldBe(sampleMessage.IntProp);
            content.StringProp.ShouldBe(sampleMessage.StringProp);
        }
示例#4
0
        public static async Task AddMessagesWithHandlerToQueue(int numberOfMessages, string queueName, CloudStorageAccount storageAccount, ILogProvider logProvider)
        {
            var queueManager = new QueueManager(queueName, storageAccount);
            await queueManager.CreateIfNotExistsAsync().ConfigureAwait(false);

            await queueManager.ClearAsync().ConfigureAwait(false);

            for (var i = 0; i < numberOfMessages; i++)
            {
                await queueManager.AddMessageAsync(new MyMessage { MessageContent = $"Hello world {i}" }).ConfigureAwait(false);
            }
        }
示例#5
0
        public void AddMessageAsync_large_message()
        {
            // Arrange
            var queueName         = "myqueue";
            var mockBlobItemUri   = new Uri(BLOB_STORAGE_URL + "test.txt");
            var mockQueue         = GetMockQueue(queueName);
            var mockQueueClient   = GetMockQueueClient(mockQueue);
            var mockBlobContainer = GetMockBlobContainer();
            var mockBlobClient    = GetMockBlobClient(mockBlobContainer);
            var storageAccount    = GetMockStorageAccount(mockBlobClient, mockQueueClient);

            var mockBlobItem = new Mock <CloudBlockBlob>(MockBehavior.Strict, mockBlobItemUri);

            mockBlobItem
            .Setup(b => b.UploadFromStreamAsync(It.IsAny <Stream>(), It.IsAny <AccessCondition>(), It.IsAny <BlobRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

            mockBlobContainer
            .Setup(c => c.GetBlockBlobReference(It.IsAny <string>()))
            .Returns(mockBlobItem.Object)
            .Verifiable();

            mockQueue
            .Setup(q => q.AddMessageAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

            // Act
            var excessivelyLargeContent = new String('z', (int)CloudQueueMessage.MaxMessageSize * 2);
            var queueManager            = new QueueManager(queueName, storageAccount.Object);

            queueManager.AddMessageAsync(excessivelyLargeContent).Wait();

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();
        }
示例#6
0
文件: Program.cs 项目: lulzzz/Picton
        private static async Task RunQueueManagerTests(CloudStorageAccount storageAccount, string queueName, CancellationToken cancellationToken)
        {
            var queueManager = new QueueManager(queueName, storageAccount);

            // Empty the queue
            await queueManager.ClearAsync(null, null, cancellationToken).ConfigureAwait(false);

            // Check that the queue is empty
            var queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 0)
            {
                throw new Exception($"We expected the queue to be empty but we found {queuedMessagesCount} messages.");
            }


            //-----------------------------------------------------------------
            // Send a simple message
            var sample = new SampleMessageType
            {
                StringProp = "abc123",
                IntProp    = 123,
                GuidProp   = Guid.NewGuid(),
                DateProp   = new DateTime(2016, 10, 6, 1, 2, 3, DateTimeKind.Utc)
            };
            await queueManager.AddMessageAsync(sample);

            // Check that there is one message in the queue
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 1)
            {
                throw new Exception($"We expected only one message in the queue but we found {queuedMessagesCount} messages.");
            }

            // Get the message
            var message1 = await queueManager.GetMessageAsync(TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false);

            if (message1.Content.GetType() != typeof(SampleMessageType))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            var receivedMessage = (SampleMessageType)message1.Content;

            if (receivedMessage.StringProp != sample.StringProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.IntProp != sample.IntProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.GuidProp != sample.GuidProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.DateProp != sample.DateProp)
            {
                throw new Exception("Did not receive the expected message");
            }

            // Delete the message from the queue
            await queueManager.DeleteMessageAsync(message1).ConfigureAwait(false);

            // Check that the queue is empty
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 0)
            {
                throw new Exception($"We expected the queue to be empty but we found {queuedMessagesCount} messages.");
            }


            //-----------------------------------------------------------------
            // Send a message that exceeds the max size allowed in Azure queues
            int characterCount = 100000;
            var largeSample    = new SampleMessageType
            {
                StringProp = new string('x', characterCount)
            };
            await queueManager.AddMessageAsync(largeSample);

            // Check that there is one message in the queue
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 1)
            {
                throw new Exception($"We expected only one message in the queue but we found {queuedMessagesCount} messages.");
            }

            // Get the message
            var message2 = await queueManager.GetMessageAsync(TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false);

            if (message2.Content.GetType() != typeof(SampleMessageType))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            var largeMessage = (SampleMessageType)message2.Content;

            if (largeMessage.StringProp.Length != characterCount)
            {
                throw new Exception("Did not receive the expected message");
            }

            // Delete the message from the queue
            await queueManager.DeleteMessageAsync(message2).ConfigureAwait(false);

            // Check that the queue is empty
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 0)
            {
                throw new Exception($"We expected the queue to be empty but we found {queuedMessagesCount} messages.");
            }


            //-----------------------------------------------------------------
            // Send a simple string
            await queueManager.AddMessageAsync("Hello World");

            // Check that there is one message in the queue
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 1)
            {
                throw new Exception($"We expected only one message in the queue but we found {queuedMessagesCount} messages.");
            }

            // Get the message
            var message3 = await queueManager.GetMessageAsync(TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false);

            if (message3.Content.GetType() != typeof(string))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            if ((string)message3.Content != "Hello World")
            {
                throw new Exception("Did not receive the expected message");
            }

            // Delete the message from the queue
            await queueManager.DeleteMessageAsync(message3).ConfigureAwait(false);

            // Check that the queue is empty
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 0)
            {
                throw new Exception($"We expected the queue to be empty but we found {queuedMessagesCount} messages.");
            }


            //-----------------------------------------------------------------
            // Send messages using the Azure CloudQueue class
            // thereby bypassing Picton's queue manager serialization
            var queue = storageAccount.CreateCloudQueueClient().GetQueueReference(queueName);

            var cloudMessage = new CloudQueueMessage("Hello World STRING");
            await queue.AddMessageAsync(cloudMessage, null, null, null, null, cancellationToken).ConfigureAwait(false);

            cloudMessage = new CloudQueueMessage(string.Empty);
            cloudMessage.SetMessageContent(Encoding.UTF8.GetBytes("Hello World BINARY"));
            await queue.AddMessageAsync(cloudMessage, null, null, null, null, cancellationToken).ConfigureAwait(false);

            cloudMessage = new CloudQueueMessage(string.Empty);
            cloudMessage.SetMessageContent(BitConverter.GetBytes(1234567890));
            await queue.AddMessageAsync(cloudMessage, null, null, null, null, cancellationToken).ConfigureAwait(false);

            // Check that there are three messages in the queue
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 3)
            {
                throw new Exception($"We expected three messages in the queue but we found {queuedMessagesCount} messages.");
            }

            // Get the messages
            var messages = (await queueManager.GetMessagesAsync(10, TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false)).ToArray();

            if (messages[0].Content.GetType() != typeof(string))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            if ((string)messages[0].Content != "Hello World STRING")
            {
                throw new Exception("Did not receive the expected message");
            }
            if (messages[1].Content.GetType() != typeof(string))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            if ((string)messages[1].Content != "Hello World BINARY")
            {
                throw new Exception("Did not receive the expected message");
            }
            if (messages[2].Content.GetType() != typeof(byte[]))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            if (BitConverter.ToInt32((byte[])messages[2].Content, 0) != 1234567890)
            {
                throw new Exception("Did not receive the expected message");
            }

            // Clear the queue
            await queueManager.ClearAsync().ConfigureAwait(false);

            // Check that the queue is empty
            queuedMessagesCount = await queueManager.GetApproximateMessageCountAsync();

            if (queuedMessagesCount != 0)
            {
                throw new Exception($"We expected the queue to be empty but we found {queuedMessagesCount} messages.");
            }
        }
示例#7
0
        private static async Task RunQueueManagerTests(IStorageAccount storageAccount, string queueName, CancellationToken cancellationToken)
        {
            var queueManager = new QueueManager(queueName, storageAccount);

            // Make sure the queue is empty
            await queueManager.ClearAsync(null, null, cancellationToken).ConfigureAwait(false);

            // Send and receive a simple message
            var sample = new SampleMessageType
            {
                StringProp = "abc123",
                IntProp    = 123,
                GuidProp   = Guid.NewGuid(),
                DateProp   = new DateTime(2016, 10, 6, 1, 2, 3, DateTimeKind.Utc)
            };
            await queueManager.AddMessageAsync(sample);

            var message1 = await queueManager.GetMessageAsync(TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false);

            if (message1.Content.GetType() != typeof(SampleMessageType))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            var receivedMessage = (SampleMessageType)message1.Content;

            if (receivedMessage.StringProp != sample.StringProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.IntProp != sample.IntProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.GuidProp != sample.GuidProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            if (receivedMessage.DateProp != sample.DateProp)
            {
                throw new Exception("Did not receive the expected message");
            }
            await queueManager.DeleteMessageAsync(message1).ConfigureAwait(false);

            // Send a message that exceeds the max size allowed in Azure queues
            int characterCount = 100000;
            var largeSample    = new SampleMessageType
            {
                StringProp = new string('x', characterCount)
            };
            await queueManager.AddMessageAsync(largeSample);

            var message2 = await queueManager.GetMessageAsync(TimeSpan.FromMinutes(5), null, null, cancellationToken).ConfigureAwait(false);

            if (message2.Content.GetType() != typeof(SampleMessageType))
            {
                throw new Exception("The type of the received message does not match the expected type");
            }
            var largeMessage = (SampleMessageType)message2.Content;

            if (largeMessage.StringProp.Length != characterCount)
            {
                throw new Exception("Did not receive the expected message");
            }
            await queueManager.DeleteMessageAsync(message2).ConfigureAwait(false);
        }
示例#8
0
        public void Add_and_get_large_message()
        {
            // Arrange
            var queueName               = "myqueue";
            var mockBlobItemUri         = new Uri(Misc.BLOB_STORAGE_URL + "test.txt");
            var mockQueue               = Misc.GetMockQueue(queueName);
            var mockQueueClient         = Misc.GetMockQueueClient(mockQueue);
            var mockBlobContainer       = Misc.GetMockBlobContainer();
            var mockBlobClient          = Misc.GetMockBlobClient(mockBlobContainer);
            var storageAccount          = Misc.GetMockStorageAccount(mockBlobClient, mockQueueClient);
            var queuedMessage           = (CloudQueueMessage)null;
            var blobItemContent         = (byte[])null;
            var excessivelyLargeContent = RandomGenerator.GenerateString((int)CloudQueueMessage.MaxMessageSize * 2);

            var mockBlobItem = new Mock <CloudBlockBlob>(MockBehavior.Strict, mockBlobItemUri);

            mockBlobItem
            .Setup(b => b.UploadFromStreamAsync(It.IsAny <Stream>(), It.IsAny <AccessCondition>(), It.IsAny <BlobRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Callback((Stream source, AccessCondition ac, BlobRequestOptions o, OperationContext c, CancellationToken t) =>
            {
                using (var ms = new MemoryStream())
                {
                    source.CopyTo(ms);
                    blobItemContent = ms.ToArray();
                }
            })
            .Returns(Task.FromResult(true))
            .Verifiable();

            mockBlobContainer
            .Setup(c => c.GetBlobReference(It.IsAny <string>()))
            .Returns(mockBlobItem.Object)
            .Verifiable();

            mockQueue
            .Setup(q => q.AddMessageAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Callback((CloudQueueMessage message, TimeSpan? ttl, TimeSpan? initialVisibilityDelay, QueueRequestOptions o, OperationContext c, CancellationToken t) =>
            {
                queuedMessage = message;
            })
            .Returns(Task.FromResult(true))
            .Verifiable();

            mockQueue
            .Setup(c => c.GetMessageAsync(It.IsAny <TimeSpan?>(), It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => queuedMessage)
            .Verifiable();

            mockBlobItem
            .Setup(b => b.DownloadToStreamAsync(It.IsAny <Stream>(), It.IsAny <AccessCondition>(), It.IsAny <BlobRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Callback((Stream target, AccessCondition ac, BlobRequestOptions o, OperationContext c, CancellationToken t) =>
            {
                var bw = new BinaryWriter(target);
                try
                {
                    bw.Write(blobItemContent);
                    bw.Flush();
                    target.Seek(0, SeekOrigin.Begin);
                }
                finally
                {
                    bw.Dispose();
                }
            })
            .Returns(Task.FromResult(true))
            .Verifiable();

            // Act
            var queueManager = new QueueManager(queueName, storageAccount.Object);

            queueManager.AddMessageAsync(excessivelyLargeContent).Wait();
            var result = queueManager.GetMessageAsync().Result;

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();
            result.Content.GetType().ShouldBe(typeof(string));
            result.Content.ShouldBe(excessivelyLargeContent);
        }