| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Reflection; |
| | 9 | | using Castle.DynamicProxy; |
| | 10 | | using NUnit.Framework; |
| | 11 | |
|
| | 12 | | namespace Azure.Core.TestFramework |
| | 13 | | { |
| | 14 | | [ClientTestFixture] |
| | 15 | | public abstract class ClientTestBase |
| | 16 | | { |
| 66 | 17 | | private static readonly ProxyGenerator s_proxyGenerator = new ProxyGenerator(); |
| | 18 | |
|
| 66 | 19 | | private static readonly IInterceptor s_useSyncInterceptor = new UseSyncMethodsInterceptor(forceSync: true); |
| 66 | 20 | | private static readonly IInterceptor s_avoidSyncInterceptor = new UseSyncMethodsInterceptor(forceSync: false); |
| 66 | 21 | | private static readonly IInterceptor s_diagnosticScopeValidatingInterceptor = new DiagnosticScopeValidatingInter |
| 66 | 22 | | private static Dictionary<Type, Exception> s_clientValidation = new Dictionary<Type, Exception>(); |
| 71473 | 23 | | public bool IsAsync { get; } |
| | 24 | |
|
| 3200 | 25 | | public bool TestDiagnostics { get; set; } = true; |
| | 26 | |
|
| 1158 | 27 | | public ClientTestBase(bool isAsync) |
| | 28 | | { |
| 1158 | 29 | | IsAsync = isAsync; |
| 1158 | 30 | | } |
| | 31 | |
|
| | 32 | | protected TClient CreateClient<TClient>(params object[] args) where TClient : class |
| | 33 | | { |
| 2988 | 34 | | return InstrumentClient((TClient)Activator.CreateInstance(typeof(TClient), args)); |
| | 35 | | } |
| | 36 | |
|
| 30969 | 37 | | public TClient InstrumentClient<TClient>(TClient client) where TClient : class => (TClient)InstrumentClient(type |
| | 38 | |
|
| 796 | 39 | | protected TClient InstrumentClient<TClient>(TClient client, IEnumerable<IInterceptor> preInterceptors) where TCl |
| | 40 | |
|
| | 41 | | internal object InstrumentClient(Type clientType, object client, IEnumerable<IInterceptor> preInterceptors) |
| | 42 | | { |
| 71941 | 43 | | if (client is IProxyTargetAccessor) |
| | 44 | | { |
| | 45 | | // Already instrumented |
| 10180 | 46 | | return client; |
| | 47 | | } |
| | 48 | |
|
| 61761 | 49 | | lock (s_clientValidation) |
| | 50 | | { |
| 61761 | 51 | | if (!s_clientValidation.TryGetValue(clientType, out var validationException)) |
| | 52 | | { |
| 21408 | 53 | | foreach (MethodInfo methodInfo in clientType.GetMethods(BindingFlags.Instance | BindingFlags.Public) |
| | 54 | | { |
| 10259 | 55 | | if (methodInfo.Name.EndsWith("Async") && !methodInfo.IsVirtual) |
| | 56 | | { |
| 2 | 57 | | validationException = new InvalidOperationException($"Client type contains public non-virtua |
| | 58 | |
|
| 2 | 59 | | break; |
| | 60 | | } |
| | 61 | |
|
| | 62 | |
|
| 10257 | 63 | | if (methodInfo.Name.EndsWith("Client") && |
| 10257 | 64 | | methodInfo.Name.StartsWith("Get") && |
| 10257 | 65 | | !methodInfo.IsVirtual) |
| | 66 | | { |
| 0 | 67 | | validationException = new InvalidOperationException($"Client type contains public non-virtua |
| | 68 | |
|
| 0 | 69 | | break; |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| 446 | 73 | | s_clientValidation[clientType] = validationException; |
| | 74 | | } |
| | 75 | |
|
| 61761 | 76 | | if (validationException != null) |
| | 77 | | { |
| 4 | 78 | | throw validationException; |
| | 79 | | } |
| 61757 | 80 | | } |
| | 81 | |
|
| 61757 | 82 | | List<IInterceptor> interceptors = new List<IInterceptor>(); |
| 61757 | 83 | | if (preInterceptors != null) |
| | 84 | | { |
| 40972 | 85 | | interceptors.AddRange(preInterceptors); |
| | 86 | | } |
| | 87 | |
|
| 61757 | 88 | | if (TestDiagnostics) |
| | 89 | | { |
| 61121 | 90 | | interceptors.Add(s_diagnosticScopeValidatingInterceptor); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | // Ignore the async method interceptor entirely if we're running a |
| | 94 | | // a SyncOnly test |
| 61757 | 95 | | TestContext.TestAdapter test = TestContext.CurrentContext.Test; |
| 61757 | 96 | | bool? isSyncOnly = (bool?)test.Properties.Get(ClientTestFixtureAttribute.SyncOnlyKey); |
| 61757 | 97 | | if (isSyncOnly != true) |
| | 98 | | { |
| 61716 | 99 | | interceptors.Add(IsAsync ? s_avoidSyncInterceptor : s_useSyncInterceptor); |
| | 100 | | } |
| | 101 | |
|
| 61757 | 102 | | interceptors.Add(new InstrumentClientInterceptor(this)); |
| | 103 | |
|
| 61757 | 104 | | return s_proxyGenerator.CreateClassProxyWithTarget(clientType, client, interceptors.ToArray()); |
| | 105 | | } |
| | 106 | | } |
| | 107 | | } |