<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

using RestSharp.Portable.HttpClient;

using Xunit;
// ReSharper disable AccessToDisposedClosure

namespace RestSharp.Portable.Test
{
    public class GuardTests
    {
        [Fact]
        public void TestGuardLock1()
        {
            for (int i = 0; i != 10; ++i)
            {
                using (var evt = new AutoResetEvent(false))
                {
                    using (var guard = new AsyncLock())
                    {
                        var results = new ConcurrentQueue<int>();
                        var t1 = Task.Run(
                            async () =>
                            {
                                using (await guard.LockAsync(CancellationToken.None))
                                {
                                    evt.Set();
                                    evt.WaitOne();
                                    Thread.Sleep(TimeSpan.FromMilliseconds(500));
                                    results.Enqueue(1);
                                }
                            });
                        evt.WaitOne();

                        var t2 = Task.Run(
                            async () =>
                            {
                                evt.Set();
                                using (await guard.LockAsync(CancellationToken.None))
                                {
                                    results.Enqueue(2);
                                }
                            });

                        Task.WaitAll(t1, t2);
                        var resultsData = results.ToArray();
                        Assert.Equal(1, resultsData[0]);
                        Assert.Equal(2, resultsData[1]);
                    }
                }
            }
        }

        [Fact]
        public void TestGuardLock2()
        {
            for (int i = 0; i != 10; ++i)
            {
                using (var guard = new AsyncLock())
                {
                    var results = new ConcurrentQueue<int>();
                    var t2 = Task.Run(
                        async () =>
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(200));
                            using (await guard.LockAsync(CancellationToken.None))
                                results.Enqueue(2);
                        });
                    var t1 = Task.Run(
                        async () =>
                        {
                            using (await guard.LockAsync(CancellationToken.None))
                            {
                                Thread.Sleep(TimeSpan.FromMilliseconds(400));
                                results.Enqueue(1);
                            }
                        });
                    Task.WaitAll(t1, t2);
                    var resultsData = results.ToArray();
                    Assert.Equal(1, resultsData[0]);
                    Assert.Equal(2, resultsData[1]);
                }
            }
        }
    }
}
<commit_msg>Test of disposing of AsyncLock before execution of code under lock is finished<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

using RestSharp.Portable.HttpClient;

using Xunit;
// ReSharper disable AccessToDisposedClosure

namespace RestSharp.Portable.Test
{
    public class GuardTests
    {
        [Fact]
        public void TestGuardLock1()
        {
            for (int i = 0; i != 10; ++i)
            {
                using (var evt = new AutoResetEvent(false))
                {
                    using (var guard = new AsyncLock())
                    {
            var results = new ConcurrentQueue<int>();
            var t1 = Task.Run(
                async () =>
                    {
                        using (await guard.LockAsync(CancellationToken.None))
                        {
                                    evt.Set();
                                    evt.WaitOne();
                            Thread.Sleep(TimeSpan.FromMilliseconds(500));
                        results.Enqueue(1);
                                }
                    });
                        evt.WaitOne();

            var t2 = Task.Run(
                async () =>
                {
                                evt.Set();
                    using (await guard.LockAsync(CancellationToken.None))
                                {
                    results.Enqueue(2);
                                }
                });

            Task.WaitAll(t1, t2);
            var resultsData = results.ToArray();
            Assert.Equal(1, resultsData[0]);
            Assert.Equal(2, resultsData[1]);
        }
                }
            }
        }

        [Fact]
        public void TestGuardLock2()
        {
            for (int i = 0; i != 10; ++i)
            {
                using (var guard = new AsyncLock())
                {
                    var results = new ConcurrentQueue<int>();
                    var t2 = Task.Run(
                        async () =>
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(200));
                            using (await guard.LockAsync(CancellationToken.None))
                                results.Enqueue(2);
                        });
                    var t1 = Task.Run(
                        async () =>
                        {
                            using (await guard.LockAsync(CancellationToken.None))
                            {
                                Thread.Sleep(TimeSpan.FromMilliseconds(400));
                                results.Enqueue(1);
                            }
                        });
                    Task.WaitAll(t1, t2);
                    var resultsData = results.ToArray();
                    Assert.Equal(1, resultsData[0]);
                    Assert.Equal(2, resultsData[1]);
                }
            }
        }

        [Fact]
        public async void TestGuardLockEarlyDispose()
        {
            var cts = new CancellationTokenSource();
            var guard = new AsyncLock();
            var task = Task.Run(
                async () =>
                {
                    using (await guard.LockAsync(cts.Token))
                    {
                        await Task.Delay(200);
                        cts.Token.ThrowIfCancellationRequested();
                    }
                },
                cts.Token);
            await Task.Delay(100);
            cts.Cancel();
            guard.Dispose();

            await Task.Delay(200);
            Assert.Equal(TaskStatus.Canceled, task.Status);
        }
    }
}

{'fraction_non_alphanumeric': 0.08782697647230747, 'fraction_numerical': 0.012129183106824492, 'mean_word_length': 0.8096245372818615, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280748', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1511, 'n_words': 288}