示例#1
0
        /// <summary>
        /// Runs the request worker.
        /// </summary>
        /// <param name="cancellationToken">Token used to cancel the operation.</param>
        /// <returns>The resulting task.</returns>
        public async Task Run(CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            logger.LogInformation("Running REST API Queue Worker");

            var messages = await relay.Receive(cancellationToken);

            var tasks = from message in messages select Invoke(message, cancellationToken);

            await Task.WhenAll(tasks);
        }
            public async Task ShouldStartAndStopATraceWithMessageTraceHeader(
                RequestMessage message,
                [Frozen] IRequestMessageRelay relay,
                [Frozen] ITracingService tracing,
                [Target] DefaultRequestWorker worker,
                CancellationToken cancellationToken
                )
            {
                relay.Receive(Any <CancellationToken>()).Returns(new[] { message });
                await worker.Run(cancellationToken);

                Received.InOrder(() =>
                {
                    tracing.Received().StartTrace(Is(message.RequestDetails.TraceHeader));
                    tracing.Received().AddAnnotation(Is("event"), Is("rest-call"));
                    tracing.Received().EndTrace();
                });
            }
            public async Task ShouldInvokeAllReceivedMessagesThenCompleteTransactions(
                RequestMessage message1,
                RequestMessage message2,
                IScope scope1,
                IScope scope2,
                IRequestInvoker invoker1,
                IRequestInvoker invoker2,
                ITransaction transaction1,
                ITransaction transaction2,
                [Frozen] ITransactionFactory transactionFactory,
                [Frozen] IRequestMessageRelay relay,
                [Frozen] IScopeFactory scopeFactory,
                [Target] DefaultRequestWorker worker,
                CancellationToken cancellationToken
                )
            {
                transactionFactory.CreateTransaction().Returns(transaction1, transaction2);
                scope1.GetService <IRequestInvoker>().Returns(invoker1);
                scope2.GetService <IRequestInvoker>().Returns(invoker2);
                scopeFactory.CreateScope().Returns(scope1, scope2);

                relay.Receive(Any <CancellationToken>()).Returns(new[] { message1, message2 });
                await worker.Run(cancellationToken);

                Received.InOrder(async() =>
                {
                    transactionFactory.Received().CreateTransaction();
                    scopeFactory.Received().CreateScope();
                    scope1.Received().GetService <IRequestInvoker>();
                    await invoker1.Received().Invoke(Is(message1), Is(cancellationToken));
                    transaction1.Received().Complete();

                    transactionFactory.Received().CreateTransaction();
                    scopeFactory.Received().CreateScope();
                    scope2.Received().GetService <IRequestInvoker>();
                    await invoker2.Received().Invoke(Is(message2), Is(cancellationToken));
                    transaction2.Received().Complete();
                });
            }