Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FinallyDelegate.cs « framework « src « NUnitLite-1.0.0 - github.com/mono/NUnitLite.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfa94b600664c328bf12f29d32ca97ee2f0704ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// ***********************************************************************
// Author:
//	Alexander Kyte <alexander.kyte@xamarin.com>
//
// Copyright (c) 2016 Xamarin, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

#if __IOS__ || __WATCHOS__ || __TVOS__
#define NO_REMOTING
#endif

using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework.Api;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;

namespace NUnit.Framework.Internal
{
	[Serializable]
	class Container : ILogicalThreadAffinative {
		public Guid guid;
		public Container(Guid guid) {
			this.guid = guid;
		}
	}

	public class FinallyDelegate
	{
		// If our test spawns a thread that throws, we will bubble
		// up to the top level. To handle this, we need to have the
		// failure logic in a place that can be called from the top level.
		// The UnhandledException callback should run on a thread that isn't
		// the one dispatching the tests so this should work to ensure
		// progress in the face of exceptions on other threads
		//
		// Essentially this is a poor-man's finally clause that's
		// guaranteed to run because we pin it to the UnhandledException
		// callback

		// Because of CompositeWorkItem, we have a runtime stack of work items
		// so we need a stack of finally delegate continuations
		Stack<Tuple<TestExecutionContext, long, TestResult>> testStack;

		Dictionary<Guid, TestResult> lookupTable;

		// Why is CallContext used? Consider the following scenario:
		//
		// * say Test_A runs in Thread_1
		// * Test_A spawns another Thread_2
		// * Thread_1 finishes with Test_A, and moves on to Test_B
		// * Thread_2 isn't done yet really, crashes and causes an unhandled exception
		// * we need a way to map this unhandled exception to Test_A, although Test_B is the currently running test
		//
		// => what we need is some sort of "thread local" that gets inherited: when
		// Thread_1 creates Thread_2, it needs to have the same "thread local" values.
		// that is achieved with `CallContext`.
		//
		// Unfortunately, remoting isn't available on every platform, thus we can't
		// support this scenario. Luckily, this scenario is very rare.

#if NO_REMOTING
		static Container container;
#else
		private static readonly string CONTEXT_KEY = "TestResultName";
#endif

		public FinallyDelegate () {
			this.testStack = new Stack<Tuple<TestExecutionContext, long, TestResult>>();
			this.lookupTable = new Dictionary<Guid, TestResult>();
		}

		public void Set (TestExecutionContext context, long startTicks, TestResult result) {
			var frame = new Tuple<TestExecutionContext, long, TestResult>(context, startTicks, result);

			/* keep name in LogicalCallContext, because this will be inherited by
			 * Threads spawned by the test case */
			var guid = Guid.NewGuid();
#if NO_REMOTING
			container = new Container (guid);
#else
			CallContext.SetData(CONTEXT_KEY, new Container(guid));
#endif

			this.lookupTable.Add(guid, result);
			this.testStack.Push(frame);
		}

		public void HandleUnhandledExc (Exception ex) {
#if NO_REMOTING
			Container c = container;
#else
			Container c = (Container) CallContext.GetData(CONTEXT_KEY);
#endif
			TestResult result = this.lookupTable [c.guid];
			result.RecordException(ex);
			result.ThreadCrashFail = true;
		}

		public void Complete () {
			var frame = this.testStack.Pop();

			TestExecutionContext context = frame.Item1;
			long startTicks = frame.Item2;
			TestResult result = frame.Item3;

#if (CLR_2_0 || CLR_4_0) && !SILVERLIGHT && !NETCF_2_0
			long tickCount = Stopwatch.GetTimestamp() - startTicks;
			double seconds = (double)tickCount / Stopwatch.Frequency;
			result.Duration = TimeSpan.FromSeconds(seconds);
#else
			result.Duration = DateTime.Now - Context.StartTime;
#endif

			result.AssertCount = context.AssertCount;

			context.Listener.TestFinished(result);

			context = context.Restore();
			context.AssertCount += result.AssertCount;
		}
	}
}