Skip to content
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

[QUERY] unit testing GetBlobLeaseClient().AcquireAsync #12584

Closed
henriblMSFT opened this issue Jun 6, 2020 · 8 comments
Closed

[QUERY] unit testing GetBlobLeaseClient().AcquireAsync #12584

henriblMSFT opened this issue Jun 6, 2020 · 8 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)

Comments

@henriblMSFT
Copy link

Query/Question
How are we expected to test BlobLeaseClient.AcquireAsync given that GetBlobLeaseClient is an extension method.

public async Task PerformActionAsync(BlobBaseClient client)
{
    var leaseClient = client.GetBlobLeaseClient();
    await leaseClient.AcquireAsync();
    try
    {
        await DoLeasedActionAsync();
    }
    finally
    {
        await leaseClient.ReleaseAsync();
    }
}

While it is possible to pass in a mock of BlobBaseClient the method GetBlobLeaseClient() is an extension method and cannot be mocked. it builds a concrete lease client:

        public static BlobLeaseClient GetBlobLeaseClient(
            this BlobContainerClient client,
            string leaseId = null) =>
            new BlobLeaseClient(client, leaseId);

attempting to call AcquireAsync on a BlobLeaseClient built from a mocked BlobBaseClient throws a null reference exception when it attempts to access the Pipeline member.

Environment:

  • Name and version of the Library package used: Azure.Storage.Blobs 12.4.2
  • Hosting platform or OS and .NET runtime version (dotnet --info output for .NET Core projects):
    .NET Core SDK (reflecting any global.json):
    Version: 3.1.202
    Commit: 6ea70c8dca

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19551
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.202\

Host (useful for support):
Version: 3.1.4
Commit: 0c2e69caa6

  • IDE and version :Visual Studio 16.5.5
@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jun 6, 2020
@jsquire jsquire added Client This issue points to a problem in the data-plane of the library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files) labels Jun 8, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jun 8, 2020
@ghost
Copy link

ghost commented Jun 8, 2020

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.

@amnguye
Copy link
Member

amnguye commented Jun 9, 2020

@seanmcc-msft Could you take a look at this? Looks like they're trying to make Mock Unit tests. This might be a feature request or bug. But at the same time there might not much we can do with changing what object we return for the GetBlobLeaseClient.

@seanmcc-msft
Copy link
Member

@amnguye, sure.

@seanmcc-msft
Copy link
Member

seanmcc-msft commented Jun 9, 2020

attempting to call AcquireAsync on a BlobLeaseClient built from a mocked BlobBaseClient throws a null reference exception when it attempts to access the Pipeline member.

I believe the solution would be to return a mock BlobLeaseClient from the mock BlobBaseClient, and setup the BlobLeaseClient.AcquireAsync() with the parameters you expect to be passed it.

@henriblMSFT
Copy link
Author

henriblMSFT commented Jun 9, 2020

@seanmcc-msft this is exactly my issue.
I'm attempting to return a mock BlobLeaseClient but I'm not sure how to do that without creating a BlobLeaseClientFactory and bypassing the call to GetBlobLeaseClient() altogether.

public async Task PerformActionAsync(BlobBaseClient client)
{
    var leaseClient = _leaseClientFactory.GetBlobLeaseClient(client);
    await leaseClient.AcquireAsync();
    try
    {
        await DoLeasedActionAsync();
    }
    finally
    {
        await leaseClient.ReleaseAsync();
    }
}

According to the SDK guideline found here the library should support mocking:
https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-mocking

It seems that the method GetBlobLeaseClient() does not follow that guideline.

@seanmcc-msft
Copy link
Member

@henriblMSFT, I think you're right, it looks like it's not possible to mock and extension method.

I believe we chose to use extension methods to keep the BlobLeaseClient hidden from the Azure.Storage.Blobs namespace, with the concern being that it may confuse less-sophisticated users.

I think the best path forward is the workaround you suggested, creating a BlobLeaseClientFactory.

@seanmcc-msft
Copy link
Member

I'm going to close this issue, please re-open if you have any other questions.

@henriblMSFT
Copy link
Author

This was addressed by #15105

Mock can now be created using the Moq.Protected namespace

var blobClient = new Mock<BlobClient>();
var blobLeaseClient = new Mock<BlobLeaseClient>();
blobClient.Protected().Setup<BlobLeaseClient>("GetBlobLeaseClientCore", ItExpr.IsAny<string>())
    .Returns<string>((leaseId) => _blobLeaseClient.Object);

@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

4 participants