Пример #1
0
        public void Can_restrain_tasks_to_wait_until_beginning_start_of_all_tasks()
        {
            // Arrange
            var starts    = new ConcurrentBag <int>();
            var processes = new ConcurrentBag <int>();
            var setter1   = Synchronizable.EventSet(obj => (int)obj == 1);
            var setter2   = Synchronizable.EventSet(obj => (int)obj == 2);


            var sync = default(ISynchronizer);

            try
            {
                // Act
                sync = setter1.And(setter2).GetSynchronizer();

                var mre1  = new ST::ManualResetEventSlim(false);
                var task1 = Task.Run(() =>
                {
                    starts.Add(1);
                    sync.Begin(1).Wait();
                    mre1.Wait(10000);
                    processes.Add(1);
                    sync.End(1).Wait();
                });

                var mre2  = new ST::ManualResetEventSlim(false);
                var task2 = Task.Run(() =>
                {
                    starts.Add(2);
                    sync.Begin(2).Wait();
                    mre2.Wait(10000);
                    processes.Add(2);
                    sync.End(2).Wait();
                });

                sync.NotifyAll(false).Wait();


                // Assert
                CollectionAssert.AreEquivalent(new[] { 1, 2 }, starts);
                CollectionAssert.IsEmpty(processes);
                mre1.Set();
                mre2.Set();
                Task.WaitAll(task1, task2);
            }
            finally
            {
                sync?.Dispose();
            }
        }
Пример #2
0
        public void Can_delay_tasks_by_the_passed_time_span()
        {
            // Arrange
            var task1BeginTime = default(DateTimeOffset);
            var task2BegunTime = default(DateTimeOffset);
            var setter1        = Synchronizable.EventSet(obj => (int)obj == 1);
            var setter2        = Synchronizable.EventSet(obj => (int)obj == 2);


            var sync = default(ISynchronizer);

            try
            {
                // Act
                sync = setter1.And(setter2.Delay(TimeSpan.FromMilliseconds(1000))).GetSynchronizer();

                var mre1  = new ST::ManualResetEventSlim(false);
                var task1 = Task.Run(() =>
                {
                    task1BeginTime = DateTimeOffset.Now;
                    sync.Begin(1).Wait();
                    mre1.Set();
                    sync.End(1).Wait();
                });

                var mre2  = new ST::ManualResetEventSlim(false);
                var task2 = Task.Run(() =>
                {
                    sync.Begin(2).Wait();
                    task2BegunTime = DateTimeOffset.Now;
                    mre2.Set();
                    sync.End(2).Wait();
                });

                sync.NotifyAll(false).Wait();


                // Assert
                mre1.Wait();
                mre2.Wait();
                Assert.GreaterOrEqual(task2BegunTime - task1BeginTime, TimeSpan.FromMilliseconds(1000));
                Task.WaitAll(task1, task2);
            }
            finally
            {
                sync?.Dispose();
            }
        }
Пример #3
0
        public void Should_ignore_empty_when_restraining_tasks()
        {
            // Arrange
            var starts    = new ConcurrentBag <int>();
            var processes = new ConcurrentBag <int>();
            var empty     = Synchronizable.Empty();
            var setter1   = Synchronizable.EventSet(obj => (int)obj == 1);
            var setter2   = Synchronizable.EventSet(obj => (int)obj == 2);
            var waiter3   = Synchronizable.EventWait(obj => (int)obj == 3);
            var waiter4   = Synchronizable.EventWait(obj => (int)obj == 4);


            var sync = default(ISynchronizer);

            try
            {
                // Act
                sync = setter1.Or(setter2).Or(empty).And(empty.Then(waiter3).Then(empty).Then(waiter4).Then(empty)).And(empty).GetSynchronizer();

                var mre1  = new ST::ManualResetEventSlim(false);
                var task1 = Task.Run(() =>
                {
                    mre1.Wait(10000);
                    starts.Add(1);
                    sync.Begin(1).Wait();
                    processes.Add(1);
                    sync.End(1).Wait();
                });

                var task2 = Task.Run(() =>
                {
                    starts.Add(2);
                    sync.Begin(2).Wait();
                    processes.Add(2);
                    sync.End(2).Wait();
                });

                var task3 = Task.Run(() =>
                {
                    starts.Add(3);
                    sync.Begin(3).Wait();
                    processes.Add(3);
                    sync.End(3).Wait();
                });

                var task4 = Task.Run(() =>
                {
                    starts.Add(4);
                    sync.Begin(4).Wait();
                    processes.Add(4);
                    sync.End(4).Wait();
                });

                sync.NotifyAll(false).Wait();


                // Assert
                CollectionAssert.DoesNotContain(starts, 1);
                CollectionAssert.AreEqual(new[] { 3, 4 }, processes.Intersect(new[] { 3, 4 }));
                mre1.Set();
                Task.WaitAll(task1, task2, task3, task4);
            }
            finally
            {
                sync?.Dispose();
            }
        }
Пример #4
0
        public void Can_restrain_tasks_to_wait_until_beginning_start_of_task1_or_task2_and_until_ending_process_of_task3_and_task4_in_order()
        {
            // Arrange
            var starts    = new ConcurrentBag <int>();
            var processes = new ConcurrentBag <int>();
            var setter1   = Synchronizable.EventSet(obj => (int)obj == 1);
            var setter2   = Synchronizable.EventSet(obj => (int)obj == 2);
            var waiter3   = Synchronizable.EventWait(obj => (int)obj == 3);
            var waiter4   = Synchronizable.EventWait(obj => (int)obj == 4);


            var sync = default(ISynchronizer);

            try
            {
                // Act
                sync = setter1.Or(setter2).And(waiter3.Then(waiter4)).GetSynchronizer();

                var mre1  = new ST::ManualResetEventSlim(false);
                var task1 = Task.Run(() =>
                {
                    mre1.Wait(10000);
                    starts.Add(1);
                    sync.Begin(1).Wait();
                    processes.Add(1);
                    sync.End(1).Wait();
                });

                var task2 = Task.Run(() =>
                {
                    starts.Add(2);
                    sync.Begin(2).Wait();
                    processes.Add(2);
                    sync.End(2).Wait();
                });

                var task3 = Task.Run(() =>
                {
                    starts.Add(3);
                    sync.Begin(3).Wait();
                    processes.Add(3);
                    sync.End(3).Wait();
                });

                var task4 = Task.Run(() =>
                {
                    starts.Add(4);
                    sync.Begin(4).Wait();
                    processes.Add(4);
                    sync.End(4).Wait();
                });

                sync.NotifyAll(false).Wait();


                // Assert
                CollectionAssert.DoesNotContain(starts, 1);
                CollectionAssert.AreEqual(new[] { 3, 4 }, processes.Intersect(new[] { 3, 4 }));
                mre1.Set();
                Task.WaitAll(task1, task2, task3, task4);
            }
            finally
            {
                sync?.Dispose();
            }
        }