~anuraj.p/nunitv2/GUIFindTest

« back to all changes in this revision

Viewing changes to src/NUnitCore/core/NUnitAsyncTestMethod.cs

  • Committer: Simone
  • Author(s): Simone Busoli
  • Date: 2012-10-28 14:36:34 UTC
  • Revision ID: simone.busoli@vienna-20121028143634-3bbd7uuche6vub4h
Support for async anonymous delegates in Throws assertions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.Collections.Generic;
3
 
using System.Reflection;
4
 
using System.Threading;
5
 
 
 
1
using System.Reflection;
 
2
 
6
3
namespace NUnit.Core
7
4
{
8
5
    public class NUnitAsyncTestMethod : NUnitTestMethod
9
6
    {
10
 
            private const string TaskWaitMethod = "Wait";
11
 
            private const string TaskResultProperty = "Result";
12
 
            private const string SystemAggregateException = "System.AggregateException";
13
 
            private const string InnerExceptionsProperty = "InnerExceptions";
14
 
            private const BindingFlags TaskResultPropertyBindingFlags = BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public;
15
 
 
16
7
            public NUnitAsyncTestMethod(MethodInfo method) : base(method)
17
8
        {
18
9
        }
19
10
 
20
 
        protected override object RunTestMethod(TestResult testResult)
21
 
        {
22
 
            if (method.ReturnType == typeof (void))
23
 
            {
24
 
                return RunVoidAsyncMethod(testResult);
25
 
            }
26
 
            
27
 
            return RunTaskAsyncMethod(testResult);
28
 
        }
29
 
 
30
 
            private object RunVoidAsyncMethod(TestResult testResult)
31
 
        {
32
 
            var previousContext = SynchronizationContext.Current;
33
 
            var currentContext = new AsyncSynchronizationContext();
34
 
            SynchronizationContext.SetSynchronizationContext(currentContext);
35
 
 
36
 
            try
37
 
            {
38
 
                object result = base.RunTestMethod(testResult);
39
 
 
40
 
                    try
41
 
                    {
42
 
                            currentContext.WaitForPendingOperationsToComplete();
43
 
                    }
44
 
                    catch (Exception e)
45
 
                    {
46
 
                                        throw new NUnitException("Rethrown", e);                            
47
 
                    }
48
 
 
49
 
                return result;
50
 
            }
51
 
            finally
52
 
            {
53
 
                SynchronizationContext.SetSynchronizationContext(previousContext);
54
 
            }
55
 
        }
56
 
 
57
 
            private object RunTaskAsyncMethod(TestResult testResult)
58
 
            {
59
 
                    try
60
 
                    {
61
 
                            object task = base.RunTestMethod(testResult);
62
 
 
63
 
                            Reflect.InvokeMethod(method.ReturnType.GetMethod(TaskWaitMethod, new Type[0]), task);
64
 
                            PropertyInfo resultProperty = Reflect.GetNamedProperty(method.ReturnType, TaskResultProperty, TaskResultPropertyBindingFlags);
65
 
 
66
 
                            return resultProperty != null ? resultProperty.GetValue(task, null) : task;
67
 
                    }
68
 
                    catch (NUnitException e)
69
 
                    {
70
 
                            if (e.InnerException != null && 
71
 
                                        e.InnerException.GetType().FullName.Equals(SystemAggregateException))
72
 
                            {
73
 
                                    IList<Exception> inner = (IList<Exception>)e.InnerException.GetType()
74
 
                                                .GetProperty(InnerExceptionsProperty).GetValue(e.InnerException, null);
75
 
 
76
 
                                    throw new NUnitException("Rethrown", inner[0]);
77
 
                            }
78
 
 
79
 
                            throw;
80
 
                    }
81
 
            }
82
 
    }
 
11
        protected override object RunTestMethod()
 
12
        {
 
13
                        using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(method))
 
14
                        {
 
15
                                var result = base.RunTestMethod();
 
16
 
 
17
                                try
 
18
                                {
 
19
                                        return region.WaitForPendingOperationsToComplete(result);
 
20
                                }
 
21
                                catch (AsyncInvocationException e)
 
22
                                {
 
23
                                        throw new NUnitException("Rethrown", e.InnerException);
 
24
                                }
 
25
                        }
 
26
        }
 
27
    }
83
28
}
 
 
b'\\ No newline at end of file'