-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Update tests and framework to instrument all clients recursively #19443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
m-nash
merged 10 commits into
Azure:feature/mgmt-track2
from
AME-Redmond:mnash-5599-updateTests
Mar 16, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d86a316
Framework changes
m-nash 2210fb5
Azure.ResourceManager.Core changes to support client interceptors
m-nash 103cd51
Test changes
m-nash c77336f
proto client changes
m-nash d0274f2
merge from master
m-nash b42629b
updates after merge
m-nash 6ad5a09
Address review comments
m-nash e4d85d8
WIP
m-nash a239648
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
m-nash 9402bfe
updates after WIP
m-nash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
sdk/core/Azure.Core.TestFramework/src/AsyncPageableInterceptor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Castle.DynamicProxy; | ||
|
|
||
| namespace Azure.Core.TestFramework | ||
| { | ||
| public class AsyncPageableInterceptor<T> : IAsyncEnumerator<T> | ||
| where T : class | ||
| { | ||
| private ClientTestBase _testBase; | ||
| private IAsyncEnumerator<T> _inner; | ||
|
|
||
| public AsyncPageableInterceptor(ClientTestBase testBase, IAsyncEnumerator<T> inner) | ||
| { | ||
| _testBase = testBase; | ||
| _inner = inner; | ||
| } | ||
|
|
||
| public T Current => _testBase.InstrumentClient(typeof(T), _inner.Current, new IInterceptor[] { new ManagementInterceptor(_testBase) }) as T; | ||
|
|
||
| public ValueTask<bool> MoveNextAsync() | ||
| { | ||
| return _inner.MoveNextAsync(); | ||
| } | ||
|
|
||
| public ValueTask DisposeAsync() | ||
| { | ||
| return _inner.DisposeAsync(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sdk/core/Azure.Core.TestFramework/src/ManagementInterceptor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Castle.DynamicProxy; | ||
|
|
||
| namespace Azure.Core.TestFramework | ||
| { | ||
| internal class ManagementInterceptor : IInterceptor | ||
| { | ||
| private readonly ClientTestBase _testBase; | ||
| private static readonly ProxyGenerator s_proxyGenerator = new ProxyGenerator(); | ||
|
|
||
| public ManagementInterceptor(ClientTestBase testBase) | ||
| { | ||
| _testBase = testBase; | ||
| } | ||
|
|
||
| public void Intercept(IInvocation invocation) | ||
| { | ||
| invocation.Proceed(); | ||
|
|
||
| var result = invocation.ReturnValue; | ||
| if (result == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var type = result.GetType(); | ||
|
|
||
| if (type.Name.StartsWith("Task")) | ||
| { | ||
| var taskResultType = type.GetGenericArguments()[0]; | ||
| if (taskResultType.Name.StartsWith("ArmResponse") || taskResultType.Name.StartsWith("ArmOperation")) | ||
| { | ||
| var taskResult = result.GetType().GetProperty("Result").GetValue(result); | ||
| var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) }); | ||
| var method = typeof(Task).GetMethod("FromResult", BindingFlags.Public | BindingFlags.Static); | ||
| var genericType = taskResultType.Name.StartsWith("Ph") ? taskResultType.BaseType : taskResultType; //TODO: remove after 5279 and 5284 | ||
| var genericMethod = method.MakeGenericMethod(genericType); | ||
| invocation.ReturnValue = genericMethod.Invoke(null, new object[] { instrumentedResult }); | ||
| } | ||
| } | ||
| else if (invocation.Method.Name.EndsWith("Value") && type.BaseType.Name.EndsWith("Operations")) | ||
| { | ||
| invocation.ReturnValue = _testBase.InstrumentClient(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) }); | ||
| } | ||
| else if (type.BaseType.Name.StartsWith("AsyncPageable")) | ||
| { | ||
| invocation.ReturnValue = s_proxyGenerator.CreateClassProxyWithTarget(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) }); | ||
| } | ||
| else if (invocation.Method.Name.StartsWith("Get") && invocation.Method.Name.EndsWith("Enumerator")) | ||
| { | ||
| var wrapperType = typeof(AsyncPageableInterceptor<>); | ||
| var genericType = wrapperType.MakeGenericType(type.GetGenericArguments()[0]); | ||
| var ctor = genericType.GetConstructor(new Type[] { typeof(ClientTestBase), result.GetType() }); | ||
| invocation.ReturnValue = ctor.Invoke(new object[] { _testBase, result }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.