protected async Task RunTestAsync(Func <CsvAsyncInputBase> sutCreator, string filePath, bool ignoreUTF8ByteOrderMark)
        {
            (byte[] fileData, int originalLength) = GetExpectedCsvData(filePath, ignoreUTF8ByteOrderMark);
            var expected = TokenizeCsvFileUsingCursively(fileData, fileData.Length, (byte)',');

            // run without progress
            {
                var sut          = sutCreator();
                var inputVisitor = new StringBufferingVisitor();

                await sut.ProcessAsync(inputVisitor).ConfigureAwait(false);

                Assert.Equal(expected, inputVisitor.Records);

                await Assert.ThrowsAsync <InvalidOperationException>(() => sut.ProcessAsync(null).AsTask());
            }

            // run with progress
            {
                var sut          = sutCreator();
                var inputVisitor = new StringBufferingVisitor();

                var readSoFar = new List <int>();
                var progress  = new ImmediateProgress <int>(readSoFar.Add);

                await sut.ProcessAsync(inputVisitor, progress).ConfigureAwait(false);

                Assert.Equal(expected, inputVisitor.Records);

                Assert.Equal(originalLength, readSoFar.Sum());
                Assert.Equal(0, readSoFar.Last());

                await Assert.ThrowsAsync <InvalidOperationException>(() => sut.ProcessAsync(null).AsTask());
            }
        }
예제 #2
0
        public void EachReportedProgressValueInvokesTheAction()
        {
            int             value    = 0;
            IProgress <int> progress = new ImmediateProgress <int>(v => value = v);

            Assert.Equal(0, value);
            progress.Report(240);
            Assert.Equal(240, value);
            progress.Report(-240);
            Assert.Equal(-240, value);
        }
예제 #3
0
        public async Task CallbacksAreInvokedSynchronously_NeitherThroughSynchronizationContextNorOnTheThreadPool()
        {
            Thread?thread = null;
            IProgress <object?> progress = new ImmediateProgress <object?>(value => thread = Thread.CurrentThread);

            Assert.Null(thread);
            progress.Report(null);
            Assert.NotNull(thread);
            Assert.Same(Thread.CurrentThread, thread);

            TaskCompletionSource <int> tcs = new();

            progress = new Progress <object?>(value => tcs.SetResult(Thread.CurrentThread.ManagedThreadId));

            progress.Report(null);
            int threadId = await tcs.Task.ConfigureAwait(false);

            Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, threadId);
        }