Ejemplo n.º 1
0
        public void TestThreadUnsafe()
        {
            SyncChecker syncChecker = new SyncChecker();

            Assert.ThrowsAsync <AssertionException>(async() =>
            {
                Task[] tasks = Enumerable.Range(0, 2).Select(x => Task.Run(async() =>
                {
                    syncChecker.Enter();
                    await Task.Delay(TimeSpan.FromSeconds(0.5));
                    syncChecker.Leave();
                })).ToArray();

                await Task.WhenAll(tasks);
            });
        }
Ejemplo n.º 2
0
        public async Task TestThreadSafe()
        {
            SyncChecker syncChecker = new SyncChecker();

            using (AsyncLocker locker = new AsyncLocker())
            {
                Task[] tasks = Enumerable.Range(0, 2).Select(x => Task.Run(async() =>
                {
                    using (locker.Lock())
                    {
                        syncChecker.Enter();
                        await Task.Delay(TimeSpan.FromSeconds(1));
                        syncChecker.Leave();
                    }
                })).ToArray();

                await Task.WhenAll(tasks);
            }
        }