Exemplo n.º 1
0
        public async Task PropertiesCanBeSetConcurrentlyWithObjectCreation()
        {
            const int personsPerThread = 50;
            const int threadAmount     = 10;
            var       threads          = new Thread[threadAmount];

            var allViewModels = new TestViewModel[threadAmount][];

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i] = new Thread((index) =>
                {
                    var localViewModels = new TestViewModel[personsPerThread];

                    for (int j = 0; j < personsPerThread; j++)
                    {
                        var viewModel = new TestViewModel
                        {
                            Age = 18
                        };

                        viewModel.Age = 19;

                        localViewModels[j] = viewModel;
                    }

                    allViewModels[(int)index] = localViewModels;
                });
            }

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i].Start(i);
            }

            await Task.Delay(1000);

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i].Join();
            }

            var flatenSortedIdentifiers =
                allViewModels
                .SelectMany(o => o)
                .Select(o => o.UniqueIdentifier)
                .OrderBy(o => o)
                .ToArray();

            var firstId = flatenSortedIdentifiers[0];

            Assert.That(flatenSortedIdentifiers, Is.EquivalentTo(Enumerable.Range(firstId, personsPerThread * threadAmount)));
        }
Exemplo n.º 2
0
        public async Task MultipleViewModelsCanBeCreatedConcurrently()
        {
            const int personsPerThread = 50;
            const int threadAmount     = 10;
            var       threads          = new Thread[threadAmount];

            var allViewModels = new TestViewModel[threadAmount][];

            for (var i = 0; i < threadAmount; i++)
            {
                threads[i] = new Thread(index =>
                {
                    var localViewModels = new TestViewModel[personsPerThread];
                    for (var j = 0; j < personsPerThread; j++)
                    {
                        localViewModels[j] = new TestViewModel();
                    }

                    lock (allViewModels)
                    {
                        allViewModels[(int)index] = localViewModels;
                    }
                });
            }

            for (var i = 0; i < threadAmount; i++)
            {
                threads[i].Start(i);
            }

            await Task.Delay(1000);

            for (var i = 0; i < threadAmount; i++)
            {
                threads[i].Join();
            }

            var flatenSortedIdentifiers =
                allViewModels
                .SelectMany(o => o)
                .Select(o => o.UniqueIdentifier)
                .OrderBy(o => o)
                .ToArray();

            var firstId = flatenSortedIdentifiers[0];

            Assert.That(flatenSortedIdentifiers, Is.EquivalentTo(Enumerable.Range(firstId, personsPerThread * threadAmount)));
        }
Exemplo n.º 3
0
        public void CommandsCanBeCalledConcurrentlyWithObjectCreation()
        {
            const int personsPerThread = 50;
            const int threadAmount     = 10;
            var       threads          = new Thread[threadAmount];

            var allViewModels = new TestViewModel[threadAmount][];

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i] = new Thread((index) =>
                {
                    var localViewModels = new TestViewModel[personsPerThread];
                    for (int j = 0; j < personsPerThread; j++)
                    {
                        var viewModel = new TestViewModel();
                        viewModel.GenerateData.Execute();
                        localViewModels[j] = viewModel;
                    }
                    allViewModels[(int)index] = localViewModels;
                });
            }

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i].Start(i);
            }

            for (int i = 0; i < threadAmount; i++)
            {
                threads[i].Join();
            }

            var flatenSortedIdentifiers =
                allViewModels
                .SelectMany(o => o)
                .Select(o => o.UniqueIdentifier)
                .OrderBy(o => o)
                .ToArray();

            var firstId = flatenSortedIdentifiers[0];

            Assert.That(flatenSortedIdentifiers, Is.EquivalentTo(Enumerable.Range(firstId, personsPerThread * threadAmount)));
        }