Commit b82caefa authored by Nick Craver's avatar Nick Craver

Ambient exceptions: store on an AsyncLocal<T> for test isolation

parent 486f03fc
...@@ -62,7 +62,13 @@ static TestBase() ...@@ -62,7 +62,13 @@ static TestBase()
#if NETCOREAPP1_0 #if NETCOREAPP1_0
if (IgnorableExceptionPredicates.Any(predicate => predicate(args.Exception.InnerException))) return; if (IgnorableExceptionPredicates.Any(predicate => predicate(args.Exception.InnerException))) return;
#endif #endif
Interlocked.Increment(ref sharedFailCount); lock (sharedFailCount)
{
if (sharedFailCount != null)
{
sharedFailCount.Value++;
}
}
lock (backgroundExceptions) lock (backgroundExceptions)
{ {
backgroundExceptions.Add(args.Exception.ToString()); backgroundExceptions.Add(args.Exception.ToString());
...@@ -97,7 +103,7 @@ protected void OnInternalError(object sender, InternalErrorEventArgs e) ...@@ -97,7 +103,7 @@ protected void OnInternalError(object sender, InternalErrorEventArgs e)
} }
private int privateFailCount; private int privateFailCount;
private static int sharedFailCount; private static AsyncLocal<int> sharedFailCount = new AsyncLocal<int>();
private volatile int expectedFailCount; private volatile int expectedFailCount;
private static readonly List<string> privateExceptions = new List<string>(); private static readonly List<string> privateExceptions = new List<string>();
...@@ -107,7 +113,10 @@ public void ClearAmbientFailures() ...@@ -107,7 +113,10 @@ public void ClearAmbientFailures()
{ {
Collect(); Collect();
Interlocked.Exchange(ref privateFailCount, 0); Interlocked.Exchange(ref privateFailCount, 0);
Interlocked.Exchange(ref sharedFailCount, 0); lock (sharedFailCount)
{
sharedFailCount.Value = 0;
}
expectedFailCount = 0; expectedFailCount = 0;
lock (privateExceptions) lock (privateExceptions)
{ {
...@@ -136,7 +145,13 @@ private static void Collect() ...@@ -136,7 +145,13 @@ private static void Collect()
public void Teardown() public void Teardown()
{ {
Collect(); Collect();
if (expectedFailCount >= 0 && (Interlocked.CompareExchange(ref sharedFailCount, 0, 0) + privateFailCount) != expectedFailCount) int sharedFails;
lock (sharedFailCount)
{
sharedFails = sharedFailCount.Value;
sharedFailCount.Value = 0;
}
if (expectedFailCount >= 0 && (sharedFails + privateFailCount) != expectedFailCount)
{ {
lock (privateExceptions) lock (privateExceptions)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment