Exemple #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);
Exemple #2
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));
        }
Exemple #3
0
        public void Setup()
        {
            var assemblies = new[] { typeof(BackgroundQueueBenchmark).Assembly };

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

            _container.RegisterInstance(_resetEvent);

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

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

            _bus = _container.GetInstance <IBus>();

            var queue = _container.GetInstance <IBackgroundHandleProxyQueue>();

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

            _ = _processor.StartAsync(default);
Exemple #4
0
        public async Task StartAsync_processes_handle_proxy_delegates(int delegatesNumber)
        {
            var queue     = new BackgroundHandleProxyQueue();
            var logger    = Mock.Of <ILogger <BackgroundHandleProxyProcessor> >();
            var processor = new BackgroundHandleProxyProcessor(queue, logger);

            _ = processor.StartAsync(CancellationToken.None);

            AsyncAutoResetEvent resetEvent = new AsyncAutoResetEvent(false);

            Task EnqueueAsyncDelegate(int index) => queue.EnqueueAsync(cancellationToken =>
            {
                logger.LogInformation($"item: {index}");

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

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

            Task[] enqueueTasks = Enumerable.Range(0, delegatesNumber).Select(EnqueueAsyncDelegate).ToArray();

            await Task.WhenAll(enqueueTasks);

            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));
        }