コード例 #1
0
ファイル: QueueManagerTests.cs プロジェクト: marcblum/Picton
        public void ClearAsync()
        {
            // 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.ClearAsync(It.IsAny <QueueRequestOptions>(), It.IsAny <OperationContext>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

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

            queueManager.ClearAsync().Wait();

            // Assert
            mockQueue.Verify();
            mockQueueClient.Verify();
            mockBlobContainer.Verify();
            mockBlobClient.Verify();
        }
コード例 #2
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);
            }
        }
コード例 #3
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.");
            }
        }
コード例 #4
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);
        }
コード例 #5
0
        public static async Task Main(string [] args)
        {
            Console.WriteLine($"DotNet Core Console - Containerized - Update Azure Storeage - v{RuntimeHelper.GetAppVersion()} - IsRunningContainerMode:{RuntimeHelper.IsRunningContainerMode()}");
            Console.WriteLine($"Env Fred={Environment.GetEnvironmentVariable("Fred")}");
            Console.WriteLine(RuntimeHelper.GetContextInformation());

            var aListOfStrings = new List <string>()
            {
                "a", "b"
            };

            Console.WriteLine($"DynamicSugarCore: List<string>.Format {aListOfStrings.Format()}");

            var alphabetDic = DS.Resources.GetTextResource(new Regex("embed.text.txt", RegexOptions.IgnoreCase), Assembly.GetExecutingAssembly());

            foreach (var e in alphabetDic)
            {
                Console.WriteLine($"DynamicSugarCore Embed Resource: key:{e.Key} value:{e.Value} ");
            }

            var dataTxtFile = RuntimeHelper.GetAppFilePath("data.txt");

            Console.WriteLine($"dataTxtFile:${dataTxtFile}, exists:{File.Exists(dataTxtFile)}");

            var tutuFolder = RuntimeHelper.GetAppFolderPath("tutu");

            Console.WriteLine($"tutuFolder:${tutuFolder}, exists:{Directory.Exists(tutuFolder)}");

            // Initialize Azure storage and queue
            var          storageAccount = RuntimeHelper.GetAppSettings("storage:accountName");
            var          storageKey     = RuntimeHelper.GetAppSettings("storage:key");
            const string containerName  = "public";
            const string queueName      = "myQueue2";

            var qm = new QueueManager(storageAccount, storageKey, queueName);
            var bm = new BlobManager(storageAccount, storageKey, containerName);
            var tm = new TableManager(storageAccount, storageKey, "FileHistory");

            //var fileLogHistoryAzureTableRecord = new FileLogHistoryAzureTableRecord {
            //    FileName = "zizi.txt", ComputerOrigin = Environment.MachineName, CreationTime = DateTime.UtcNow
            //};
            //fileLogHistoryAzureTableRecord.SetIdentification();
            //await tm.Insert(fileLogHistoryAzureTableRecord);
            var allRecords = await tm.GetRecords <FileLogHistoryAzureTableRecord>(Environment.MachineName);

            var ziziRecords = await tm.GetRecords <FileLogHistoryAzureTableRecord>(Environment.MachineName, "zizi.txt",
                                                                                   new TableManager.WhereClauseExpression {
                Name = "ComputerOrigin", Value = Environment.MachineName
            });

            if (args.Length > 0)
            {
                switch (args[0].ToLowerInvariant())
                {
                case "help":
                    Console.WriteLine(@"fCoreConsoleAzureStorage
clearQueue | clearStorage | dirStorage | dirQueue | getQueue | sendMessage ""text""
");
                    break;

                case "clearqueue":
                    var deleteMessages = await qm.ClearAsync();

                    Console.WriteLine($"{deleteMessages.Count} deleted message");
                    break;

                case "dirqueue":
                {
                    var messageCount = await qm.ApproximateMessageCountAsync();

                    Console.WriteLine($"{messageCount} messages");
                }
                break;

                case "getqueue":
                {
                    while (true)
                    {
                        var m = await qm.DequeueAsync();

                        if (m == null)
                        {
                            break;
                        }
                        await qm.DeleteAsync(m.Id);

                        Console.WriteLine($"Message id:{m.Id}, body:{m.AsString}");
                    }
                }
                break;

                case "clearstorage":
                {
                    var blobs = await bm.DirAsync();

                    Console.WriteLine($"About to delete {blobs.Count} cloud file from storage container:{bm.ContainerName}");
                    await bm.DeleteFileAsync(blobs);
                }
                break;

                case "dirstorage":
                {
                    var blobs = await bm.DirAsync();

                    Console.WriteLine($"{blobs.Count} file(s) found in container:{bm.ContainerName}");
                    foreach (var b in blobs)
                    {
                        Console.WriteLine($"  {b}");
                    }
                }
                break;

                case "sendmessage":
                    Console.WriteLine($"Sending Message:{args[1]}");
                    var messageId = await qm.EnqueueAsync(args[1]);

                    Console.WriteLine($"MessageId:${messageId}");
                    break;

                default:
                    Console.WriteLine($"Command not supported:{args[0]}");
                    break;
                }
                Environment.Exit(0);
            }

            Console.WriteLine($"Storage:{storageAccount}, container:{containerName}");

            for (var i = 0; i < 100; i++)
            {
                Console.WriteLine($"");
                Console.WriteLine($"{i} execution(s).");
                CreateTextFileInStorage(bm, qm).GetAwaiter().GetResult();
                Console.WriteLine($"Waiting {WaitTime} seconds");
                System.Threading.Tasks.Task.Delay(1000 * WaitTime).Wait();
            }
            Console.WriteLine("Done");
        }