Exemplo n.º 1
0
        public void Setup()
        {
            var assemblies = new[] { typeof(BackgroundChannelsBenchmark).Assembly };

            _container.RegisterSingleton <IBus>(() => new Bus(_container.GetInstance));

            _container.RegisterInstance(_resetEvent);

            _container.Collection.Register(typeof(INotificationHandler <>), assemblies);

            _container.RegisterDecorator(typeof(INotificationHandler <>), typeof(BackgroundNotificationHandler <>));

            _container.RegisterSingleton(() => Channel.CreateUnbounded <HandleProxy>());
            _container.RegisterSingleton(() => _container.GetInstance <Channel <HandleProxy> >().Reader);
            _container.RegisterSingleton(() => _container.GetInstance <Channel <HandleProxy> >().Writer);

            _container.RegisterDecorator(typeof(INotificationHandler <>), typeof(BackgroundNotificationHandler <>));

            _bus = _container.GetInstance <IBus>();

            var channelReader = _container.GetInstance <ChannelReader <HandleProxy> >();
            var configuration = new BackgroundHandleProxyProcessorConfiguration(maxConcurrency: 1);

            _processor = new BackgroundHandleProxyProcessor(channelReader, configuration, Mock.Of <ILogger <BackgroundHandleProxyProcessor> >());

            _ = _processor.StartAsync(default);
Exemplo n.º 2
0
        public async Task StartAsync_throws_when_cancelled()
        {
            var channel = Channel.CreateUnbounded <HandleProxy>();

            var configuration = new BackgroundHandleProxyProcessorConfiguration(maxConcurrency: 1);

            var processor = new BackgroundHandleProxyProcessor(channel.Reader, configuration, logger: default);

            CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.Zero);

            Func <Task> startTask = () => processor.StartAsync(cts.Token);

            await startTask.Should().ThrowAsync <OperationCanceledException>();
        }
Exemplo n.º 3
0
        public async Task StartAsync_processes_handle_proxy_delegates(int concurrency, int delegatesNumber)
        {
            var channel = Channel.CreateUnbounded <HandleProxy>();

            var configuration = new BackgroundHandleProxyProcessorConfiguration(concurrency);

            var logger = Mock.Of <ILogger <BackgroundHandleProxyProcessor> >();

            var processor = new BackgroundHandleProxyProcessor(channel.Reader, configuration, logger);

            _ = processor.StartAsync(CancellationToken.None);

            AsyncAutoResetEvent resetEvent = new AsyncAutoResetEvent(false);

            Task WriteAsyncDelegate(int index) => channel.Writer.WriteAsync(cancellationToken =>
            {
                logger.LogInformation($"item: {index}");

                if (index == delegatesNumber - 1)
                {
                    resetEvent.Set();
                }

                return(Task.CompletedTask);
            }).AsTask();

            Task[] writeTasks = Enumerable.Range(0, delegatesNumber).Select(WriteAsyncDelegate).ToArray();

            await Task.WhenAll(writeTasks);

            await resetEvent.WaitAsync();

            Mock.Get(logger)
            .Verify(
                l => l.Log(
                    It.Is <LogLevel>(l => l == LogLevel.Information),
                    It.IsAny <EventId>(),
                    It.IsAny <It.IsAnyType>(),
                    It.Is <Exception>(e => e == null),
                    It.IsAny <Func <It.IsAnyType, Exception, string> >()),
                Times.Exactly(delegatesNumber));
        }