<commit_before>﻿using System;
using System.Threading;
using ReactiveDomain.Messaging.Bus;
using ReactiveDomain.Messaging.Testing;
using Xunit;

namespace ReactiveDomain.Messaging.Tests
{
    public class when_sending_concurrent_commands : CommandQueueSpecification
    {
        public when_sending_concurrent_commands() : base(3)
        {

        }
        private long _cmd1Count = 0;
        long _cmd2Count = 0;
        long _cmd3Count = 0;

        protected override void Given()
        {
             
             
            Bus.Subscribe(new AdHocCommandHandler<TestCommands.TestCommand>(
                                    cmd => { Interlocked.Increment(ref _cmd1Count); return true; }));
            Bus.Subscribe(new AdHocCommandHandler<TestCommands.TestCommand2>(
                                    cmd => { Interlocked.Increment(ref _cmd2Count); return true; }));
            Bus.Subscribe(new AdHocCommandHandler<TestCommands.TestCommand3>(
                                    cmd => { Interlocked.Increment(ref _cmd3Count); return true; }));

            Assert.True(_cmd1Count == 0, $"gotSuccess should be 0, found { _cmd1Count}");

        }

        protected override void When()
        {
            for (int i = 0; i < 50; i++)
            {
                Queue.Handle(new TestCommands.TestCommand(Guid.NewGuid(), null));
                Queue.Handle(new TestCommands.TestCommand2(Guid.NewGuid(), null));
                Queue.Handle(new TestCommands.TestCommand3(Guid.NewGuid(), null));
            }
        }
        [Fact]
        public void concurrent_commands_should_pass()
        {
            Assert.IsOrBecomesTrue(() => _cmd3Count == 50, 50 * 55 + 200, $"gotTimeout should be 50, found { _cmd3Count}");
            Assert.IsOrBecomesTrue(() => _cmd1Count == 50, 2000, $"gotSuccess should be 50, found { _cmd1Count}");
            Assert.IsOrBecomesTrue(() => _cmd2Count == 50, 2000, $"gotFail should be 50, found { _cmd2Count}");
         }
    }
}<commit_msg>simplified concurrent commands send test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ReactiveDomain.Messaging.Bus;
using ReactiveDomain.Messaging.Testing;
using Xunit;

namespace ReactiveDomain.Messaging.Tests
{
	// ReSharper disable once InconsistentNaming
	public class when_sending_concurrent_commands :
		IHandleCommand<TestCommands.TestCommand>,
		IHandleCommand<TestCommands.TestCommand2>,
		IHandleCommand<TestCommands.TestCommand3>

	{
		private readonly TimeSpan _2Sec = TimeSpan.FromSeconds(2);
		private readonly CommandBus _bus;
		private long _cmd1Count;
		private long _cmd2Count;
		private long _cmd3Count;
		private readonly List<Command> _commands;
		private int _count;
		public when_sending_concurrent_commands()
		{
			_bus = new CommandBus("Test", false, _2Sec, _2Sec);
			_bus.Subscribe<TestCommands.TestCommand>(this);
			_bus.Subscribe<TestCommands.TestCommand2>(this);
			_bus.Subscribe<TestCommands.TestCommand3>(this);
			_commands = new List<Command>();
			_count = 5;
			for (int i = 0; i < _count; i++) {
				_commands.Add(new TestCommands.TestCommand(Guid.NewGuid(), null));
				_commands.Add(new TestCommands.TestCommand2(Guid.NewGuid(), null));
				_commands.Add(new TestCommands.TestCommand3(Guid.NewGuid(), null));
			}
		}

		[Fact]
		public void concurrent_commands_should_pass() {

			Parallel.ForEach(_commands, cmd => _bus.Fire(cmd));

			Assert.True(_cmd1Count == _count, $"Should be {_count}, found { _cmd1Count}");
			Assert.True(_cmd2Count == _count, $"Should be {_count}, found { _cmd2Count}");
			Assert.True(_cmd3Count == _count, $"Should be {_count}, found { _cmd3Count}");
			}

		public CommandResponse Handle(TestCommands.TestCommand command)
		{
			Interlocked.Increment(ref _cmd1Count);
			return command.Succeed();
		}

		public CommandResponse Handle(TestCommands.TestCommand2 command)
		{
			Interlocked.Increment(ref _cmd2Count);
			return command.Succeed();
		}

		public CommandResponse Handle(TestCommands.TestCommand3 command)
		{
			Interlocked.Increment(ref _cmd3Count);
			return command.Succeed();
		}
	}
}
{'fraction_non_alphanumeric': 0.10883698531182277, 'fraction_numerical': 0.01926318324103058, 'mean_word_length': 3.2172588832487308, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741674', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1230, 'n_words': 257}