public async Task WhenWorkIsAddedAgainWhenPreviousIsAnalysing_ThenDontWaitAnotherOneToGetReady()
        {
            var now           = DateTime.UtcNow;
            var loggerFactory = new LoggerFactory();
            var queue         = new AnalyzerWorkQueue(loggerFactory, utcNow: () => now);
            var document      = CreateTestDocumentId();

            queue.PutWork(document);

            now = PassOverThrotlingPeriod(now);

            var work        = queue.TakeWork();
            var waitingCall = Task.Run(async() => await queue.WaitForResultsAsync(work));
            await Task.Delay(50);

            // User updates code -> document is queued again during period when theres already api call waiting
            // to continue.
            queue.PutWork(document);

            // First iteration of work is done.
            queue.MarkWorkAsCompleteForDocumentId(document);

            // Waiting call continues because it's iteration of work is done, even when theres next
            // already waiting.
            await waitingCall;

            Assert.True(waitingCall.IsCompleted);
            Assert.Empty(loggerFactory.Logger.RecordedMessages);
        }
        public async Task WhenMultipleThreadsAreConsumingAnalyzerWorkerQueueItWorksAsExpected()
        {
            var now = DateTime.UtcNow;

            var queue = new AnalyzerWorkQueue(new LoggerFactory(), utcNow: () => now, timeoutForPendingWorkMs: 1000);

            var parallelQueues =
                Enumerable.Range(0, 10)
                .Select(_ =>
                        Task.Run(() => {
                var document = CreateTestDocumentId();

                queue.PutWork(document);

                now      = PassOverThrotlingPeriod(now);
                var work = queue.TakeWork();

                var pendingTask = queue.WaitForResultsAsync(work);

                foreach (var workDoc in work)
                {
                    queue.MarkWorkAsCompleteForDocumentId(workDoc);
                }

                pendingTask.Wait(TimeSpan.FromMilliseconds(300));
            }))
                .ToArray();

            await Task.WhenAll(parallelQueues);

            Assert.Empty(queue.TakeWork());
        }
        public void WhenWorkIsWaitedButTimeoutForWaitIsExceededAllowContinue()
        {
            var now           = DateTime.UtcNow;
            var loggerFactory = new LoggerFactory();
            var queue         = new AnalyzerWorkQueue(loggerFactory, utcNow: () => now, timeoutForPendingWorkMs: 20);
            var document      = CreateTestDocumentId();

            queue.PutWork(document);

            now = PassOverThrotlingPeriod(now);
            var work = queue.TakeWork();

            var pendingTask = queue.WaitForResultsAsync(work);
            var cts         = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(5));
            pendingTask.Wait(cts.Token);

            Assert.True(pendingTask.IsCompleted);
            Assert.Contains("Timeout before work got ready", loggerFactory.Logger.RecordedMessages.Single());
        }
        public void WhenWorkIsUnderAnalysisOutFromQueueThenWaitUntilNextIterationOfItIsReady()
        {
            var now      = DateTime.UtcNow;
            var queue    = new AnalyzerWorkQueue(new LoggerFactory(), utcNow: () => now, timeoutForPendingWorkMs: 500);
            var document = CreateTestDocumentId();

            queue.PutWork(document);

            now = PassOverThrotlingPeriod(now);

            var work = queue.TakeWork();

            var pendingTask = queue.WaitForResultsAsync(work);

            pendingTask.Wait(TimeSpan.FromMilliseconds(50));

            Assert.False(pendingTask.IsCompleted);
            queue.MarkWorkAsCompleteForDocumentId(document);
            pendingTask.Wait(TimeSpan.FromMilliseconds(50));
            Assert.True(pendingTask.IsCompleted);
        }