コード例 #1
0
    private static async Task IndependentValuesBetweenContextsHelper <T>()
        where T : class, new()
    {
        var asyncLocal = new Microsoft.VisualStudio.Threading.AsyncLocal <T>();
        var player1    = new AsyncAutoResetEvent();
        var player2    = new AsyncAutoResetEvent();
        await Task.WhenAll(
            Task.Run(async delegate
        {
            Assert.Null(asyncLocal.Value);
            var value        = new T();
            asyncLocal.Value = value;
            Assert.Same(value, asyncLocal.Value);
            player1.Set();
            await player2.WaitAsync();
            Assert.Same(value, asyncLocal.Value);
        }),
            Task.Run(async delegate
        {
            await player1.WaitAsync();
            Assert.Null(asyncLocal.Value);
            var value        = new T();
            asyncLocal.Value = value;
            Assert.Same(value, asyncLocal.Value);
            asyncLocal.Value = null;
            player2.Set();
        }));

        Assert.Null(asyncLocal.Value);
    }
コード例 #2
0
    public void ValuePersistsAcrossExecutionContextChanges()
    {
        var jtLocal = new Microsoft.VisualStudio.Threading.AsyncLocal <object>();

        jtLocal.Value = 1;
        Func <Task> asyncMethod = async delegate
        {
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            Assert.Equal(1, jtLocal.Value);
            jtLocal.Value = 3;
            Assert.Equal(3, jtLocal.Value);
            await TaskScheduler.Default;
            Assert.Equal(3, jtLocal.Value);
        };

        asyncMethod().GetAwaiter().GetResult();

        Assert.Equal(1, jtLocal.Value);
    }
コード例 #3
0
    public void AsyncLocalPerfTest()
    {
        GenericParameterHelper[]? values = Enumerable.Range(1, 50000).Select(n => new GenericParameterHelper(n)).ToArray();

        var creates = Stopwatch.StartNew();

        for (int i = 0; i < values.Length; i++)
        {
            this.asyncLocal = new Microsoft.VisualStudio.Threading.AsyncLocal <GenericParameterHelper>();
        }

        creates.Stop();

        var writes = Stopwatch.StartNew();

        for (int i = 0; i < values.Length; i++)
        {
            this.asyncLocal.Value = values[0];
        }

        writes.Stop();

        var reads = Stopwatch.StartNew();

        for (int i = 0; i < values.Length; i++)
        {
            GenericParameterHelper?value = this.asyncLocal.Value;
        }

        reads.Stop();

        // We don't actually validate the perf here. We just print out the results.
        this.Logger.WriteLine("Creating {0} instances took {1} ms", values.Length, creates.ElapsedMilliseconds);
        this.Logger.WriteLine("Saving {0} values took {1} ms", values.Length, writes.ElapsedMilliseconds);
        this.Logger.WriteLine("Reading {0} values took {1} ms", values.Length, reads.ElapsedMilliseconds);
    }
コード例 #4
0
 public AsyncLocalTests(ITestOutputHelper logger)
     : base(logger)
 {
     this.asyncLocal = new Microsoft.VisualStudio.Threading.AsyncLocal <GenericParameterHelper>();
 }