Ejemplo n.º 1
0
        internal async Task Run_ActivationSched_Test1(TaskScheduler scheduler, bool bounceToThreadPool)
        {
            var grainId = GrainId.GetGrainId(0, Guid.NewGuid());
            var grain   = new NonReentrentStressGrainWithoutState(grainId, new GrainRuntime(Guid.NewGuid(), null, null, null, null, null, null));
            await grain.OnActivateAsync();

            Task        wrapped     = null;
            var         wrapperDone = new TaskCompletionSource <bool>();
            var         wrappedDone = new TaskCompletionSource <bool>();
            Task <Task> wrapper     = new Task <Task>(() =>
            {
                output.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                                 SynchronizationContext.Current, TaskScheduler.Current);

                Task t1 = grain.Test1();

                Action wrappedDoneAction = () => { wrappedDone.SetResult(true); };

                if (bounceToThreadPool)
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction(),
                                              CancellationToken.None,
                                              TaskContinuationOptions.ExecuteSynchronously,
                                              TaskScheduler.Default);
                }
                else
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction());
                }
                wrapperDone.SetResult(true);
                return(wrapped);
            });

            wrapper.Start(scheduler);
            await wrapper;

            var timeoutLimit = TimeSpan.FromSeconds(1);

            try
            {
                await wrapperDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.True(false, "Result did not arrive before timeout " + timeoutLimit);
            }
            bool done = wrapperDone.Task.Result;

            Assert.True(done, "Wrapper Task finished");
            Assert.True(wrapper.IsCompleted, "Wrapper Task completed");

            //done = wrapped.Wait(TimeSpan.FromSeconds(12));
            //Assert.True(done, "Wrapped Task not timeout");
            await wrapped;

            try
            {
                await wrappedDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.True(false, "Result did not arrive before timeout " + timeoutLimit);
            }
            done = wrappedDone.Task.Result;
            Assert.True(done, "Wrapped Task should be finished");
            Assert.True(wrapped.IsCompleted, "Wrapped Task completed");
        }
        internal static async Task Run_ActivationSched_Test1(TaskScheduler scheduler, bool bounceToThreadPool)
        {
            var grainId = GrainId.GetGrainId(0, Guid.NewGuid());
            var grain = new NonReentrentStressGrainWithoutState( grainId, new GrainRuntime(Guid.NewGuid(), null, null, null, null, null, null));
            await grain.OnActivateAsync();

            Task wrapped = null;
            var wrapperDone = new TaskCompletionSource<bool>();
            var wrappedDone = new TaskCompletionSource<bool>();
            Task<Task> wrapper = new Task<Task>(() =>
            {
                Console.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                    SynchronizationContext.Current, TaskScheduler.Current);

                Task t1 = grain.Test1();

                Action wrappedDoneAction = () => { wrappedDone.SetResult(true); };

                if (bounceToThreadPool)
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction(),
                        CancellationToken.None,
                        TaskContinuationOptions.ExecuteSynchronously,
                        TaskScheduler.Default);
                }
                else
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction());
                }
                wrapperDone.SetResult(true);
                return wrapped;
            });
            wrapper.Start(scheduler);
            await wrapper;
            
            var timeoutLimit = TimeSpan.FromSeconds(1);
            try
            {
                await wrapperDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.Fail("Result did not arrive before timeout " + timeoutLimit);
            }
            bool done = wrapperDone.Task.Result;

            Assert.IsTrue(done, "Wrapper Task finished");
            Assert.IsTrue(wrapper.IsCompleted, "Wrapper Task completed");

            //done = wrapped.Wait(TimeSpan.FromSeconds(12));
            //Assert.IsTrue(done, "Wrapped Task not timeout");
            await wrapped;
            try
            {
                await wrappedDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.Fail("Result did not arrive before timeout " + timeoutLimit);
            }
            done = wrappedDone.Task.Result;
            Assert.IsTrue(done, "Wrapped Task should be finished");
            Assert.IsTrue(wrapped.IsCompleted, "Wrapped Task completed");
        }