Esempio n. 1
0
        public async Task ReceiveAsync_EmptyQueue_()
        {
            // Arrange
            var collection             = new ConcurrentQueue <ICommand>();
            InMemoryCommandQueue queue = new InMemoryCommandQueue(collection);

            collection.Enqueue(new CommandToQueue());
            CancellationTokenSource source            = new CancellationTokenSource();
            CancellationToken       cancellationToken = source.Token;

            source.CancelAfter(200);
            ICommand command = null;

            // Act
            try
            {
                command = await queue.ReceiveAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
            }

            // Assert
            Assert.Null(command);
        }
Esempio n. 2
0
        public async Task RunAsync_RequestsAreConsumed()
        {
            // Arrange
            const int CommandCount = 100;
            var       collection   = new ConcurrentQueue <ICommand>();

            for (int i = 0; i < CommandCount; i++)
            {
                collection.Enqueue(new CommandToQueue(i));
            }

            InMemoryCommandQueue queue = new InMemoryCommandQueue(collection);

            Mock <IMessageProcessor> processor    = new Mock <IMessageProcessor>(MockBehavior.Strict);
            CancellationTokenSource  cancellation = new CancellationTokenSource();

            processor
            .Setup(p => p.ProcessAsync(It.IsAny <CommandToQueue>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(HandlerResponse.Empty));

            CommandRunner broker = new CommandRunner(processor.Object, queue, 8);

            // Act
            cancellation.CancelAfter(1000);
            await broker.StartAsync(cancellation.Token);

            // Assert
            processor.Verify(p => p.ProcessAsync(It.IsAny <CommandToQueue>(), It.IsAny <CancellationToken>()), Times.Exactly(CommandCount));
        }
Esempio n. 3
0
        public async Task SendAsync_RequestInQueue()
        {
            // Arrange
            var collection             = new ConcurrentQueue <ICommand>();
            InMemoryCommandQueue queue = new InMemoryCommandQueue(collection);
            var command = new CommandToQueue();

            // Act
            await queue.SendAsync(command, default(CancellationToken));

            // Assert
            Assert.Equal(1, collection.Count);
        }
Esempio n. 4
0
        public static void EnableInMemoryMessageQueuing(this ProcessorConfiguration configuration, int degreeOfParallelism)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            InMemoryCommandQueue inMemoryQueue = null;

            try
            {
                inMemoryQueue = new InMemoryCommandQueue();
                configuration.EnableMessageQueuing(degreeOfParallelism, inMemoryQueue, inMemoryQueue);
                inMemoryQueue = null;
            }
            finally
            {
                if (inMemoryQueue != null)
                {
                    inMemoryQueue.Dispose();
                }
            }
        }