public static async Task <TEntity?> FindSingleOrDefaultAsync <TEntity, TKey>(this IConcurrentSqlStorage <TEntity, TKey> storage, Expression <Func <TEntity, bool> > criterion, CancellationToken cancellationToken = default) where TEntity : class, ISqlEntity <TKey> where TKey : notnull { var searchResult = await storage.FindAsync(criterion, 2, cancellationToken); if (searchResult.Length <= 1) { return(searchResult.FirstOrDefault()); } throw new InvalidOperationException($"Found more than one {typeof(TEntity).Name} trying to get single."); }
private static void InternalTestWriteAndReadThroughMultipleThreads(IReadOnlyCollection <TestValueTypedPropertiesStorageElement> objects, IConcurrentSqlStorage <TestValueTypedPropertiesStorageElement, Guid> storage) { Parallel.ForEach(objects.Batch(objects.Count / 10), batch => batch.ForEach(e => storage.CreateOrUpdateAsync(e).GetAwaiter().GetResult())); var objectsCaptured = objects; objects.Batch(objects.Count / 10) .AsParallel() .ForAll(batch => { var batchList = batch as IList <TestValueTypedPropertiesStorageElement> ?? batch.ToList(); batchList.ForEach(x => { var findResult = storage.FindAsync(y => y.IntProperty == x.IntProperty, int.MaxValue).GetAwaiter().GetResult(); AssertUnorderedArraysEquality(findResult, objectsCaptured.Where(y => y.IntProperty == x.IntProperty)); }); }); }