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); }
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 AzureQueueStorageMessage(CloudQueue queue, CloudQueueMessage message, TimeSpan maxMessageTime) { _message = message; _queue = queue; _strMessage = new Lazy <string>(() => _message.AsString); var waitFor = ((message.NextVisibleTime.HasValue ? DateTime.UtcNow.AddMinutes(1) : message.NextVisibleTime.Value.UtcDateTime) - DateTime.UtcNow).Subtract(TimeSpan.FromSeconds(10)); _lockWatcher = AsyncEnumerableEx.CreateTimer(waitFor) .Select(i => _queue.UpdateMessageAsync(_message, TimeSpan.FromMinutes(1), MessageUpdateFields.Visibility)) .Unwrap() .TakeUntilDisposed(maxMessageTime, t => _isDisposed = true); }
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); }
private async Task <Tuple <IDisposable, string> > LockInternal(ICloudBlob blob) { string leaseId; try { leaseId = await blob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null).ConfigureAwait(false); LeoTrace.WriteLine("Leased Blob: " + blob.Name); } catch (StorageException e) { // If we have a conflict this blob is already locked... if (e.RequestInformation.HttpStatusCode == 409) { return(null); } if (e.RequestInformation.HttpStatusCode == 404) { leaseId = null; } else { throw e.Wrap(blob.Name); } } // May not have had a blob pushed... if (leaseId == null) { try { using (var stream = new MemoryStream(new byte[1])) { try { await blob.UploadFromStreamAsync(stream).ConfigureAwait(false); } catch (StorageException) { } // Just eat storage exceptions at this point... something was created obviously } leaseId = await blob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null).ConfigureAwait(false); LeoTrace.WriteLine("Created new blob and lease (2 calls): " + blob.Name); } catch (StorageException e) { // If we have a conflict this blob is already locked... if (e.RequestInformation.HttpStatusCode == 409) { return(null); } if (e.RequestInformation.HttpStatusCode == 404) { return(null); } else { throw e.Wrap(blob.Name); } } } var condition = AccessCondition.GenerateLeaseCondition(leaseId); // Every 30 secs keep the lock renewed var keepAlive = AsyncEnumerableEx.CreateTimer(TimeSpan.FromSeconds(30)) .Select(t => { LeoTrace.WriteLine("Renewed Lease: " + blob.Name); return(blob.RenewLeaseAsync(condition)); }) .Unwrap() .TakeUntilDisposed(null, t => { try { // We need to do this to make sure after the dispose the lease is gone blob.ReleaseLeaseAsync(condition).GetAwaiter().GetResult(); } catch (Exception e) { LeoTrace.WriteLine("Release failed: " + e.Message); } }); return(Tuple.Create((IDisposable)keepAlive, leaseId)); }