Ejemplo n.º 1
0
        public async Task InvokeMultipleWork_RunSynchronously()
        {
            // Arrange

            var trap   = new ConcurrentTrap();
            var log    = new ConcurrentQueue <int>();
            var worker = new Worker();

            // Act

            for (var i = 1; i <= 10; i++)
            {
                var local_i = i;
                worker.Invoke(() =>
                {
                    using (new ConcurrentTrapBlock(trap))
                        log.Enqueue(local_i);
                });
            }

            await worker.SetBarrierAsync();

            // Assert

            Assert.Equal(false, trap.Trapped);
            Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, log);
        }
Ejemplo n.º 2
0
        public async Task InvokeMultipleAsyncWork_RunSynchronously()
        {
            // Arrange

            var trap   = new ConcurrentTrap();
            var log    = new ConcurrentQueue <int>();
            var worker = new Worker();

            // Act

            for (var i = 1; i <= 10; i++)
            {
                var local_i = i;
                worker.Invoke(async() =>
                {
                    using (new ConcurrentTrapBlock(trap))
                        log.Enqueue(local_i);

                    await Task.Yield();

                    using (new ConcurrentTrapBlock(trap))
                        log.Enqueue(-local_i);
                });
            }

            await worker.SetBarrierAsync();

            // Assert

            Assert.Equal(false, trap.Trapped);
            Assert.Equal(20, log.Count);
            Assert.Equal(20, log.Distinct().ToList().Count);
        }
Ejemplo n.º 3
0
        public async Task InvokeAtomicWork_SyncWithOtherWorker()
        {
            // Arrange

            var log     = new ConcurrentQueue <int>();
            var trap1   = new ConcurrentTrap();
            var trap2   = new ConcurrentTrap();
            var worker1 = new Worker("Worker1");
            var worker2 = new Worker("Worker2");

            // Act

            worker1.Invoke(async() =>
            {
                using (new ConcurrentTrapBlock(trap1))
                    log.Enqueue(1);

                await Task.Yield();

                using (new ConcurrentTrapBlock(trap1))
                    log.Enqueue(-1);
            });

            worker1.Invoke(async() =>
            {
                using (new ConcurrentTrapBlock(trap2))
                    log.Enqueue(100);

                await Task.Yield();

                using (new ConcurrentTrapBlock(trap2))
                    log.Enqueue(101);
            }, InvokeOptions.Atomic, new SyncOptions(worker2));

            worker2.Invoke(async() =>
            {
                using (new ConcurrentTrapBlock(trap1))
                    using (new ConcurrentTrapBlock(trap2))
                        log.Enqueue(2);

                await Task.Yield();

                using (new ConcurrentTrapBlock(trap1))
                    using (new ConcurrentTrapBlock(trap2))
                        log.Enqueue(-2);
            });

            await Task.WhenAll(worker1.SetBarrierAsync(),
                               worker2.SetBarrierAsync());

            // Assert

            Assert.Equal(false, trap1.Trapped);
            Assert.Equal(false, trap2.Trapped);
            Assert.Equal(6, log.Count);
            var idx = log.ToList().IndexOf(100);

            Assert.Equal(101, log.ToList()[idx + 1]);
        }
Ejemplo n.º 4
0
 public ConcurrentTrapBlock(ConcurrentTrap trap)
 {
     _trap = trap;
     _trap.Acquire();
 }