Esempio n. 1
0
        public void Progress()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            progress?.Update(0.01f);
            Assert.AreEqual(0.01f, progress?.PortionComplete ?? float.NaN);
            Assert.AreEqual("", progress?.ItemCurrentlyBeingProcessed);
            progress?.Update(0.02f, "test");
            Assert.AreEqual(0.02f, progress?.PortionComplete ?? float.NaN);
            Assert.AreEqual("test", progress?.ItemCurrentlyBeingProcessed);
            progress?.Update(0.03f);
            Assert.AreEqual(0.03f, progress?.PortionComplete ?? float.NaN);
            Assert.AreEqual("test", progress?.ItemCurrentlyBeingProcessed);
            progress?.Update(0.04f, "");
            Assert.AreEqual(0.04f, progress?.PortionComplete ?? float.NaN);
            Assert.AreEqual("", progress?.ItemCurrentlyBeingProcessed);
            using (progress?.TrackPart(0.05f, 0.10f, null, true))
            {
                IAmbientProgress subprogress = AmbientProgressService.GlobalProgress;
                subprogress?.ResetCancellation(TimeSpan.FromMilliseconds(5));
            }
            using (progress?.TrackPart(0.10f, 0.15f, null, false))
            {
                IAmbientProgress subprogress = AmbientProgressService.GlobalProgress;
                subprogress?.ResetCancellation(TimeSpan.FromMilliseconds(5));
            }
        }
Esempio n. 2
0
        public void PortionCompleteTooHighError()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            progress?.Update(1.01f);
        }
Esempio n. 3
0
        public void ProgressThread()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            progress?.Update(0.25f, "main");
            Thread thread = new Thread(new ParameterizedThreadStart(o =>
            {
                // the progress here should be a SEPARATE progress because it's a separate execution thread
                IAmbientProgress threadProgress = AmbientProgressService.GlobalProgress;
                threadProgress?.Update(0.75f, "thread");
                threadProgress?.Update(0.33f, "cross-thread");
            }));

            thread.Start();
            thread.Join();
            Assert.AreEqual(0.33f, progress?.PortionComplete ?? 0);
        }
Esempio n. 4
0
        public void PartPortionCompleteTooLowError()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            using (progress?.TrackPart(0.01f, 0.02f))
            {
                IAmbientProgress subprogress = AmbientProgressService.GlobalProgress;
                subprogress?.Update(-.01f);
            }
        }
Esempio n. 5
0
    public Task Unzip()
    {
        IAmbientProgress? progress = AmbientProgressService.Progress;
        CancellationToken cancel   = progress?.CancellationToken ?? default(CancellationToken);

        ZipArchive archive = new ZipArchive(_package);
        int        entries = archive.Entries.Count;

        for (int entry = 0; entry < entries; ++entry)
        {
            ZipArchiveEntry archiveEntry = archive.Entries[entry];
            // update the progress
            progress?.Update(entry * 1.0f / entries, archiveEntry.FullName);
            archiveEntry.ExtractToFile(Path.Combine(_targetFolder, archiveEntry.FullName));
        }
        return(Task.CompletedTask);
    }
Esempio n. 6
0
        public void ProgressPart()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            using (progress?.TrackPart(0.05f, 0.05f, "prefix-"))
            {
                IAmbientProgress subprogress = AmbientProgressService.GlobalProgress;
                Assert.AreEqual(0.0f, subprogress?.PortionComplete);
                Assert.AreNotEqual(progress, AmbientProgressService.GlobalProgress);
                subprogress?.Update(.5f, "subitem");
                Assert.AreEqual(0.5f, subprogress?.PortionComplete);
                Assert.AreEqual("subitem", subprogress?.ItemCurrentlyBeingProcessed);
                Assert.AreEqual(0.075f, progress?.PortionComplete);
                Assert.AreEqual("prefix-subitem", progress?.ItemCurrentlyBeingProcessed);
            }
            Assert.AreEqual(progress, AmbientProgressService.GlobalProgress);
            Assert.AreEqual(0.10f, progress?.PortionComplete);
            Assert.AreEqual("prefix-subitem", progress?.ItemCurrentlyBeingProcessed);
        }
Esempio n. 7
0
    public async Task Download()
    {
        IAmbientProgress? progress = AmbientProgressService.Progress;
        CancellationToken cancel   = progress?.CancellationToken ?? default(CancellationToken);
        HttpWebRequest    request  = HttpWebRequest.CreateHttp(_downlaodUrl);

        using (WebResponse response = request.GetResponse())
        {
            long   totalBytesRead = 0;
            int    bytesRead;
            long   totalBytes = response.ContentLength;
            byte[] buffer     = new byte[8192];
            using (Stream downloadReader = response.GetResponseStream())
            {
                while ((bytesRead = await downloadReader.ReadAsync(buffer, 0, buffer.Length, cancel)) != 0)
                {
                    await _package.WriteAsync(buffer, 0, bytesRead, cancel);

                    totalBytesRead += bytesRead;
                    progress?.Update(totalBytesRead * 1.0f / totalBytes);
                }
            }
        }
    }
Esempio n. 8
0
        public void SubProgressCancellationTokenSource()
        {
            IAmbientProgress progress = AmbientProgressService.Progress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            using (progress?.TrackPart(0.0f, 1.0f))
            {
                IAmbientProgress subprogress = AmbientProgressService.Progress;
                using (AmbientClock.Pause())
                {
                    subprogress?.ResetCancellation(TimeSpan.FromMilliseconds(100));
                    AmbientCancellationTokenSource tokenSource = subprogress?.CancellationTokenSource;
                    Assert.IsNotNull(tokenSource);
                    Assert.IsFalse(tokenSource?.IsCancellationRequested ?? true);
                    Assert.AreEqual(tokenSource?.Token, subprogress?.CancellationToken);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));
                    Assert.IsTrue(tokenSource?.IsCancellationRequested ?? false);
                    Assert.AreEqual(tokenSource?.Token, subprogress?.CancellationToken);
                    Assert.IsTrue(subprogress?.CancellationToken.IsCancellationRequested ?? false);

                    subprogress?.ResetCancellation(null);
                    AmbientCancellationTokenSource newTokenSource = subprogress?.CancellationTokenSource;
                    Assert.IsNotNull(newTokenSource);
                    Assert.IsFalse(newTokenSource?.IsCancellationRequested ?? true);
                    Assert.AreEqual(newTokenSource?.Token, subprogress?.CancellationToken);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));
                    Assert.IsFalse(newTokenSource?.IsCancellationRequested ?? true);
                    Assert.IsTrue(newTokenSource?.Token.CanBeCanceled ?? false);
                    Assert.AreEqual(newTokenSource?.Token, subprogress?.CancellationToken);
                    Assert.IsFalse(subprogress?.CancellationToken.IsCancellationRequested ?? true);

                    subprogress?.Update(0.5f);
                    subprogress?.ThrowIfCancelled();
                }
            }
        }
Esempio n. 9
0
        public void CancellationTokenSource()
        {
            IAmbientProgress progress = AmbientProgressService.Progress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            using (AmbientClock.Pause())
            {
                progress?.ResetCancellation(TimeSpan.FromMilliseconds(102));
                using (AmbientCancellationTokenSource tokenSource = progress?.CancellationTokenSource)
                {
                    Assert.IsNotNull(tokenSource);
                    Assert.IsFalse(tokenSource?.IsCancellationRequested ?? true);
                    Assert.AreEqual(tokenSource?.Token, progress?.CancellationToken);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(111));
                    Assert.IsTrue(tokenSource?.IsCancellationRequested ?? false);
                    Assert.AreEqual(tokenSource?.Token, progress?.CancellationToken);
                    Assert.IsTrue(progress?.CancellationToken.IsCancellationRequested ?? false);

                    progress?.ResetCancellation();
                    AmbientCancellationTokenSource newTokenSource = progress?.CancellationTokenSource;
                    Assert.IsNotNull(newTokenSource);
                    Assert.IsFalse(newTokenSource?.IsCancellationRequested ?? true);
                    Assert.AreEqual(newTokenSource?.Token, progress?.CancellationToken);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));
                    Assert.IsFalse(newTokenSource?.IsCancellationRequested ?? true);
                    Assert.IsTrue(newTokenSource?.Token.CanBeCanceled ?? false);
                    Assert.AreEqual(newTokenSource?.Token, progress?.CancellationToken);
                    Assert.IsFalse(progress?.CancellationToken.IsCancellationRequested ?? true);

                    progress?.Update(0.5f);
                    progress?.ThrowIfCancelled();

                    using (CancellationTokenSource ts = new CancellationTokenSource())
                    {
                        progress?.ResetCancellation(ts);
                        newTokenSource = progress?.CancellationTokenSource;
                        Assert.IsNotNull(newTokenSource);
                        Assert.IsFalse(newTokenSource?.IsCancellationRequested ?? true);
                        Assert.AreEqual(newTokenSource?.Token, progress?.CancellationToken);
                        Assert.IsTrue(newTokenSource?.Token.CanBeCanceled ?? false);
                        ts.Cancel();
                        Assert.IsTrue(newTokenSource?.IsCancellationRequested ?? false);
                        Assert.AreEqual(newTokenSource?.Token, progress?.CancellationToken);
                        Assert.IsTrue(progress?.CancellationToken.IsCancellationRequested ?? false);
                    }
                    using (AmbientCancellationTokenSource ts = new AmbientCancellationTokenSource(null, null))
                    {
                        Assert.IsFalse(ts.IsCancellationRequested);
                        Assert.IsFalse(ts.Token.IsCancellationRequested);

                        ts.Dispose();

                        Assert.IsTrue(ts.IsCancellationRequested);
                        Assert.IsTrue(ts.Token.IsCancellationRequested);
                    }
                    using (AmbientCancellationTokenSource ts = new AmbientCancellationTokenSource(null, TimeSpan.FromMilliseconds(int.MaxValue)))
                    {
                        Assert.IsFalse(ts.IsCancellationRequested);     // theoretically, this could fail if this part of the test takes more than 30 days to execute
                        Assert.IsFalse(ts.Token.IsCancellationRequested);

                        ts.Dispose();

                        Assert.IsTrue(ts.IsCancellationRequested);
                        Assert.IsTrue(ts.Token.IsCancellationRequested);
                    }
                    AmbientCancellationTokenSource dispose;
                    using (AmbientCancellationTokenSource ts = new AmbientCancellationTokenSource())
                    {
                        dispose = ts;
                    }
                    dispose.CancelAfter(1);
                    System.Threading.Thread.Sleep(500);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(500));
                    dispose.Cancel(false);
                }
            }
        }