Skip to content

Commit 28ac0da

Browse files
[Internal] Tests: Refactors GlobalDatabaseAccountTests to use async/await instead of sync-over-async (#5767)
Eliminates sync-over-async anti-patterns (.Wait(), .Result) in GlobalDatabaseAccountTests.cs by converting test methods to async Task and using await. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6bb0a5b commit 28ac0da

1 file changed

Lines changed: 56 additions & 56 deletions

File tree

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GlobalDatabaseAccountTests.cs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public class GlobalDatabaseAccountTests
3434
private const string GlobalDatabaseAccountName = "globaldb";
3535

3636
[TestInitialize]
37-
public void TestInitialize()
37+
public async Task TestInitialize()
3838
{
3939
DocumentClient client = TestCommon.CreateClient(false);
40-
TestCommon.DeleteAllDatabasesAsync().Wait();
40+
await TestCommon.DeleteAllDatabasesAsync();
4141

4242
this.writeRegionEndpointUri = new Uri(Utils.ConfigurationManager.AppSettings["GatewayEndpoint"]);
4343
this.masterKey = Utils.ConfigurationManager.AppSettings["MasterKey"];
@@ -456,9 +456,9 @@ await client.CreateDocumentCollectionAsync(
456456

457457
[TestMethod]
458458
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
459-
public void TestClientWithNoFailover()
459+
public async Task TestClientWithNoFailover()
460460
{
461-
TestClientWithNoFailoverAsync().Wait();
461+
await TestClientWithNoFailoverAsync();
462462
}
463463

464464
private async Task TestClientWithNoFailoverAsync()
@@ -514,9 +514,9 @@ private async Task TestClientWithNoFailoverAsync()
514514

515515
[TestMethod]
516516
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
517-
public void TestClientWithPreferredRegion()
517+
public async Task TestClientWithPreferredRegion()
518518
{
519-
TestClientWithPreferredRegionAsync().Wait();
519+
await TestClientWithPreferredRegionAsync();
520520
}
521521

522522
private async Task TestClientWithPreferredRegionAsync()
@@ -569,9 +569,9 @@ private async Task TestClientWithPreferredRegionAsync()
569569
}
570570

571571
[TestMethod]
572-
public void TestUpsertOperationWithPreferredRegion()
572+
public async Task TestUpsertOperationWithPreferredRegion()
573573
{
574-
TestUpsertOperationWithPreferredRegionAsync().Wait();
574+
await TestUpsertOperationWithPreferredRegionAsync();
575575
}
576576

577577
private async Task TestUpsertOperationWithPreferredRegionAsync()
@@ -617,9 +617,9 @@ private async Task TestUpsertOperationWithPreferredRegionAsync()
617617
}
618618

619619
[TestMethod]
620-
public void TestPreferredRegionOrder()
620+
public async Task TestPreferredRegionOrder()
621621
{
622-
TestPreferredRegionOrderAsync().Wait();
622+
await TestPreferredRegionOrderAsync();
623623
}
624624

625625
private async Task TestPreferredRegionOrderAsync()
@@ -649,7 +649,7 @@ private async Task TestPreferredRegionOrderAsync()
649649

650650
[TestMethod]
651651
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
652-
public void TestDocumentClientMemoryLeakDirectTCP()
652+
public async Task TestDocumentClientMemoryLeakDirectTCP()
653653
{
654654
ConnectionPolicy connectionPolicy = new ConnectionPolicy
655655
{
@@ -658,12 +658,12 @@ public void TestDocumentClientMemoryLeakDirectTCP()
658658
};
659659

660660

661-
this.TestDocumentClientMemoryLeakPrivate(connectionPolicy);
661+
await this.TestDocumentClientMemoryLeakPrivateAsync(connectionPolicy);
662662
}
663663

664664
[TestMethod]
665665
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
666-
public void TestDocumentClientMemoryLeakDirectHttps()
666+
public async Task TestDocumentClientMemoryLeakDirectHttps()
667667
{
668668
ConnectionPolicy connectionPolicy = new ConnectionPolicy
669669
{
@@ -672,12 +672,12 @@ public void TestDocumentClientMemoryLeakDirectHttps()
672672
};
673673

674674

675-
this.TestDocumentClientMemoryLeakPrivate(connectionPolicy);
675+
await this.TestDocumentClientMemoryLeakPrivateAsync(connectionPolicy);
676676
}
677677

678678
[TestMethod]
679679
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
680-
public void TestDocumentClientMemoryLeakGatewayHttps()
680+
public async Task TestDocumentClientMemoryLeakGatewayHttps()
681681
{
682682
ConnectionPolicy connectionPolicy = new ConnectionPolicy
683683
{
@@ -686,12 +686,12 @@ public void TestDocumentClientMemoryLeakGatewayHttps()
686686
};
687687

688688

689-
this.TestDocumentClientMemoryLeakPrivate(connectionPolicy);
689+
await this.TestDocumentClientMemoryLeakPrivateAsync(connectionPolicy);
690690
}
691691

692692

693693
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", Justification = "This is a test for checking memory leak fix which requires me to run GC.Collect")]
694-
private void TestDocumentClientMemoryLeakPrivate(ConnectionPolicy connectionPolicy)
694+
private async Task TestDocumentClientMemoryLeakPrivateAsync(ConnectionPolicy connectionPolicy)
695695
{
696696
Uri globalEndpointUri = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
697697
string authKey = ConfigurationManager.AppSettings["MasterKey"];
@@ -702,7 +702,7 @@ private void TestDocumentClientMemoryLeakPrivate(ConnectionPolicy connectionPoli
702702
WeakReference reference = new WeakReference(client, true);
703703

704704
// Executing any request using this client
705-
client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() }).Wait();
705+
await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() });
706706

707707
// Verify that the Write and Read Endpoints point to same endpoint(since no PreferredLocations was specified)
708708
Assert.AreEqual(client.WriteEndpoint, ConfigurationManager.AppSettings["GatewayEndpoint"]);
@@ -711,7 +711,7 @@ private void TestDocumentClientMemoryLeakPrivate(ConnectionPolicy connectionPoli
711711
// Adding a preferred read location, which should trigger the event handler to update the Read and Write endpoints
712712
connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location2"]);
713713

714-
client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() }).Wait();
714+
await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() });
715715

716716
// Verify that the read endpoint now changes to this new preferred location
717717
Assert.AreEqual(client.WriteEndpoint, ConfigurationManager.AppSettings["GatewayEndpoint"]);
@@ -732,9 +732,9 @@ private void TestDocumentClientMemoryLeakPrivate(ConnectionPolicy connectionPoli
732732

733733
[TestMethod]
734734
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
735-
public void TestDatabaseAccountRegionList()
735+
public async Task TestDatabaseAccountRegionList()
736736
{
737-
TestDatabaseAccountRegionListAsync().Wait();
737+
await TestDatabaseAccountRegionListAsync();
738738
}
739739

740740
private async Task TestDatabaseAccountRegionListAsync()
@@ -765,15 +765,15 @@ private async Task TestDatabaseAccountRegionListAsync()
765765
}
766766

767767
[TestMethod]
768-
public void TestMasterCRUD()
768+
public async Task TestMasterCRUD()
769769
{
770-
ValidateMasterCRUDAsync().Wait();
770+
await ValidateMasterCRUDAsync();
771771
}
772772

773773
[TestMethod]
774-
public void ValidatePartitionResourceCRUD()
774+
public async Task ValidatePartitionResourceCRUD()
775775
{
776-
this.ValidatePartitionResourceCRUDAsync().Wait();
776+
await this.ValidatePartitionResourceCRUDAsync();
777777
}
778778

779779
private async Task ValidatePartitionResourceCRUDAsync()
@@ -825,60 +825,60 @@ private async Task ValidatePartitionResourceCRUDAsync()
825825
}
826826

827827
[TestMethod]
828-
public void TestTopologyWriteStatus()
828+
public async Task TestTopologyWriteStatus()
829829
{
830-
this.ValidateWriteStatus().Wait();
830+
await this.ValidateWriteStatus();
831831
}
832832

833833
[TestMethod]
834834
[TestCategory("Quarantine") /* Used to filter out quarantined in gated runs */]
835-
public void TestFailoverAPIs()
835+
public async Task TestFailoverAPIs()
836836
{
837-
this.ValidateFailoverAPIs().Wait();
837+
await this.ValidateFailoverAPIs();
838838
}
839839

840840
[TestMethod]
841841
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
842-
public void TestGeoCollectionCRUD()
842+
public async Task TestGeoCollectionCRUD()
843843
{
844-
ValidateCollectionCRUDAsync().Wait();
844+
await ValidateCollectionCRUDAsync();
845845
}
846846

847847
[TestMethod]
848848
[TestCategory("Quarantine") /* Used to filter out quarantined tests in gated runs */]
849-
public void TestGeoPartitionedCollectionCRUD()
849+
public async Task TestGeoPartitionedCollectionCRUD()
850850
{
851-
ValidatePartitionedCollectionCRUDAsync().Wait();
851+
await ValidatePartitionedCollectionCRUDAsync();
852852
}
853853

854854
[TestMethod]
855-
public void TestFailoverWriteOperationRetryPolicy()
855+
public async Task TestFailoverWriteOperationRetryPolicy()
856856
{
857-
this.TestFailoverWriteOperationRetryPolicyAsync().Wait();
857+
await this.TestFailoverWriteOperationRetryPolicyAsync();
858858
}
859859

860860
[TestMethod]
861-
public void ValidateUpdateServiceManagerConfigOperation()
861+
public async Task ValidateUpdateServiceManagerConfigOperation()
862862
{
863-
this.ValidateUpdateServiceManagerConfigOperationAsync().Wait();
863+
await this.ValidateUpdateServiceManagerConfigOperationAsync();
864864
}
865865

866866
[TestMethod]
867-
public void ValidateCrossRegionCapacityAllocationWorkflow()
867+
public async Task ValidateCrossRegionCapacityAllocationWorkflow()
868868
{
869-
this.ValidateCrossRegionCapacityAllocationWorkflowAsync().Wait();
869+
await this.ValidateCrossRegionCapacityAllocationWorkflowAsync();
870870
}
871871

872872
[TestMethod]
873-
public void ReadDocumentFromReadRegionWithRetry()
873+
public async Task ReadDocumentFromReadRegionWithRetry()
874874
{
875-
ReadDocumentFromReadRegionWithRetryAsync().Wait();
875+
await ReadDocumentFromReadRegionWithRetryAsync();
876876
}
877877

878878
[TestMethod]
879-
public void ValidateGetDatabaseAccountFromGateway()
879+
public async Task ValidateGetDatabaseAccountFromGateway()
880880
{
881-
ValidateGetDatabaseAccountFromGatewayAsync().Wait();
881+
await ValidateGetDatabaseAccountFromGatewayAsync();
882882
}
883883

884884
private async Task TestFailoverWriteOperationRetryPolicyAsync()
@@ -980,8 +980,8 @@ private async Task ReadDocumentFromReadRegionWithRetryAsync()
980980
string collectionId = "GlobalDB_SessionRetry_Col1";
981981

982982
CosmosDatabaseSettings database =
983-
client.ReadDatabaseFeedAsync(new FeedOptions())
984-
.Result.FirstOrDefault(database1 => database1.Id.Equals(databaseId, StringComparison.InvariantCultureIgnoreCase));
983+
(await client.ReadDatabaseFeedAsync(new FeedOptions()))
984+
.FirstOrDefault(database1 => database1.Id.Equals(databaseId, StringComparison.InvariantCultureIgnoreCase));
985985

986986
if (database == null)
987987
{
@@ -991,8 +991,8 @@ private async Task ReadDocumentFromReadRegionWithRetryAsync()
991991
await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);
992992

993993
CosmosContainerSettings collection =
994-
client.ReadDocumentCollectionFeedAsync(database.SelfLink)
995-
.Result.FirstOrDefault(
994+
(await client.ReadDocumentCollectionFeedAsync(database.SelfLink))
995+
.FirstOrDefault(
996996
documentCollection => documentCollection.Id.Equals(collectionId, StringComparison.InvariantCultureIgnoreCase));
997997

998998
if (collection == null)
@@ -1152,7 +1152,7 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
11521152
"region2"
11531153
};
11541154

1155-
FederationEntity entityTemplate = storeProviderForEmulator.ListAsync<FederationEntity>(null).Result[0];
1155+
FederationEntity entityTemplate = (await storeProviderForEmulator.ListAsync<FederationEntity>(null))[0];
11561156
IList<DocumentServiceManagerStateEntity> dsmEntities = await storeProviderForEmulator.ListAsync<DocumentServiceManagerStateEntity>("emulatorfederation");
11571157

11581158
IFabricClient fabricClientFacade = new FabricClientFacade(new FabricClient(), this.GetType().ToString());
@@ -1165,7 +1165,7 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
11651165
fabricClientFacade);
11661166

11671167
TestServiceProvider serviceProvider = new TestServiceProvider(new TestAdminClientFactory());
1168-
ManagementUtil.Initialize(serviceProvider).Wait();
1168+
await ManagementUtil.Initialize(serviceProvider);
11691169
IStoreProvider storeProvider = serviceProvider.GetService<IStoreProvider>();
11701170

11711171
const int numFederations = 2;
@@ -1201,7 +1201,7 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
12011201

12021202
Logger.LogLine("ServiceConfigUri : {0}", configUri.AbsoluteUri);
12031203

1204-
Shared.BackoffRetryUtility<bool>.ExecuteAsync(async () =>
1204+
await Shared.BackoffRetryUtility<bool>.ExecuteAsync(async () =>
12051205
{
12061206
try
12071207
{
@@ -1217,15 +1217,15 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
12171217
}
12181218

12191219
return true;
1220-
}, new FabricExponentialRetryPolicy()).Wait();
1220+
}, new FabricExponentialRetryPolicy());
12211221

12221222
List<ServicePoolLimits> servicePoolLimits =
12231223
new List<ServicePoolLimits>();
12241224
for (int servicePoolIndex = 0; servicePoolIndex < 2; servicePoolIndex++)
12251225
{
12261226
ServicePool servicePool = new ServicePool(true, federationName,
12271227
FabricServiceType.ServerService.ToString() + "/" + servicePoolIndex, fabricClientFacade, true);
1228-
servicePool.CreateAsync(default(CancellationToken)).Wait();
1228+
await servicePool.CreateAsync(default(CancellationToken));
12291229

12301230
servicePoolLimits.Add(new ServicePoolLimits()
12311231
{
@@ -1238,7 +1238,7 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
12381238
{
12391239
Uri serviceUri = new Uri(appName + "/svc" + servicePoolIndex.ToString() + "-" + serviceIndex.ToString());
12401240

1241-
Shared.BackoffRetryUtility<bool>.ExecuteAsync(async () =>
1241+
await Shared.BackoffRetryUtility<bool>.ExecuteAsync(async () =>
12421242
{
12431243
try
12441244
{
@@ -1254,7 +1254,7 @@ private async Task ValidateCrossRegionCapacityAllocationWorkflowAsync()
12541254
}
12551255

12561256
return true;
1257-
}, new FabricExponentialRetryPolicy()).Wait();
1257+
}, new FabricExponentialRetryPolicy());
12581258

12591259
FabricServiceConfiguration serviceConfig = new FabricServiceConfiguration()
12601260
{
@@ -2035,7 +2035,7 @@ private async Task ValidateFailoverAPIs()
20352035
{
20362036
// make sure we always restore the write status in write region.
20372037
originalTopology.SetMajorIncrementGlobalConfigNumber(topology.GlobalConfigurationNumber);
2038-
adminClientWriteRegion.GrantWriteStatusAsync(originalTopology, adminClientWriteRegion.MasterServiceIdentity).Wait();
2038+
await adminClientWriteRegion.GrantWriteStatusAsync(originalTopology, adminClientWriteRegion.MasterServiceIdentity);
20392039
}
20402040

20412041
bool bWriteStatusRevoked1 = await adminClientWriteRegion.GetIsWriteStatusRevokedAsync(adminClientWriteRegion.MasterServiceIdentity);
@@ -2066,7 +2066,7 @@ private async Task VerifyRevokedWriteStatus()
20662066

20672067
Assert.AreEqual(HttpStatusCode.Forbidden, responseMessage.StatusCode, @"Unexpected error code.");
20682068

2069-
if (!responseMessage.Content.ReadAsStringAsync().Result.Contains(
2069+
if (!(await responseMessage.Content.ReadAsStringAsync()).Contains(
20702070
@"The requested operation cannot be performed at this region"))
20712071
{
20722072
Assert.Fail(@"Unexpected error returned, when checking for revoked write status.");

0 commit comments

Comments
 (0)