Esempio n. 1
0
        protected async Task DoWorkAsync(IQueueEntry <SimpleWorkItem> w, AsyncCountdownEvent countdown, WorkInfo info)
        {
            _logger.Trace($"Starting: {w.Value.Id}");
            Assert.Equal("Hello", w.Value.Data);

            try {
                // randomly complete, abandon or blowup.
                if (RandomData.GetBool())
                {
                    _logger.Trace($"Completing: {w.Value.Id}");
                    await w.CompleteAsync();

                    info.IncrementCompletedCount();
                }
                else if (RandomData.GetBool())
                {
                    _logger.Trace($"Abandoning: {w.Value.Id}");
                    await w.AbandonAsync();

                    info.IncrementAbandonCount();
                }
                else
                {
                    _logger.Trace($"Erroring: {w.Value.Id}");
                    info.IncrementErrorCount();
                    throw new Exception();
                }
            } finally {
                _logger.Trace($"Signal {countdown.CurrentCount}");
                countdown.Signal();
            }
        }
Esempio n. 2
0
        protected void DoWork(QueueEntry <SimpleWorkItem> w, CountdownEvent latch, WorkInfo info)
        {
            Trace.WriteLine(String.Format("Starting: {0}", w.Value.Id));
            Assert.Equal("Hello", w.Value.Data);

            try {
                // randomly complete, abandon or blowup.
                if (RandomData.GetBool())
                {
                    Trace.WriteLine(String.Format("Completing: {0}", w.Value.Id));
                    w.Complete();
                    info.IncrementCompletedCount();
                }
                else if (RandomData.GetBool())
                {
                    Trace.WriteLine(String.Format("Abandoning: {0}", w.Value.Id));
                    w.Abandon();
                    info.IncrementAbandonCount();
                }
                else
                {
                    Trace.WriteLine(String.Format("Erroring: {0}", w.Value.Id));
                    info.IncrementErrorCount();
                    throw new ApplicationException();
                }
            } finally {
                latch.Signal();
            }
        }
Esempio n. 3
0
        protected async Task CanHaveMultipleQueueInstancesWithLockingImplAsync(CacheLockProvider distributedLock)
        {
            var queue = GetQueue(retries: 0, retryDelay: TimeSpan.Zero);

            if (queue == null)
            {
                return;
            }

            try {
                await queue.DeleteQueueAsync();
                await AssertEmptyQueueAsync(queue);

                const int workItemCount = 16;
                const int workerCount   = 4;
                var       countdown     = new AsyncCountdownEvent(workItemCount);
                var       info          = new WorkInfo();
                var       workers       = new List <IQueue <SimpleWorkItem> > {
                    queue
                };

                try {
                    for (int i = 0; i < workerCount; i++)
                    {
                        var q             = GetQueue(retries: 0, retryDelay: TimeSpan.Zero);
                        int instanceCount = i;
                        await q.StartWorkingAsync(async w => {
                            _logger.Info("[{0}] Acquiring distributed lock in work item: {1}", instanceCount, w.Id);
                            var l = await distributedLock.AcquireAsync("test");
                            Assert.NotNull(l);
                            _logger.Info("[{0}] Acquired distributed lock: {1}", instanceCount, w.Id);
                            SystemClock.Sleep(TimeSpan.FromMilliseconds(50));
                            await l.ReleaseAsync();
                            _logger.Info("[{0}] Released distributed lock: {1}", instanceCount, w.Id);

                            await w.CompleteAsync();
                            info.IncrementCompletedCount();
                            countdown.Signal();
                            _logger.Info("[{0}] Signaled countdown: {1}", instanceCount, w.Id);
                        });

                        workers.Add(q);
                    }

                    await Run.InParallelAsync(workItemCount, async i => {
                        string id = await queue.EnqueueAsync(new SimpleWorkItem {
                            Data = "Hello",
                            Id   = i
                        });
                        _logger.Trace("Enqueued Index: {0} Id: {1}", i, id);
                    });

                    await countdown.WaitAsync(TimeSpan.FromSeconds(5).ToCancellationToken());

                    await SystemClock.SleepAsync(50);

                    _logger.Trace("Completed: {0} Abandoned: {1} Error: {2}", info.CompletedCount, info.AbandonCount, info.ErrorCount);

                    _logger.Info("Work Info Stats: Completed: {completed} Abandoned: {abandoned} Error: {errors}", info.CompletedCount, info.AbandonCount, info.ErrorCount);
                    Assert.Equal(workItemCount, info.CompletedCount + info.AbandonCount + info.ErrorCount);

                    // In memory queue doesn't share state.
                    if (queue.GetType() == typeof(InMemoryQueue <SimpleWorkItem>))
                    {
                        var stats = await queue.GetQueueStatsAsync();

                        Assert.Equal(info.CompletedCount, stats.Completed);
                    }
                    else
                    {
                        var workerStats = new List <QueueStats>();
                        for (int i = 0; i < workers.Count; i++)
                        {
                            var stats = await workers[i].GetQueueStatsAsync();
                            _logger.Info("Worker#{i} Working: {working} Completed: {completed} Abandoned: {abandoned} Error: {errors} Deadletter: {deadletter}", i, stats.Working, stats.Completed, stats.Abandoned, stats.Errors, stats.Deadletter);
                            workerStats.Add(stats);
                        }

                        Assert.Equal(info.CompletedCount, workerStats.Sum(s => s.Completed));
                    }
                } finally {
                    foreach (var q in workers)
                    {
                        await CleanupQueueAsync(q);
                    }
                }
            } finally {
                await CleanupQueueAsync(queue);
            }
        }
Esempio n. 4
0
        protected void DoWork(QueueEntry<SimpleWorkItem> w, CountdownEvent latch, WorkInfo info)
        {
            Trace.WriteLine($"Starting: {w.Value.Id}");
            Assert.Equal("Hello", w.Value.Data);

            try {
                // randomly complete, abandon or blowup.
                if (RandomData.GetBool()) {
                    Trace.WriteLine($"Completing: {w.Value.Id}");
                    w.Complete();
                    info.IncrementCompletedCount();
                } else if (RandomData.GetBool()) {
                    Trace.WriteLine($"Abandoning: {w.Value.Id}");
                    w.Abandon();
                    info.IncrementAbandonCount();
                } else {
                    Trace.WriteLine($"Erroring: {w.Value.Id}");
                    info.IncrementErrorCount();
                    throw new ApplicationException();
                }
            } finally {

                Trace.WriteLine($"Signal {latch.CurrentCount}");
                latch.Signal();
            }
        }
Esempio n. 5
0
        protected async Task DoWorkAsync(QueueEntry<SimpleWorkItem> w, AsyncCountdownEvent countdown, WorkInfo info) {
            Trace.WriteLine($"Starting: {w.Value.Id}");
            Assert.Equal("Hello", w.Value.Data);

            try {
                // randomly complete, abandon or blowup.
                if (RandomData.GetBool()) {
                    Trace.WriteLine($"Completing: {w.Value.Id}");
                    await w.CompleteAsync();
                    info.IncrementCompletedCount();
                } else if (RandomData.GetBool()) {
                    Trace.WriteLine($"Abandoning: {w.Value.Id}");
                    await w.AbandonAsync();
                    info.IncrementAbandonCount();
                } else {
                    Trace.WriteLine($"Erroring: {w.Value.Id}");
                    info.IncrementErrorCount();
                    throw new ApplicationException();
                }
            } finally {
                Trace.WriteLine($"Signal {countdown.CurrentCount}");
                countdown.Signal();
            }
        }