public void FireAndForgetResetContextDoesNotNotInterfereWIthTop()
        {
            Guid   customId = Guid.Empty;
            Thread thread   = null;
            var    provider = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");

            // Action
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetResetContext(() => FindCustomValue());
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());

            // Func<Task>
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetResetContext(async() => await FindCustomValueAsync());
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());

            // Func<CancellationToken, Task>
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetResetContext(async(ct) => await FindCustomValueAsync(),
                                                                 CancellationToken.None);
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());
        }
        public void FireAndForgetWithExpensiveStackTracePreservationCanReadCustomContextValue()
        {
            Guid   customId = Guid.Empty;
            Thread thread   = null;
            var    provider = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");

            // Action
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetWithExpensiveStackTracePreservation(() => FindCustomValue());
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());

            // Func<Task>
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetWithExpensiveStackTracePreservation(async() => await FindCustomValueAsync());
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());

            // Func<CancellationToken, Task>
            customId = Guid.NewGuid();
            provider.SetValue(customId);
            _foundValue = Guid.NewGuid();
            thread      = ThreadHelper.FireAndForgetWithExpensiveStackTracePreservation(async(ct) => await FindCustomValueAsync(),
                                                                                        CancellationToken.None);
            WaitForThreadToComplete(thread);
            UT.Assert.AreEqual(customId, provider.GetValue());
        }
Beispiel #3
0
        public void ThreadsCanAccessCustomContextValue()
        {
            var customId  = Guid.NewGuid();
            var provider1 = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");

            provider1.SetValue(customId);
            var done      = false;
            var canAccess = false;
            var thread    = new BasicThreadHandler().FireAndForget(token =>
            {
                var provider2 = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");
                canAccess     = customId == provider2.GetValue();
                done          = true;
            });

            UT.Assert.IsNotNull(thread);
            while (!done)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(10));
            }

            UT.Assert.IsTrue(canAccess);
            Thread.Sleep(TimeSpan.FromMilliseconds(10));
            UT.Assert.IsFalse(thread.IsAlive);
        }
        private async Task FindCustomValueAsync()
        {
            var provider = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");

            _foundValue = provider.GetValue();
            await Task.CompletedTask;
        }
        private void FindCustomValue()
        {
            var provider = new OneValueProvider <Guid>(FulcrumApplication.Context.ValueProvider, "Custom");

            _foundValue = provider.GetValue();
        }