From fa6687c084d667911c4ed6aca27a59d9a04119cd Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sun, 30 Sep 2018 19:26:25 +0100 Subject: [PATCH] fixed test by adding semaphores. Also reduced collections name to avoid hitting the 127 byte limit regarding index names. --- ...PartitionedCollectionNameAttributeTests.cs | 2 +- ...PartitionedCollectionNameAttributeTests.cs | 2 +- .../Infrastructure/MongoDbDocumentTestBase.cs | 49 +++++++++++++------ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs b/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs index a6381a9..1e8d8dd 100644 --- a/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs +++ b/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs @@ -5,7 +5,7 @@ using System; namespace CoreIntegrationTests { - [CollectionName("CoreTestingCollectionNameAttributePartitionedTKey")] + [CollectionName("CoreTestingCNameAttrPart")] public class CorePartitionedCollectionNameDoc : TestDoc, IPartitionedDocument { public CorePartitionedCollectionNameDoc() diff --git a/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs b/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs index 71d982c..e9f3e29 100644 --- a/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs +++ b/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs @@ -5,7 +5,7 @@ using System; namespace CoreIntegrationTests { - [CollectionName("TestingCollectionNameAttributePartitionedTKey")] + [CollectionName("TestingCNameAttrPartTKey")] public class CoreTKeyPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument { public CoreTKeyPartitionedCollectionNameDoc() diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs index c1aa018..8577eff 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using Xunit; @@ -946,6 +947,10 @@ namespace CoreIntegrationTests.Infrastructure #region Index Management + + //Instantiate a Singleton of the Semaphore with a value of 1. This means that only 1 thread can be granted access at a time. + static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1); + [Fact] public async Task CreateTextIndexNoOptionAsync() { @@ -953,34 +958,50 @@ namespace CoreIntegrationTests.Infrastructure const string expectedIndexName = "SomeContent_text"; // Act - var result = await SUT.CreateTextIndexAsync(x => x.SomeContent, null, PartitionKey); + await textIndexSemaphore.WaitAsync(); + try + { + var result = await SUT.CreateTextIndexAsync(x => x.SomeContent, null, PartitionKey); - // Assert - var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); - Assert.Contains(expectedIndexName, listOfIndexNames); + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); - // Cleanup - await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + finally + { + textIndexSemaphore.Release(); + } } [Fact] public async Task CreateTextIndexWithOptionAsync() { // Arrange - string expectedIndexName = $"SomeContent_text_{Guid.NewGuid()}"; + string expectedIndexName = $"{Guid.NewGuid()}"; var option = new IndexCreationOptions { Name = expectedIndexName }; - // Act - var result = await SUT.CreateTextIndexAsync(x => x.SomeContent, option, PartitionKey); + await textIndexSemaphore.WaitAsync(); + try + { + // Act + var result = await SUT.CreateTextIndexAsync(x => x.AddedAtUtc, option, PartitionKey); - // Assert - var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); - Assert.Contains(expectedIndexName, listOfIndexNames); + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); - // Cleanup - await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + finally + { + textIndexSemaphore.Release(); + } } #endregion Index Management