public void Pulse_ReleasesOneWaiter()
        {
            Test.Async(async() =>
            {
                var monitor    = new AsyncMonitor();
                int completed  = 0;
                var task1Ready = new TaskCompletionSource();
                var task2Ready = new TaskCompletionSource();
                var task1      = TaskShim.Run(async() =>
                {
                    using (await monitor.EnterAsync())
                    {
                        var waitTask1 = monitor.WaitAsync();
                        task1Ready.SetResult();
                        await waitTask1;
                        Interlocked.Increment(ref completed);
                    }
                });
                await task1Ready.Task;
                var task2 = TaskShim.Run(async() =>
                {
                    using (await monitor.EnterAsync())
                    {
                        var waitTask2 = monitor.WaitAsync();
                        task2Ready.SetResult();
                        await waitTask2;
                        Interlocked.Increment(ref completed);
                    }
                });
                await task2Ready.Task;

                using (await monitor.EnterAsync())
                {
                    monitor.Pulse();
                }
                await TaskShim.WhenAny(task1, task2);
                var result = Interlocked.CompareExchange(ref completed, 0, 0);

                Assert.AreEqual(1, result);
            });
        }
Esempio n. 2
0
        public async Task Pulse_ReleasesOneWaiter()
        {
            AsyncMonitor monitor = new AsyncMonitor();

            int[] completed = { 0 };
            TaskCompletionSource <object> task1Ready = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();
            TaskCompletionSource <object> task2Ready = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();
            Task task1 = Task.Run(async() =>
            {
                using (await monitor.EnterAsync())
                {
                    Task waitTask1 = monitor.WaitAsync();
                    task1Ready.SetResult(null);
                    await waitTask1;
                    Interlocked.Increment(ref completed[0]);
                }
            });
            await task1Ready.Task;
            Task task2 = Task.Run(async() =>
            {
                using (await monitor.EnterAsync())
                {
                    Task waitTask2 = monitor.WaitAsync();
                    task2Ready.SetResult(null);
                    await waitTask2;
                    Interlocked.Increment(ref completed[0]);
                }
            });
            await task2Ready.Task;

            using (await monitor.EnterAsync())
            {
                monitor.Pulse();
            }
            await Task.WhenAny(task1, task2).ConfigureAwait(false);

            int result = Interlocked.CompareExchange(ref completed[0], 0, 0);

            Assert.Equal(1, result);
        }
        public async Task Pulse_ReleasesOneWaiter()
        {
            var monitor    = new AsyncMonitor();
            int completed  = 0;
            var task1Ready = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();
            var task2Ready = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();
            var task1      = Task.Run(async() =>
            {
                using (await monitor.EnterAsync())
                {
                    var waitTask1 = monitor.WaitAsync();
                    task1Ready.SetResult(null);
                    await waitTask1;
                    Interlocked.Increment(ref completed);
                }
            });
            await task1Ready.Task;
            var task2 = Task.Run(async() =>
            {
                using (await monitor.EnterAsync())
                {
                    var waitTask2 = monitor.WaitAsync();
                    task2Ready.SetResult(null);
                    await waitTask2;
                    Interlocked.Increment(ref completed);
                }
            });
            await task2Ready.Task;

            using (await monitor.EnterAsync())
            {
                monitor.Pulse();
            }
            await Task.WhenAny(task1, task2);

            var result = Interlocked.CompareExchange(ref completed, 0, 0);

            Assert.Equal(1, result);
        }
        public async Task Locked_PreventsLockUntilUnlocked()
        {
            var monitor       = new AsyncMonitor();
            var task1HasLock  = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();
            var task1Continue = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();

            var task1 = Task.Run(async() =>
            {
                using (await monitor.EnterAsync())
                {
                    task1HasLock.SetResult(null);
                    await task1Continue.Task;
                }
            });
            await task1HasLock.Task;

            var lockTask = monitor.EnterAsync().AsTask();

            Assert.False(lockTask.IsCompleted);
            task1Continue.SetResult(null);
            await lockTask;
        }
        public async Task <byte[]> ReadAsync(string fileName)
        {
            if (_cache.ContainsKey(fileName))
            {
                return(_cache[fileName]);
            }

            using (await AsyncMonitor.EnterAsync(fileName))
            {
                // double check cache since it can be populated in another thread
                if (_cache.ContainsKey(fileName))
                {
                    return(_cache[fileName]);
                }

                var fileData = await _fileReader.ReadAsync(fileName);

                RealReadCallCount++;
                _cache[fileName] = fileData;
                return(fileData);
            }
        }
Esempio n. 6
0
        public async Task WillPulseMonitor()
        {
            var monitor = new AsyncMonitor();
            var sw      = Stopwatch.StartNew();

            // Monitor will not be pulsed and should be cancelled after 100ms.
            using (await monitor.EnterAsync())
                await Assert.ThrowsAsync <OperationCanceledException>(() => monitor.WaitAsync(TimeSpan.FromMilliseconds(100).ToCancellationToken()));
            sw.Stop();
            Assert.InRange(sw.ElapsedMilliseconds, 75, 125);

            var t = Task.Run(async() => {
                await SystemClock.SleepAsync(25);
                using (await monitor.EnterAsync())
                    monitor.Pulse();
            });

            sw = Stopwatch.StartNew();
            using (await monitor.EnterAsync())
                await monitor.WaitAsync(TimeSpan.FromSeconds(1).ToCancellationToken());
            sw.Stop();
            Assert.InRange(sw.ElapsedMilliseconds, 25, 100);
        }
        public void Locked_PreventsLockUntilUnlocked()
        {
            Test.Async(async() =>
            {
                var monitor       = new AsyncMonitor();
                var task1HasLock  = new TaskCompletionSource();
                var task1Continue = new TaskCompletionSource();

                var task1 = TaskShim.Run(async() =>
                {
                    using (await monitor.EnterAsync())
                    {
                        task1HasLock.SetResult();
                        await task1Continue.Task;
                    }
                });
                await task1HasLock.Task;

                var lockTask = monitor.EnterAsync().AsTask();
                Assert.IsFalse(lockTask.IsCompleted);
                task1Continue.SetResult();
                await lockTask;
            });
        }
        public void Id_IsNotZero()
        {
            var monitor = new AsyncMonitor();

            Assert.NotEqual(0, monitor.Id);
        }