Пример #1
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
        }
Пример #2
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);
        }
Пример #3
0
        public async Task DequeueBehaviorTests()
        {
            await _sharedQueue.InitializeAsync(CancellationToken.None);

            var testCases = new List <TestCase>();
            // have three functions
            // A run X times  => expect X times
            TestCase a = new TestCase
            {
                CallCount     = 0,
                TotalEnqueues = 3,
                Register      = true
            };

            testCases.Add(a);
            // B run Y times  => expect Y times
            TestCase b = new TestCase
            {
                CallCount     = 0,
                TotalEnqueues = 5,
                Register      = true
            };

            testCases.Add(b);
            // C run Z times  => expect 0 times
            TestCase c = new TestCase
            {
                CallCount     = 0,
                TotalEnqueues = 7,
                Register      = false //(disabled)
            };

            testCases.Add(c);

            // start enqueuing
            await RunDummyEnqueueAsync(testCases);

            // start dequeuing
            await _sharedQueue.StartQueueAsync(CancellationToken.None);

            // wait for dequeue
            await TestHelpers.Await(() => a.CallCount >= a.TotalEnqueues && b.CallCount >= b.TotalEnqueues, 1000, 200);

            await _sharedQueue.StopQueueAsync(CancellationToken.None);

            Assert.Equal(a.TotalEnqueues, a.CallCount);
            Assert.Equal(b.TotalEnqueues, b.CallCount);
            Assert.Equal(0, c.CallCount);
        }