Пример #1
0
        public async Task QueueInitializationTest()
        {
            // first initialization should be fine
            await _sharedQueue.InitializeAsync(CancellationToken.None);

            // should not initialize twice
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(
                () => _sharedQueue.InitializeAsync(CancellationToken.None));

            Assert.Equal($"Expected state to be \"Created\" but actualy state is \"Initialized\", this is probably because methods are not called in correct order",
                         exception.Message);
        }
Пример #2
0
        public async Task InMemoryDispatchQueueHandlerTest()
        {
            string error = "no storage account found";

            _accountProviderMock.Setup(m => m.TryGetAccountAsync(ConnectionStringNames.Storage, It.IsAny <CancellationToken>())).
            ThrowsAsync(new Exception(error));

            await _sharedQueue.InitializeAsync(CancellationToken.None);

            Assert.Empty(_loggerProvider.GetAllLogMessages());

            // listenercontext should return inMemoryDispatchQueueHandler when there's no storage account
            var descriptorMock             = new Mock <FunctionDescriptor>();
            var triggerExecutorMock        = new Mock <ITriggeredFunctionExecutor>();
            ListenerFactoryContext context = new ListenerFactoryContext(
                descriptorMock.Object,
                triggerExecutorMock.Object,
                _sharedQueue,
                CancellationToken.None);

            var messageHandlerMock = new Mock <IMessageHandler>();
            var condition          = new AutoResetEvent(false);

            messageHandlerMock.Setup(m => m.TryExecuteAsync(It.IsAny <JObject>(), It.IsAny <CancellationToken>()))
            .Returns(async() =>
            {
                await Task.Yield();
                condition.Set();      // will run asynchronously
                return(new FunctionResult(true));
            });

            var dispatchQueue = context.GetDispatchQueue(messageHandlerMock.Object);

            // make sure initialization error is traced
            Assert.Equal(error, _loggerProvider.GetAllLogMessages().Single().Exception.Message);
            Assert.Equal(SharedQueueHandler.InitErrorMessage, _loggerProvider.GetAllLogMessages().Single().FormattedMessage);
            Assert.IsType <InMemoryDispatchQueueHandler>(dispatchQueue);

            await dispatchQueue.EnqueueAsync(JObject.Parse("{}"), CancellationToken.None);

            // without storage account, it is still possible to perform local enqueue, dequeue
            Assert.True(condition.WaitOne(200));

            // following two should be NOOP, inner queueListener was never created
            await _sharedQueue.StartQueueAsync(CancellationToken.None);

            await _sharedQueue.StopQueueAsync(CancellationToken.None);

            // no NullPointerException
        }
Пример #3
0
        private async Task StartAsyncCore(CancellationToken cancellationToken)
        {
            // create sharedQueue so that once the listener started, they can enqueue
            await _sharedQueue.InitializeAsync(cancellationToken);

            _listener = await _factory.CreateAsync(cancellationToken);

            _cancellationRegistration = _cancellationSource.Token.Register(_listener.Cancel);
            await _listener.StartAsync(cancellationToken); // composite listener, startAsync in parallel

            // start sharedQueue after other listeners
            await _sharedQueue.StartQueueAsync(cancellationToken);
        }