예제 #1
0
        public async Task Check_that_next_instance_start_after_than_previouse_finished_async()
        {
            const int maxThreads = 2;
            var       timeLog    = new ConcurrentBag <TimeLog>();
            var       timeToWait = new TimeSpan(0, 0, 0, 0, 100);

            Func <Task> scenario = (async() =>
            {
                var synchronizationService = _container.Resolve <SynchronizationService>();

                await synchronizationService.ExecuteAsync(ServiceToSynchronise, async() =>
                {
                    var time = new TimeLog {
                        Start = DateTime.Now
                    };
                    Trace.WriteLine(string.Format("Service To Synchronise: {0} in thread #{1}", ServiceToSynchronise, Thread.CurrentThread.ManagedThreadId));
                    await Task.Delay(timeToWait);
                    time.Stop = DateTime.Now;
                    timeLog.Add(time);
                });
            });

            var tasks = Enumerable.Range(1, maxThreads).Select(i => scenario());
            await Task.WhenAll(tasks);

            var times = timeLog.OrderBy(x => x.Start).ToArray();

            //Check that time of start next instance greater than stop of previouse instance
            //and they not overlaped in the time
            for (int i = 0; i < maxThreads - 1; i++)
            {
                Assert.That(times[i].Stop <= times[i + 1].Start);
            }
        }
예제 #2
0
        public void Check_that_next_instance_start_after_than_previouse_finished()
        {
            const int maxThreads = 2;
            var       timeLog    = new ConcurrentBag <TimeLog>();
            var       timeToWait = new TimeSpan(0, 0, 0, 0, 100);

            var scenario = new Action(() =>
            {
                var synchronizationService = _container.Resolve <SynchronizationService>();

                synchronizationService.Execute(ServiceToSynchronise, new Action(() =>
                {
                    var time = new TimeLog {
                        Start = DateTime.Now
                    };
                    Trace.WriteLine(string.Format("Service To Synchronise: {0} in thread #{1}", ServiceToSynchronise, Thread.CurrentThread.ManagedThreadId));
                    Thread.Sleep(timeToWait);
                    time.Stop = DateTime.Now;
                    timeLog.Add(time);
                }));
            });

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = maxThreads
            };

            Parallel.Invoke(options, Enumerable.Repeat(scenario, maxThreads).ToArray());

            var times = timeLog.OrderBy(x => x.Start).ToArray();

            //Check that time of start next instance greater than stop of previouse instance
            //and they not overlaped in the time
            for (int i = 0; i < maxThreads - 1; i++)
            {
                Assert.That(times[i].Stop <= times[i + 1].Start);
            }
        }