示例#1
0
        public async Task StartAndStopTheEngineCorrectly()
        {
            using var cancellationSource = new CancellationSource();
            using var target             = new ScheduledProducerEngine <object>(executionStrategy.Object, cancellationSourceFactory.Object, errorHandler.Object, scheduler.Object, new ScheduledEngineOptions());

            cancellationSourceFactory.Setup(o => o.Create(It.IsAny <CancellationToken>())).Returns(cancellationSource);

            target.Initialize(context => { }, CancellationToken.None);

            await target.StartAsync();

            Assert.True(target.IsRunning);

            await target.StopAsync();

            Assert.False(target.IsRunning);
        }
示例#2
0
        public async Task RunsTheErrorHandlerWhenAnErrorHappensInStrategy()
        {
            executionStrategy.Setup(o => o.ExecuteAsync(It.IsAny <Action <IProducerConsumerContext <object> > >(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception("An exception occurred"));

            using var cancellationSource = new CancellationSource();
            using var target             = new ScheduledProducerEngine <object>(executionStrategy.Object, cancellationSourceFactory.Object, errorHandler.Object, scheduler.Object, new ScheduledEngineOptions());

            cancellationSourceFactory.Setup(o => o.Create(It.IsAny <CancellationToken>())).Returns(cancellationSource);

            target.Initialize(context => { }, CancellationToken.None);

            await target.StartAsync();

            cancellationSource.RequestCancellationAfter(TimeSpan.FromSeconds(5));

            await target.WaitForCompletionAsync();

            errorHandler.Verify(o => o.Handle(It.IsAny <Exception>(), ErrorSeverityLevel.NonFatal), Times.AtLeastOnce);
        }
示例#3
0
        public async Task RunsTheExecutionStrategy()
        {
            executionStrategy.Setup(o => o.ExecuteAsync(It.IsAny <Action <IProducerConsumerContext <object> > >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(false);

            using var cancellationSource = new CancellationSource();
            using var target             = new ScheduledProducerEngine <object>(executionStrategy.Object, cancellationSourceFactory.Object, errorHandler.Object, scheduler.Object, new ScheduledEngineOptions());

            cancellationSourceFactory.Setup(o => o.Create(It.IsAny <CancellationToken>())).Returns(cancellationSource);

            target.Initialize(context => { }, CancellationToken.None);

            await target.StartAsync();

            cancellationSource.RequestCancellationAfter(TimeSpan.FromSeconds(5));

            await target.WaitForCompletionAsync();

            executionStrategy.Verify(o => o.ExecuteAsync(It.IsAny <Action <IProducerConsumerContext <object> > >(),
                                                         It.IsAny <CancellationToken>()), Times.AtLeastOnce);
        }