/// <summary>
        /// Callback for async operations (search, download and install) to update the <see cref="CurrentProgress"/> property.
        /// </summary>
        /// <param name="job">The async operation which makes progress.</param>
        /// <param name="currentUpdate">The current update where the async operation is working on.</param>
        /// <param name="currentIndex">The index of the current update.</param>
        /// <param name="count">Total number of updates which the async operation has to proceed.</param>
        private void ProgressChangedCallback(WuStateAsyncJob job, IUpdate currentUpdate, int currentIndex, int count, int percent)
        {
            Debug.Assert(count >= 0 && currentIndex >= 0 && percent >= 0);
            Debug.Assert(percent <= 100);
            Debug.Assert(currentIndex < count);
            Debug.Assert(job != null && currentUpdate != null);

            try
            {
                var progress = new ProgressDescription((currentUpdate != null) ? UpdateHolder.ToUpdateDescription(currentUpdate) : null, currentIndex, count, percent);
                using (ll.Lock(StateLock))
                {
                    if (_currentState == job)
                    {
                        CurrentProgress = progress;
                        Log.Debug("Progress callback received: " + CurrentProgress);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message); // do not hide in test scenarios
                Log.Error($"Failed to create {nameof(ProgressDescription)}, progress will be ignored.", e);
            }
        }
Exemple #2
0
        public void Should_CallProgressCallback_When_OnProgressIsCalled()
        {
            ManualResetEvent callbackSignal = new ManualResetEvent(false);
            int             timeoutSec = 1;
            IUpdate         callbackUpdate = null, inputUpdate = new UpdateFake("id");
            int             callbackIndex = -1, callbackCount = -1, callbackPercent = -1, inputIndex = 0, inputCount = 1, inputPercent = 37;
            WuStateAsyncJob callbackJob = null;

            WuStateAsyncJob.ProgressChangedCallback callback = (sender, update, index, count, percent) =>
            {
                callbackJob     = sender;
                callbackUpdate  = update;
                callbackIndex   = index;
                callbackCount   = count;
                callbackPercent = percent;
                callbackSignal.Set();
            };

            var job = (new WuStateAsyncJobProxy(WuStateId.Downloading, "name", timeoutSec, (x, y) => { }, callback));

            job.EnterState(new WuStateReady());
            job.SimulateOnProgressNow(inputUpdate, inputIndex, inputCount, inputPercent);
            if (!callbackSignal.WaitOne(timeoutSec * 2000))
            {
                Assert.Fail($"progress callback was not called");
            }
            Assert.AreEqual(callbackIndex, inputIndex);
            Assert.AreEqual(callbackCount, inputCount);
            Assert.AreSame(callbackUpdate, inputUpdate);
            Assert.AreSame(callbackJob, job);

            job.Dispose();
        }
 /// <summary>
 /// Timeout callback for install operations.
 /// </summary>
 private void TimeoutInstallUpdates(WuStateAsyncJob job, int timeoutSec)
 {
     Log.Error($"Installation of updates aborted: timeout after {timeoutSec} sec.");
     EnterStateWhenAllowed(new WuStateInstallFailed(null, $"Timeout after {timeoutSec} sec."));
 }
 /// <summary>
 /// Timeout callback for download operations.
 /// </summary>
 private void TimeoutDownloadUpdates(WuStateAsyncJob job, int timeoutSec)
 {
     Log.Error($"Downloading updates aborted: timeout after {timeoutSec} sec.");
     EnterStateWhenAllowed(new WuStateDownloadFailed(null, $"Timeout after {timeoutSec} sec"));
 }
 /// <summary>
 /// Timeout callback for search operations.
 /// </summary>
 private void TimeoutSearchUpdates(WuStateAsyncJob job, int timeoutSec)
 {
     Log.Error($"Searching for updates aborted: timeout after {timeoutSec} sec.");
     EnterStateWhenAllowed(new WuStateSearchFailed(null, $"Timeout after {timeoutSec} sec."));
 }