public void RunAsync_IOException() { // ARRANGE byte[] expected = new byte[FactoryNode.Node.Size.Value]; new Random().NextBytes(expected); IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(); Stream s = new MemoryStream(); FileDownloadCallbackMock callback = new FileDownloadCallbackMock(); FileDownload f = new FileDownload(c, "id1", FactoryNode.Node, s); f.AddFileDownloadCallback(callback); Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(123)).OnAllThreads(); Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0)) .Returns(FactoryNode.ApiDownloadToken).OnAllThreads(); DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>(); Mock.Arrange(() => Mock.Create <DownloadProgressChangedEventArgs>().BytesReceived).IgnoreInstance().Returns(expected.Length); Mock.Arrange(() => c.Executor.ExecuteWebClientDownload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), RequestType.GetDownloadChunk, Arg.IsAny <Thread>(), Arg.AnyInt)) .Raises(() => wc.DownloadProgressChanged += null, null, Mock.Create <DownloadProgressChangedEventArgs>()).Returns(expected).OnAllThreads(); Mock.Arrange(() => s.Write(Arg.IsAny <byte[]>(), Arg.AnyInt, Arg.AnyInt)).Throws(new IOException()).OnAllThreads(); Mock.Arrange(() => c.Builder.ProvideChunkDownloadWebClient(Arg.AnyLong, Arg.AnyLong)).Returns(wc).OnAllThreads(); Mock.Arrange(() => callback.OnFailed(Arg.AnyString, Arg.IsAny <DracoonException>())).Occurs(1); // ACT f.RunAsync(); while (f.RunningThread.IsAlive) { } s.Close(); // ASSERT Mock.Assert(callback); }
public void RunSync_Fail() { // ARRANGE byte[] expected = new byte[FactoryNode.Node.Size.Value]; new Random().NextBytes(expected); IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(); Stream s = new MemoryStream(); FileDownloadCallbackMock callback = new FileDownloadCallbackMock(); FileDownload f = new FileDownload(c, "id1", FactoryNode.Node, s); f.AddFileDownloadCallback(callback); Mock.NonPublic.Arrange(f, "StartDownload").Throws(new DracoonException()); Mock.Arrange(() => callback.OnFailed(Arg.AnyString, Arg.IsAny <DracoonException>())).Occurs(1); // ACT - ASSERT Assert.Throws <DracoonException>(() => f.RunSync()); Mock.Assert(f); Mock.Assert(callback); }
public void RunSync_Success() { // ARRANGE byte[] expected = new byte[FactoryNode.Node.Size.Value]; new Random().NextBytes(expected); IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(); Stream s = new MemoryStream(); FileDownloadCallbackMock callback = new FileDownloadCallbackMock(); FileDownload f = new FileDownload(c, "id1", FactoryNode.Node, s); f.AddFileDownloadCallback(callback); Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(2354)).Occurs(1); Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0)) .Returns(FactoryNode.ApiDownloadToken).Occurs(1); DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>(); Mock.Arrange(() => Mock.Create <DownloadProgressChangedEventArgs>().BytesReceived).IgnoreInstance().Returns(expected.Length); Mock.Arrange(() => c.Builder.ProvideChunkDownloadWebClient(Arg.AnyLong, Arg.AnyLong)).Returns(wc); Mock.Arrange(() => c.Executor.ExecuteWebClientDownload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), RequestType.GetDownloadChunk, Arg.IsAny <Thread>(), Arg.AnyInt)).DoInstead(() => Thread.Sleep(250)) .Raises(() => wc.DownloadProgressChanged += null, null, Mock.Create <DownloadProgressChangedEventArgs>()).Returns(expected); Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1); Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1); Mock.Arrange(() => callback.OnFinished(Arg.AnyString)).Occurs(1); // ACT f.RunSync(); s.Position = 0; byte[] actual = new byte[expected.Length]; s.Read(actual, 0, expected.Length); s.Close(); // ASSERT Assert.Equal(expected, actual); Mock.Assert(callback); Mock.Assert(c.Builder); Mock.Assert(c.Executor); }