示例#1
0
        public async Task InvokeAsync_T_CanRethrowExceptions()
        {
            // Arrange
            var context = new CircuitSynchronizationContext();

            // Act
            var task = context.InvokeAsync <string>(() =>
            {
                throw new InvalidTimeZoneException();
            });

            // Assert
            await Assert.ThrowsAsync <InvalidTimeZoneException>(async() => await task);
        }
示例#2
0
        public async Task InvokeAsync_T_CanRunSynchronously_WhenNotBusy()
        {
            // Arrange
            var context = new CircuitSynchronizationContext();
            var thread  = Thread.CurrentThread;

            // Act
            var task = context.InvokeAsync(() =>
            {
                return(Task.FromResult(Thread.CurrentThread));
            });

            // Assert
            Assert.Same(thread, await task);
        }
示例#3
0
        public async Task InvokeAsync_Void_CanRunAsynchronously_WhenBusy()
        {
            // Arrange
            var    context        = new CircuitSynchronizationContext();
            var    thread         = Thread.CurrentThread;
            Thread capturedThread = null;

            var e1 = new ManualResetEventSlim();
            var e2 = new ManualResetEventSlim();
            var e3 = new ManualResetEventSlim();

            var task1 = Task.Run(() =>
            {
                context.Send((_) =>
                {
                    e1.Set();
                    Assert.True(e2.Wait(Timeout), "timeout");
                }, null);
            });

            Assert.True(e1.Wait(Timeout), "timeout");

            var task2 = context.InvokeAsync(() =>
            {
                capturedThread = Thread.CurrentThread;

                e3.Set();
                return(Task.CompletedTask);
            });

            // Assert
            Assert.False(e2.IsSet);
            e2.Set(); // Unblock the first item
            await task1;

            Assert.True(e3.Wait(Timeout), "timeout");
            await task2;

            Assert.NotSame(thread, capturedThread);
        }