public void GetOrAddUniqueValueKeyIsThreadSafe() { var count = 1000; var service = new InMemoryUniqueService(); var data = Enumerable.Range(0, count).Select(i => Tuple.Create($"val-{i:d4}", new TestId(Guid.NewGuid().ToString("N")))).ToArray(); var threadWork = new Func<int, Task<TestId[]>>(async (threadNr) => { var res = new TestId[count]; foreach (var i in Enumerable.Range(0, count).OrderBy(i => Guid.NewGuid())) { res[i] = await service.GetOrAddUniqueValueKey("single-group", data[i].Item1, data[i].Item2); //Console.WriteLine($"Key for value {data[i].Item1} is {res[i]} by thread {threadNr:d3}."); await Task.Delay(i % 10); } return res; }); var results = threadWork.ExecuteMultithread(); for (int threadIndex = 0; threadIndex < results.Length; threadIndex++) { var threadResults = results[threadIndex]; for (int result = 0; result < count; result++) { var expected = data[result].Item2; var actual = threadResults[result]; Assert.AreEqual(expected, actual); } } }
public void GetOrAddUniqueValueKeyIsThreadSafe() { var count = 1000; var service = new InMemoryUniqueService(); var data = Enumerable.Range(0, count).Select(i => Tuple.Create($"val-{i:d4}", new TestId(Guid.NewGuid().ToString("N")))).ToArray(); var threadWork = new Func <int, Task <TestId[]> >(async(threadNr) => { var res = new TestId[count]; foreach (var i in Enumerable.Range(0, count).OrderBy(i => Guid.NewGuid())) { res[i] = await service.GetOrAddUniqueValueKey("single-group", data[i].Item1, data[i].Item2); //Console.WriteLine($"Key for value {data[i].Item1} is {res[i]} by thread {threadNr:d3}."); await Task.Delay(i % 10); } return(res); }); var results = threadWork.ExecuteMultithread(); for (int threadIndex = 0; threadIndex < results.Length; threadIndex++) { var threadResults = results[threadIndex]; for (int result = 0; result < count; result++) { var expected = data[result].Item2; var actual = threadResults[result]; Assert.AreEqual(expected, actual); } } }
public async Task UpdateUniqueValueKeyTest() { var tableName = RandomTableNameName(); Console.WriteLine($"Table name for this test: {tableName}"); var service = new TableStorageUniqueService(connString, tableName); service.Init(); const string ContractorVatNrInCountryForCompanyAccount = "ContractorVatNrInCountryForCompanyAccount"; var companyAccountId = Guid.NewGuid(); var countryCode = "PL"; var vatNumber = "8971649500"; var contractorId = new TestId(Guid.NewGuid()); var value = String.Format("{0}:{1}:{2}", companyAccountId.ToString("N"), countryCode, vatNumber); var id = await service.GetOrAddUniqueValueKey(ContractorVatNrInCountryForCompanyAccount, value, contractorId); if (!id.Equals(contractorId)) { Assert.Fail(); } var updatedValue = String.Format("{0}:{1}:{2}", companyAccountId.ToString("N"), countryCode, "8971649501"); var id2 = await service.UpdateUniqueValueKey(ContractorVatNrInCountryForCompanyAccount, value, updatedValue, contractorId, 5); if (!id2.Equals(contractorId)) { Assert.Fail(); } var contractorId2 = new TestId(Guid.NewGuid()); var task1 = service.UpdateUniqueValueKey(ContractorVatNrInCountryForCompanyAccount, updatedValue, value, contractorId, 5); var task2 = service.GetOrAddUniqueValueKey(ContractorVatNrInCountryForCompanyAccount, value, contractorId2); var t = await TaskEx.WhenAll(task1, task2); if (!t.Item1.Equals(contractorId)) { Assert.Fail(); } if (t.Item2.Equals(contractorId2)) { Assert.Fail(); } }