< Summary

Class:Azure.Core.TestFramework.DiagnosticScopeValidatingInterceptor
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\DiagnosticScopeValidatingInterceptor.cs
Covered lines:41
Uncovered lines:1
Coverable lines:42
Total lines:106
Line coverage:97.6% (41 of 42)
Covered branches:23
Total branches:24
Branch coverage:95.8% (23 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Intercept(...)-97.62%95.83%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\DiagnosticScopeValidatingInterceptor.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Linq;
 6using System.Reflection;
 7using System.Threading.Tasks;
 8using Azure.Core.Tests;
 9using Castle.DynamicProxy;
 10
 11namespace Azure.Core.TestFramework
 12{
 13    public class DiagnosticScopeValidatingInterceptor : IInterceptor
 14    {
 15        public void Intercept(IInvocation invocation)
 16        {
 16286617            var methodName = invocation.Method.Name;
 16286618            if (methodName.EndsWith("Async"))
 19            {
 7488620                Type declaringType = invocation.Method.DeclaringType;
 7488621                var ns = declaringType.Namespace;
 7488622                var expectedName = declaringType.Name + "." + methodName.Substring(0, methodName.Length - 5);
 18281805823                using ClientDiagnosticListener diagnosticListener = new ClientDiagnosticListener(s => s.StartsWith("Azur
 7488624                invocation.Proceed();
 25
 7487026                bool expectFailure = false;
 7487027                bool skipChecks = false;
 28
 15036829                bool strict = !invocation.Method.GetCustomAttributes(true).Any(a => a.GetType().FullName == "Azure.Core.
 7487030                if (invocation.Method.ReturnType.Name.Contains("Pageable") ||
 7487031                    invocation.Method.ReturnType.Name.Contains("IAsyncEnumerable"))
 32                {
 325233                    return;
 34                }
 35
 36                try
 37                {
 7161838                    object returnValue = invocation.ReturnValue;
 7161839                    if (returnValue is Task t)
 40                    {
 7124241                        t.GetAwaiter().GetResult();
 42                    }
 43                    else
 44                    {
 45                        // Await ValueTask
 37646                        Type returnType = returnValue.GetType();
 37647                        MethodInfo getAwaiterMethod = returnType.GetMethod("GetAwaiter", BindingFlags.Instance | Binding
 37648                        MethodInfo getResultMethod = getAwaiterMethod.ReturnType.GetMethod("GetResult", BindingFlags.Ins
 49
 37650                        getResultMethod.Invoke(
 37651                            getAwaiterMethod.Invoke(returnValue, Array.Empty<object>()),
 37652                            Array.Empty<object>());
 53                    }
 6801554                }
 55                catch (Exception ex)
 56                {
 360357                    expectFailure = true;
 58
 360359                    if (ex is ArgumentException)
 60                    {
 61                        // Don't expect scope for argument validation failures
 58062                        skipChecks = true;
 63                    }
 360364                }
 65                finally
 66                {
 67                    // Remove subscribers before enumerating events.
 7161868                    diagnosticListener.Dispose();
 7161869                    if (!skipChecks)
 70                    {
 7103871                        if (strict)
 72                        {
 14122473                            ClientDiagnosticListener.ProducedDiagnosticScope e = diagnosticListener.Scopes.FirstOrDefaul
 74
 7061275                            if (e == default)
 76                            {
 477                                throw new InvalidOperationException($"Expected diagnostic scope not created {expectedNam
 78                            }
 79
 16635780                            if (!e.Activity.Tags.Any(tag => tag.Key == "az.namespace"))
 81                            {
 082                                throw new InvalidOperationException($"All diagnostic scopes should have 'az.namespace' a
 83                            }
 84
 7060885                            if (expectFailure && !e.IsFailed)
 86                            {
 887                                throw new InvalidOperationException($"Expected scope {expectedName} to be marked as fail
 88                            }
 89                        }
 90                        else
 91                        {
 42692                            if (!diagnosticListener.Scopes.Any())
 93                            {
 494                                throw new InvalidOperationException($"Expected some diagnostic scopes to be created, fou
 95                            }
 96                        }
 97                    }
 7160298                }
 99            }
 100            else
 101            {
 87980102                invocation.Proceed();
 103            }
 162825104        }
 105    }
 106}

Methods/Properties

Intercept(...)