public void Write100000EntriesAndSearchAtSameTimeNoErrors() { var docs = CreateIpsumDocs(100000); string error = null; int numDocs = 0; using (var reading = AsyncEnumerableEx.CreateTimer(TimeSpan.FromSeconds(0.5)) .Select(t => _indexer.SearchDocuments(i => { var query = new TermQuery(new Term("words", "ipsum")); return(i.Search(query, 20)); })) .Select(d => numDocs += d.Count()) .TakeUntilDisposed(null, t => { if (t.IsFaulted) { error = t.Exception.GetBaseException().Message; } })) { _indexer.WriteToIndex(docs, true).Wait(); } Assert.AreEqual(null, error); }
private IAsyncEnumerable <ICloudBlob> ListBlobs(CloudBlobContainer container, string prefix, BlobListingDetails options) { // Clean up the prefix if required prefix = prefix == null ? null : SafePath.MakeSafeFilePath(prefix); return(AsyncEnumerableEx.Create <ICloudBlob>(async(y) => { BlobContinuationToken token = new BlobContinuationToken(); try { do { var segment = await container.ListBlobsSegmentedAsync(prefix, true, options, null, token, null, null, y.CancellationToken).ConfigureAwait(false); LeoTrace.WriteLine("Listed blob segment for prefix: " + prefix); foreach (var blob in segment.Results.OfType <ICloudBlob>()) { await y.YieldReturn(blob).ConfigureAwait(false); } token = segment.ContinuationToken; }while (token != null && !y.CancellationToken.IsCancellationRequested); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode != 404) { throw e.Wrap(container.Name + "_" + (prefix ?? string.Empty) + "*"); } } })); }
public async Task Throw1() { var ex = new Exception("Bang!"); var xs = AsyncEnumerableEx.Throw <int>(ex); var e = xs.GetAsyncEnumerator(); await AssertThrowsAsync(e.MoveNextAsync(), ex); Assert.False(await e.MoveNextAsync()); }
public async ValueTask <int> Linq_AsyncEnumerable_Value() { var sum = 0; await foreach (var item in asyncEnumerableValue.SelectMany(item => AsyncEnumerableEx.Return(item))) { sum += item; } return(sum); }
public void CanTimerWaitSuccessfullyCancels() { bool finished = false; var time = TimeSpan.FromSeconds(0.3); var enumerable = AsyncEnumerableEx.CreateTimer(time).TakeUntilDisposed(TimeSpan.FromSeconds(0.8), t => finished = true); Thread.Sleep(1000); Assert.AreEqual(true, finished); Assert.AreEqual(true, enumerable.RunningTask.IsCompleted); Assert.AreEqual(true, enumerable.RunningTask.IsCanceled); }
public void TimerFunctionWaits() { var stopwatch = Stopwatch.StartNew(); var time = TimeSpan.FromSeconds(0.3); var enumerable = AsyncEnumerableEx.CreateTimer(time).GetEnumerator(); enumerable.MoveNext().Wait(); enumerable.MoveNext().Wait(); enumerable.MoveNext().Wait(); stopwatch.Stop(); Assert.GreaterOrEqual(stopwatch.Elapsed, time + time + time); }
public IAsyncEnumerable <TElement> Deserialize <TElement>(object executionResult, IGremlinQueryEnvironment environment) { var result = _fragmentSerializer .TryDeserialize(executionResult, typeof(TElement[]), environment); return(result switch { TElement[] elements => elements.ToAsyncEnumerable(), IAsyncEnumerable <TElement> enumerable => enumerable, TElement element => AsyncEnumerableEx.Return(element), IEnumerable enumerable => enumerable.Cast <TElement>().ToAsyncEnumerable(), { } obj => throw new InvalidCastException($"A result of type {obj.GetType()} can't be interpreted as {nameof(IAsyncEnumerable<TElement>)}."), _ => AsyncEnumerable.Empty <TElement>() });
public IAsyncEnumerable <bool> RunEvery(StoreLocation location, TimeSpan interval, Action <Exception> unhandledExceptions = null) { return(AsyncEnumerableEx.Create <bool>(async(y) => { var blob = GetBlockBlob(location); var minimum = TimeSpan.FromSeconds(5); // so we're not polling the leased blob too fast while (!y.CancellationToken.IsCancellationRequested) { var timeLeft = TimeSpan.FromSeconds(0); // Don't allow you to throw to get out of the loop... try { var lastPerformed = DateTimeOffset.MinValue; var lease = await LockInternal(blob).ConfigureAwait(false); if (lease != null) { using (var arl = lease.Item1) { await blob.ExecuteWrap(b => b.FetchAttributesAsync(null, null, null, y.CancellationToken)).ConfigureAwait(false); if (blob.Metadata.ContainsKey("lastPerformed")) { DateTimeOffset.TryParseExact(blob.Metadata["lastPerformed"], "R", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out lastPerformed); } if (DateTimeOffset.UtcNow >= lastPerformed + interval) { await y.YieldReturn(true).ConfigureAwait(false); lastPerformed = DateTimeOffset.UtcNow; blob.Metadata["lastPerformed"] = lastPerformed.ToString("R", CultureInfo.InvariantCulture); await blob.ExecuteWrap(b => b.SetMetadataAsync(AccessCondition.GenerateLeaseCondition(lease.Item2), null, null, y.CancellationToken)).ConfigureAwait(false); } } } timeLeft = (lastPerformed + interval) - DateTimeOffset.UtcNow; } catch (TaskCanceledException) { throw; } catch (Exception e) { unhandledExceptions?.Invoke(e); LeoTrace.WriteLine("Error on lock loop: " + e.Message); } // Do this outside the exception to prevent it going out of control await Task.Delay(timeLeft > minimum ? timeLeft : minimum, y.CancellationToken).ConfigureAwait(false); } })); }