Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ public void Intercept(IInvocation invocation)
}

var type = result.GetType();
if (type.Name.StartsWith("ValueTask"))
if (type.Name.StartsWith("ValueTask") ||
type.Name.StartsWith("Task") ||
type.Name.StartsWith("AsyncStateMachineBox")) //in .net 5 the type is not task here
{
if ((bool)type.GetProperty("IsFaulted").GetValue(result))
return;

var taskResultType = type.GetGenericArguments()[0];
if (taskResultType.Name.StartsWith("Response") || taskResultType.Name.StartsWith("ArmResponse"))
if (taskResultType.Name.StartsWith("Response"))
{
var taskResult = result.GetType().GetProperty("Result").GetValue(result);
var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) });
var genericValueTask = typeof(ValueTask<>).MakeGenericType(taskResultType);
var vtCtor = genericValueTask.GetConstructor(new Type[] { taskResultType });
invocation.ReturnValue = vtCtor.Invoke(new object[] { instrumentedResult });
invocation.ReturnValue = type.Name.StartsWith("ValueTask")
? GetValueFromValueTask(taskResultType, instrumentedResult)
: GetValueFromOther(taskResultType, instrumentedResult);
}
}
else if (invocation.Method.Name.EndsWith("Value") && type.BaseType.Name.EndsWith("Operations"))
Expand All @@ -60,5 +62,19 @@ public void Intercept(IInvocation invocation)
invocation.ReturnValue = ctor.Invoke(new object[] { _testBase, result });
}
}

private object GetValueFromOther(Type taskResultType, object instrumentedResult)
{
var method = typeof(Task).GetMethod("FromResult", BindingFlags.Public | BindingFlags.Static);
var genericMethod = method.MakeGenericMethod(taskResultType);
return genericMethod.Invoke(null, new object[] { instrumentedResult });
}

private object GetValueFromValueTask(Type taskResultType, object instrumentedResult)
{
var genericValueTask = typeof(ValueTask<>).MakeGenericType(taskResultType);
var vtCtor = genericValueTask.GetConstructor(new Type[] { taskResultType });
return vtCtor.Invoke(new object[] { instrumentedResult });
}
}
}
10 changes: 5 additions & 5 deletions sdk/core/Azure.Core/Azure.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Test.Perf", "..\..\..
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager.Core", "..\..\resourcemanager\Azure.ResourceManager.Core\src\Azure.ResourceManager.Core.csproj", "{8E60A748-3973-471A-B103-EC9406BB3313}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.TextAnalytics", "..\..\textanalytics\Azure.AI.TextAnalytics\src\Azure.AI.TextAnalytics.csproj", "{B1FFA603-1CAA-4CE0-97E9-CA6CE9D498B9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.TextAnalytics", "..\..\textanalytics\Azure.AI.TextAnalytics\src\Azure.AI.TextAnalytics.csproj", "{9E2B7F26-D6AE-459B-ADFC-C34949674ED5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -95,10 +95,10 @@ Global
{8E60A748-3973-471A-B103-EC9406BB3313}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E60A748-3973-471A-B103-EC9406BB3313}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E60A748-3973-471A-B103-EC9406BB3313}.Release|Any CPU.Build.0 = Release|Any CPU
{B1FFA603-1CAA-4CE0-97E9-CA6CE9D498B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1FFA603-1CAA-4CE0-97E9-CA6CE9D498B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1FFA603-1CAA-4CE0-97E9-CA6CE9D498B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1FFA603-1CAA-4CE0-97E9-CA6CE9D498B9}.Release|Any CPU.Build.0 = Release|Any CPU
{9E2B7F26-D6AE-459B-ADFC-C34949674ED5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E2B7F26-D6AE-459B-ADFC-C34949674ED5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E2B7F26-D6AE-459B-ADFC-C34949674ED5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E2B7F26-D6AE-459B-ADFC-C34949674ED5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<ItemGroup>
<ProjectReference Include="$(AzureCoreTestFramework)" />
<ProjectReference Include="..\..\..\resourcemanager\Azure.ResourceManager.Core\src\Azure.ResourceManager.Core.csproj" />
<ProjectReference Include="..\..\..\textanalytics\Azure.AI.TextAnalytics\src\Azure.AI.TextAnalytics.csproj" />
<ProjectReference Include="..\src\Azure.Core.csproj" />
<ProjectReference Include="..\..\Microsoft.Azure.Core.NewtonsoftJson\src\Microsoft.Azure.Core.NewtonsoftJson.csproj" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
<ProjectReference Include="..\..\..\textanalytics\Azure.AI.TextAnalytics\src\Azure.AI.TextAnalytics.csproj" />
Comment thread
allenjzhang marked this conversation as resolved.
</ItemGroup>
<ItemGroup>
<Compile Include="..\src\Shared\Multipart\*.cs" LinkBase="Shared\Multipart" />
Expand Down
75 changes: 48 additions & 27 deletions sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Resources.Models;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

Expand Down Expand Up @@ -67,30 +66,6 @@ public async Task ValidateInstrumentArmResponse()
Assert.AreEqual("success", result);
}

[Test]
public async Task ValidateInstrumentPhArmOperation()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
var sub = client.DefaultSubscription;
var operation = (await sub.GetPhArmOperationAsync()).Value;
var result = operation.Method();

Assert.AreEqual("TestResourceProxy", operation.GetType().Name);
Assert.AreEqual("success", result);
}

[Test]
public async Task ValidateInstrumentPhArmResponse()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
var sub = client.DefaultSubscription;
var response = (await sub.GetPhArmOperationAsync()).Value;
var result = response.Method();

Assert.AreEqual("TestResourceProxy", response.GetType().Name);
Assert.AreEqual("success", result);
}

[Test]
[SyncOnly]
public void ValidateInstrumentGetContainer()
Expand Down Expand Up @@ -141,7 +116,7 @@ public void ValidateExceptionResponse()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
Assert.ThrowsAsync(typeof(ArgumentException), async () => await rgOp.GetArmResponseExceptionAsync());
Assert.ThrowsAsync(typeof(ArgumentException), async () => await rgOp.GetResponseExceptionAsync());
}

[Test]
Expand All @@ -160,5 +135,51 @@ public async Task ValidateExceptionOperationWaitForCompletion()
var testResourceOp = await rgOp.GetArmOperationAsync(true);
Assert.ThrowsAsync(typeof(ArgumentException), async () => await testResourceOp.WaitForCompletionAsync());
}

[Test]
public async Task ValidateLroWrapper()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource testResource = await rgOp.LroWrapperAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
}

[Test]
public async Task ValidateStartLroWrapper()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
var testResourceOp = await rgOp.StartLroWrapperAsync();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
}

[Test]
public async Task ValidateSkipWait()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
Stopwatch timer = Stopwatch.StartNew();
TestResource testResource = await rgOp.LroWrapperAsync();
timer.Stop();
//method waits for 10 seconds so timer should easily be less than half of that if we skip
Assert.IsTrue(timer.ElapsedMilliseconds < 5000, $"WaitForCompletion took {timer.ElapsedMilliseconds}ms");
}

[Test]
public async Task ValidateStartSkipWait()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
var testResourceOp = await rgOp.StartLroWrapperAsync();
Stopwatch timer = Stopwatch.StartNew();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
timer.Stop();
//method waits for 10 seconds so timer should easily be less than half of that if we skip
Assert.IsTrue(timer.ElapsedMilliseconds < 5000, $"WaitForCompletion took {timer.ElapsedMilliseconds}ms");
}
}
}
19 changes: 11 additions & 8 deletions sdk/core/Azure.Core/tests/TestClients/ArmOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ public class ArmOperationTest : Operation<TestResource>, IOperationSource<TestRe
private TestResource _value;
private bool _exceptionOnWait;
private OperationOrResponseInternals<TestResource> _operationHelper;
private int _delaySteps = 0;

protected ArmOperationTest()
{
}

public ArmOperationTest(TestResource value, bool exceptionOnWait = false)
public ArmOperationTest(TestResource value, bool exceptionOnWait = false, int delaySteps = 0)
{
_value = value;
_exceptionOnWait = exceptionOnWait;
_operationHelper = new OperationOrResponseInternals<TestResource>(Response.FromValue(value, new MockResponse(200)));
_delaySteps = delaySteps;
}

public override string Id => "testId";
Expand All @@ -35,20 +37,21 @@ public ArmOperationTest(TestResource value, bool exceptionOnWait = false)

public override Response GetRawResponse() => _operationHelper.GetRawResponse();

public override ValueTask<Response<TestResource>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
public async override ValueTask<Response<TestResource>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<TestResource>>(Response.FromValue(_value, new MockResponse(200)));
return await WaitForCompletionAsync(OperationInternals.DefaultPollingInterval, cancellationToken);
}

public override ValueTask<Response<TestResource>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
public async override ValueTask<Response<TestResource>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<TestResource>>(Response.FromValue(_value, new MockResponse(200)));
for (int i = 0; i < _delaySteps; i++)
{
await Task.Delay(pollingInterval);
}
return Response.FromValue(_value, new MockResponse(200));
}

public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatusAsync(cancellationToken);
Expand Down
19 changes: 0 additions & 19 deletions sdk/core/Azure.Core/tests/TestClients/ArmResponseTest.cs

This file was deleted.

43 changes: 0 additions & 43 deletions sdk/core/Azure.Core/tests/TestClients/PhArmOperationTest.cs

This file was deleted.

16 changes: 0 additions & 16 deletions sdk/core/Azure.Core/tests/TestClients/PhArmResponseTest.cs

This file was deleted.

24 changes: 8 additions & 16 deletions sdk/core/Azure.Core/tests/TestClients/TestResourceContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT License.

using System;
using System.Linq;
Comment thread
allenjzhang marked this conversation as resolved.
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
using Azure.ResourceManager.Core;
using static Azure.Core.PageResponseEnumerator;
using Azure.Core.TestFramework;

namespace Azure.Core.Tests
{
Expand All @@ -16,11 +16,6 @@ public class TestResourceContainer

[ForwardsClientCalls]
public virtual Pageable<TestResource> List(int pages = 1, CancellationToken cancellation = default)
{
return GetSyncResults(pages);
}

private FuncPageable<TestResource> GetSyncResults(int pages)
{
Page<TestResource> pageFunc(int? pageSizeHint)
{
Expand All @@ -30,24 +25,20 @@ Page<TestResource> pageFunc(int? pageSizeHint)

try
{
return Page<TestResource>.FromValues(new TestResource[] { new TestResource(), new TestResource() }, null, null);
var result = new object[] { new object(), new object() };
return Page.FromValues(result.Select(o => new TestResource()), null, new MockResponse(200));
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
return new FuncPageable<TestResource>((cToken, pageSize) => pageFunc(pageSize));
return PageableHelpers.CreateEnumerable(pageFunc, null);
}

[ForwardsClientCalls]
public virtual AsyncPageable<TestResource> ListAsync(int pages = 1, CancellationToken cancellation = default)
{
return GetAsyncResults(pages);
}

private FuncAsyncPageable<TestResource> GetAsyncResults(int pages)
{
async Task<Page<TestResource>> pageFunc(int? pageSizeHint)
{
Expand All @@ -57,15 +48,16 @@ async Task<Page<TestResource>> pageFunc(int? pageSizeHint)
try
{
await Task.Delay(10);
return Page<TestResource>.FromValues(new TestResource[] { new TestResource(), new TestResource() }, null, null);
var result = new object[] { new object(), new object() };
return Page.FromValues(result.Select(o => new TestResource()), null, new MockResponse(200));
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
return new FuncAsyncPageable<TestResource>((cToken, pageSize) => pageFunc(pageSize));
return PageableHelpers.CreateAsyncEnumerable(pageFunc, null);
}

public virtual string Method()
Expand Down
Loading