< Summary

Class:Azure.Core.TestFramework.ClientTestBase
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\ClientTestBase.cs
Covered lines:39
Uncovered lines:2
Coverable lines:41
Total lines:107
Line coverage:95.1% (39 of 41)
Covered branches:25
Total branches:26
Branch coverage:96.1% (25 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
get_IsAsync()-100%100%
get_TestDiagnostics()-100%100%
.ctor(...)-100%100%
CreateClient(...)-100%100%
InstrumentClient(...)-100%100%
InstrumentClient(...)-100%100%
InstrumentClient(...)-92.86%96.15%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Linq;
 8using System.Reflection;
 9using Castle.DynamicProxy;
 10using NUnit.Framework;
 11
 12namespace Azure.Core.TestFramework
 13{
 14    [ClientTestFixture]
 15    public abstract class ClientTestBase
 16    {
 6617        private static readonly ProxyGenerator s_proxyGenerator = new ProxyGenerator();
 18
 6619        private static readonly IInterceptor s_useSyncInterceptor = new UseSyncMethodsInterceptor(forceSync: true);
 6620        private static readonly IInterceptor s_avoidSyncInterceptor = new UseSyncMethodsInterceptor(forceSync: false);
 6621        private static readonly IInterceptor s_diagnosticScopeValidatingInterceptor = new DiagnosticScopeValidatingInter
 6622        private static Dictionary<Type, Exception> s_clientValidation = new Dictionary<Type, Exception>();
 7147323        public bool IsAsync { get; }
 24
 320025        public bool TestDiagnostics { get; set; } = true;
 26
 115827        public ClientTestBase(bool isAsync)
 28        {
 115829            IsAsync = isAsync;
 115830        }
 31
 32        protected TClient CreateClient<TClient>(params object[] args) where TClient : class
 33        {
 298834            return InstrumentClient((TClient)Activator.CreateInstance(typeof(TClient), args));
 35        }
 36
 3096937        public TClient InstrumentClient<TClient>(TClient client) where TClient : class => (TClient)InstrumentClient(type
 38
 79639        protected TClient InstrumentClient<TClient>(TClient client, IEnumerable<IInterceptor> preInterceptors) where TCl
 40
 41        internal object InstrumentClient(Type clientType, object client, IEnumerable<IInterceptor> preInterceptors)
 42        {
 7194143            if (client is IProxyTargetAccessor)
 44            {
 45                // Already instrumented
 1018046                return client;
 47            }
 48
 6176149            lock (s_clientValidation)
 50            {
 6176151                if (!s_clientValidation.TryGetValue(clientType, out var validationException))
 52                {
 2140853                    foreach (MethodInfo methodInfo in clientType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
 54                    {
 1025955                        if (methodInfo.Name.EndsWith("Async") && !methodInfo.IsVirtual)
 56                        {
 257                            validationException = new InvalidOperationException($"Client type contains public non-virtua
 58
 259                            break;
 60                        }
 61
 62
 1025763                        if (methodInfo.Name.EndsWith("Client") &&
 1025764                            methodInfo.Name.StartsWith("Get") &&
 1025765                            !methodInfo.IsVirtual)
 66                        {
 067                            validationException = new InvalidOperationException($"Client type contains public non-virtua
 68
 069                            break;
 70                        }
 71                    }
 72
 44673                    s_clientValidation[clientType] = validationException;
 74                }
 75
 6176176                if (validationException != null)
 77                {
 478                    throw validationException;
 79                }
 6175780            }
 81
 6175782            List<IInterceptor> interceptors = new List<IInterceptor>();
 6175783            if (preInterceptors != null)
 84            {
 4097285                interceptors.AddRange(preInterceptors);
 86            }
 87
 6175788            if (TestDiagnostics)
 89            {
 6112190                interceptors.Add(s_diagnosticScopeValidatingInterceptor);
 91            }
 92
 93            // Ignore the async method interceptor entirely if we're running a
 94            // a SyncOnly test
 6175795            TestContext.TestAdapter test = TestContext.CurrentContext.Test;
 6175796            bool? isSyncOnly = (bool?)test.Properties.Get(ClientTestFixtureAttribute.SyncOnlyKey);
 6175797            if (isSyncOnly != true)
 98            {
 6171699                interceptors.Add(IsAsync ? s_avoidSyncInterceptor : s_useSyncInterceptor);
 100            }
 101
 61757102            interceptors.Add(new InstrumentClientInterceptor(this));
 103
 61757104            return s_proxyGenerator.CreateClassProxyWithTarget(clientType, client, interceptors.ToArray());
 105        }
 106    }
 107}