Skip to content

Commit

Permalink
add await to async test asserts (#399)
Browse files Browse the repository at this point in the history
* add await to async test asserts

* add dispose flag

* remove flag

* revert local changes

* add failure case

---------

Co-authored-by: Stormbringer766 <storm7_662000@yahoo.com>
  • Loading branch information
stormbringer766 and dandrew-xx committed Feb 5, 2024
1 parent a4cead7 commit 1bb8db6
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Azure.CosmosRepositoryTests.Providers;
public class DefaultCosmosClientProviderTests
{
[Fact]
public void DefaultCosmosClientProviderCorrectlyDisposesOverloadWithConnectionString()
public async Task DefaultCosmosClientProviderCorrectlyDisposesOverloadWithConnectionString()
{
var mock = new Mock<ICosmosClientOptionsProvider>();
mock.SetupGet(provider => provider.ClientOptions).Returns(new CosmosClientOptions());
Expand All @@ -19,14 +19,25 @@ public void DefaultCosmosClientProviderCorrectlyDisposesOverloadWithConnectionSt
"AccountEndpoint=https://localtestcosmos.documents.azure.com:443/;AccountKey=RmFrZUtleQ==;"
}));

//Force lazy creation
_ = provider.CosmosClient;

provider.Dispose();

Assert.ThrowsAsync<ObjectDisposedException>(
async () => await provider.UseClientAsync(client => client.ReadAccountAsync()));
try
{
await provider.UseClientAsync(client => client.ReadAccountAsync());
Assert.Fail("Exception was not thrown");
}
catch (Exception ex)
{
//Actual exception is CosmosObjectDisposedException which is internal
Assert.IsType<ObjectDisposedException>(ex.GetBaseException());
}
}

[Fact]
public void DefaultCosmosClientProviderCorrectlyDisposesOverloadWithTokenCredential()
public async Task DefaultCosmosClientProviderCorrectlyDisposesOverloadWithTokenCredential()
{
var mock = new Mock<ICosmosClientOptionsProvider>();
mock.SetupGet(provider => provider.ClientOptions).Returns(new CosmosClientOptions());
Expand All @@ -41,7 +52,20 @@ public void DefaultCosmosClientProviderCorrectlyDisposesOverloadWithTokenCredent

provider.Dispose();

Assert.ThrowsAsync<ObjectDisposedException>(
async () => await provider.UseClientAsync(client => client.ReadAccountAsync()));
//Force lazy creation
_ = provider.CosmosClient;

provider.Dispose();

try
{
await provider.UseClientAsync(client => client.ReadAccountAsync());
Assert.Fail("Exception was not thrown");
}
catch (Exception ex)
{
//Actual exception is CosmosObjectDisposedException which is internal
Assert.IsType<ObjectDisposedException>(ex.GetBaseException());
}
}
}

0 comments on commit 1bb8db6

Please sign in to comment.