Skip to content

Commit

Permalink
[INTERNAL] EmulatorTests: Fixes tests to use DeleteStreamAsync to avo…
Browse files Browse the repository at this point in the history
…id throwing on test cleanup (#2709)

1. If the CreateContainerAsync fails the container delete operation throws an exception causing the test to hide the original exception.
2. Applied several Visual Studio suggestions about code formatting
  • Loading branch information
j82w authored Sep 8, 2021
1 parent 9d56895 commit c306993
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public async Task BatchCreateAndPatchAsync()

Assert.AreEqual(HttpStatusCode.Created, batchResponse[0].StatusCode);
Assert.AreEqual(HttpStatusCode.OK, batchResponse[1].StatusCode);
testDoc.Cost = testDoc.Cost + 1;
testDoc.Cost++;
await BatchTestBase.VerifyByReadAsync(BatchTestBase.JsonContainer, testDoc, isStream: false, isSchematized: false, useEpk:false);
}

Expand Down Expand Up @@ -804,7 +804,7 @@ private async Task<TransactionalBatchResponse> RunCrudAsync(bool isStream, bool
{
Assert.AreEqual(this.TestDocPk1ExistingC, batchResponse.GetOperationResultAtIndex<TestDoc>(1).Resource);
Assert.AreEqual(HttpStatusCode.OK, batchResponse[6].StatusCode);
testDocToPatch.Cost = testDocToPatch.Cost + 1;
testDocToPatch.Cost++;
await BatchTestBase.VerifyByReadAsync(container, testDocToPatch, isStream, isSchematized, useEpk);
}
else
Expand Down Expand Up @@ -834,7 +834,7 @@ public async Task BatchRateLimitingAsync()
BatchTestBase.Client.Endpoint.ToString(),
BatchTestBase.Client.AccountKey)
.WithThrottlingRetryOptions(
maxRetryWaitTimeOnThrottledRequests: default(TimeSpan),
maxRetryWaitTimeOnThrottledRequests: default,
maxRetryAttemptsOnThrottledRequests: 0)
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public class CosmosItemBulkTests
[TestInitialize]
public async Task TestInitialize()
{
CosmosClientOptions clientOptions = new CosmosClientOptions();
clientOptions.AllowBulkExecution = true;
CosmosClientOptions clientOptions = new CosmosClientOptions
{
AllowBulkExecution = true
};
CosmosClient client = TestCommon.CreateCosmosClient(clientOptions);

DatabaseResponse response = await client.CreateDatabaseIfNotExistsAsync(Guid.NewGuid().ToString());
Expand Down Expand Up @@ -536,8 +538,10 @@ private static ToDoActivity CreateItem(string id)

private static JObject CreateJObjectWithoutPK(string id)
{
JObject document = new JObject();
document["id"] = id;
JObject document = new JObject
{
["id"] = id
};
return document;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public class CosmosContainerTests
{
private CosmosClient cosmosClient = null;
private Cosmos.Database cosmosDatabase = null;
private static long ToEpoch(DateTime dateTime) => (long)(dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
private static long ToEpoch(DateTime dateTime)
{
return (long)(dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
}

[TestInitialize]
public async Task TestInit()
Expand Down Expand Up @@ -74,8 +77,10 @@ public async Task ReIndexingTest()
await container.ReplaceContainerAsync(existingContainerProperties);

// Check progress
ContainerRequestOptions requestOptions = new ContainerRequestOptions();
requestOptions.PopulateQuotaInfo = true;
ContainerRequestOptions requestOptions = new ContainerRequestOptions
{
PopulateQuotaInfo = true
};

while (true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ public async Task PartitionKeyDeleteTest()
};

ContainerInternal containerInternal = (ContainerInternal)this.Container;
ItemResponse<dynamic> itemResponse = await this.Container.CreateItemAsync<dynamic>(testItem1);
ItemResponse<dynamic> itemResponse2 = await this.Container.CreateItemAsync<dynamic>(testItem2);
ItemResponse<dynamic> itemResponse3 = await this.Container.CreateItemAsync<dynamic>(testItem3);
await this.Container.CreateItemAsync<dynamic>(testItem1);
await this.Container.CreateItemAsync<dynamic>(testItem2);
await this.Container.CreateItemAsync<dynamic>(testItem3);
Cosmos.PartitionKey partitionKey1 = new Cosmos.PartitionKey(pKString);
Cosmos.PartitionKey partitionKey2 = new Cosmos.PartitionKey(pKString2);
using (ResponseMessage pKDeleteResponse = await containerInternal.DeleteAllItemsByPartitionKeyStreamAsync(partitionKey1))
Expand Down Expand Up @@ -1392,7 +1392,6 @@ public async Task EpkPointReadTest()
[TestMethod]
public async Task ItemEpkQuerySingleKeyRangeValidation()
{
IList<ToDoActivity> deleteList = new List<ToDoActivity>();
ContainerInternal container = null;
try
{
Expand Down Expand Up @@ -1438,7 +1437,7 @@ public async Task ItemEpkQuerySingleKeyRangeValidation()
{
if (container != null)
{
await container.DeleteContainerAsync();
await container.DeleteContainerStreamAsync();
}
}
}
Expand Down Expand Up @@ -1562,7 +1561,7 @@ public async Task ValidateMaxItemCountOnItemQuery()
[TestMethod]
public async Task NegativeQueryTest()
{
IList<ToDoActivity> items = await ToDoActivity.CreateRandomItems(container: this.Container, pkCount: 10, perPKItemCount: 20, randomPartitionKey: true);
await ToDoActivity.CreateRandomItems(container: this.Container, pkCount: 10, perPKItemCount: 20, randomPartitionKey: true);

try
{
Expand Down Expand Up @@ -1690,9 +1689,11 @@ public async Task ItemPatchFailureTest()
ToDoActivity testItem = (await ToDoActivity.CreateRandomItems(this.Container, 1, randomPartitionKey: true)).First();
ContainerInternal containerInternal = (ContainerInternal)this.Container;

List<PatchOperation> patchOperations = new List<PatchOperation>();
patchOperations.Add(PatchOperation.Add("/nonExistentParent/Child", "bar"));
patchOperations.Add(PatchOperation.Remove("/cost"));
List<PatchOperation> patchOperations = new List<PatchOperation>
{
PatchOperation.Add("/nonExistentParent/Child", "bar"),
PatchOperation.Remove("/cost")
};

// item does not exist - 404 Resource Not Found error
try
Expand Down Expand Up @@ -2373,7 +2374,7 @@ public async Task ReadNonPartitionItemAsync()
{
if (fixedContainer != null)
{
await fixedContainer.DeleteContainerAsync();
await fixedContainer.DeleteContainerStreamAsync();
}
}
}
Expand Down Expand Up @@ -2461,7 +2462,7 @@ public async Task MigrateDataInNonPartitionContainer()
{
if (fixedContainer != null)
{
await fixedContainer.DeleteContainerAsync();
await fixedContainer.DeleteContainerStreamAsync();
}
}
}
Expand Down Expand Up @@ -2512,7 +2513,7 @@ public async Task VerifyToManyRequestTest(bool isQuery)
}
finally
{
await db.DeleteAsync();
await db.DeleteStreamAsync();
}
}

Expand Down Expand Up @@ -2562,7 +2563,7 @@ public async Task VerifySessionNotFoundStatistics()
}
finally
{
await database.Database.DeleteAsync();
await database.Database.DeleteStreamAsync();
}
}

Expand All @@ -2586,7 +2587,7 @@ private string GetDifferentLSNToken(string token, long lsnDifferent)
[DataTestMethod]
public async Task ContainterReCreateStatelessTest(bool operationBetweenRecreate, bool isQuery)
{
Func<Container, HttpStatusCode, Task> operation = null;
Func<Container, HttpStatusCode, Task> operation;
if (isQuery)
{
operation = ExecuteQueryAsync;
Expand Down Expand Up @@ -2636,7 +2637,7 @@ public async Task ContainterReCreateStatelessTest(bool operationBetweenRecreate,
}
finally
{
await db1.DeleteAsync();
await db1.DeleteStreamAsync();
cc1.Dispose();
cc2.Dispose();
}
Expand Down Expand Up @@ -2683,22 +2684,19 @@ public async Task CustomPropertiesItemRequestOptionsTest()
string customHeaderValue = "value1";

CosmosClient clientWithIntercepter = TestCommon.CreateCosmosClient(
builder =>
{
builder.WithTransportClientHandlerFactory(transportClient => new TransportClientHelper.TransportClientWrapper(
transportClient,
(uri, resourceOperation, request) =>
{
if (resourceOperation.resourceType == ResourceType.Document &&
resourceOperation.operationType == OperationType.Create)
{
bool customHeaderExists = request.Properties.TryGetValue(customHeaderName, out object value);
Assert.IsTrue(customHeaderExists);
Assert.AreEqual(customHeaderValue, value);
}
}));
});
builder => builder.WithTransportClientHandlerFactory(transportClient => new TransportClientHelper.TransportClientWrapper(
transportClient,
(uri, resourceOperation, request) =>
{
if (resourceOperation.resourceType == ResourceType.Document &&
resourceOperation.operationType == OperationType.Create)
{
bool customHeaderExists = request.Properties.TryGetValue(customHeaderName, out object value);
Assert.IsTrue(customHeaderExists);
Assert.AreEqual(customHeaderValue, value);
}
})));

Container container = clientWithIntercepter.GetContainer(this.database.Id, this.Container.Id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public async Task DatabaseAutoscaleIfExistsTest()
throughputResponse = await containerCore.ReplaceThroughputIfExistsAsync(
ThroughputProperties.CreateAutoscaleThroughput(6000),
requestOptions: null,
default(CancellationToken));
default);
Assert.IsNotNull(throughputResponse);
Assert.AreEqual(HttpStatusCode.NotFound, throughputResponse.StatusCode);
Assert.IsNull(throughputResponse.Resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public async Task ReadFeedIteratorCore_CrossPartitionBiDirectional(bool useState
}
finally
{
await container?.DeleteContainerAsync();
await container?.DeleteContainerStreamAsync();
}
}

Expand Down

0 comments on commit c306993

Please sign in to comment.