public void ProgressProxy_WhereFuncReturnsResult_ReportsProgressOnCurrentThread() { const string activity = "ACTIVITY"; const string target = "TARGET"; const int percentComplete = 50; const int bytesTransferred = 512; const int bytesTotal = 1024; const int result = 42; var currentThreadId = Thread.CurrentThread.ManagedThreadId; var providerContextMock = new Mock <IProviderContext>(MockBehavior.Strict); providerContextMock.Setup(p => p.WriteProgress(It.Is <ProgressRecord>(r => r.Activity == activity && r.StatusDescription == target && r.PercentComplete == percentComplete && r.CurrentOperation == "Transferring 512b/1Kb" && r.RecordType == ProgressRecordType.Processing && Thread.CurrentThread.ManagedThreadId == currentThreadId) )); var sut = new ProgressProxy(providerContextMock.Object, activity, target); ProgressProxy.ProgressFunc <int> func = async p => { return(await Task.Run(() => { Assert.AreNotEqual(currentThreadId, Thread.CurrentThread.ManagedThreadId, "Current Thread not expected for func evaluation"); p.Report(new ProgressValue(percentComplete, bytesTransferred, bytesTotal)); return result; })); }; Assert.AreEqual(result, ProgressProxy.TraceProgressOn(func, sut), "Unexpected result"); providerContextMock.VerifyAll(); }
public FileInfoContract NewFileItem(DirectoryInfoContract parent, string name, Stream content, ProgressProxy progress) { try { if (content != null && !string.IsNullOrEmpty(encryptionKey)) { content = content.Encrypt(encryptionKey); } ProgressProxy.ProgressFunc <FileInfoContract> func = p => gateway.NewFileItemAsync(rootName, parent.Id, name, content, p); return(ProgressProxy.TraceProgressOn(func, progress)); } catch (AggregateException ex) when(ex.InnerExceptions.Count == 1) { throw ex.InnerExceptions[0]; } finally { InvalidateDrive(); } }
public void SetContent(FileInfoContract target, Stream content, ProgressProxy progress) { try { if (!string.IsNullOrEmpty(encryptionKey)) { content = content.Encrypt(encryptionKey); } Func <FileSystemInfoLocator> locator = () => new FileSystemInfoLocator(target); ProgressProxy.ProgressFunc <bool> func = p => gateway.SetContentAsync(rootName, target.Id, content, p, locator); ProgressProxy.TraceProgressOn(func, progress); target.Size = content.Length; } catch (AggregateException ex) when(ex.InnerExceptions.Count == 1) { throw ex.InnerExceptions[0]; } finally { InvalidateDrive(); } }