Skip to content

Commit

Permalink
Merge pull request #38 from FrendsPlatform/issue-37
Browse files Browse the repository at this point in the history
DownloadFiles - Added timeout setups for FtpClient
  • Loading branch information
Svenskapojkarna authored Jan 8, 2024
2 parents 08d99d6 + e0398a9 commit 090dee8
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 191 deletions.
4 changes: 4 additions & 0 deletions Frends.FTP.DownloadFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.1.1] - 2024-01-04
### Added
- Added setup for FtpClient.ReadTimeout, FtpClient.DataConnectionConnectTimeout and FtpClient.DataConnectionReadTimeout which were all defaulting to 15 seconds.

## [1.1.0] - 2023-09-12
### Added
- Added search for local certificates from machine certification store.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
using System.IO;
using System.Threading;
using Frends.FTP.DownloadFiles.Enums;
using Frends.FTP.DownloadFiles.TaskConfiguration;
using Frends.FTP.DownloadFiles.TaskResult;
using Frends.FTP.DownloadFiles.Tests.Lib;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests;

[TestFixture]
public class DestinationActionTests : DownloadFilesTestBase
{
[Test]
public void DestinationAction_Append_NoRenameInTransfer()
{
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt", "mycontent");

var result1 = CallDownloadFiles(
DestinationAction.Overwrite,
"file1.txt",
LocalDirFullPath,
false);
var result2 = CallDownloadFiles(
DestinationAction.Append,
"file1.txt",
LocalDirFullPath,
using System.IO;
using System.Threading;
using Frends.FTP.DownloadFiles.Enums;
using Frends.FTP.DownloadFiles.TaskConfiguration;
using Frends.FTP.DownloadFiles.TaskResult;
using Frends.FTP.DownloadFiles.Tests.Lib;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests;

[TestFixture]
public class DestinationActionTests : DownloadFilesTestBase
{
[Test]
public void DestinationAction_Append_NoRenameInTransfer()
{
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt", "mycontent");

var result1 = CallDownloadFiles(
DestinationAction.Overwrite,
"file1.txt",
LocalDirFullPath,
false);
var result2 = CallDownloadFiles(
DestinationAction.Append,
"file1.txt",
LocalDirFullPath,
false);

Assert.IsTrue(result1.Success, result1.UserResultMessage);
Assert.IsTrue(result2.Success, result2.UserResultMessage);
Assert.AreEqual(1, result2.SuccessfulTransferCount);
Assert.AreEqual("mycontentmycontent", File.ReadAllText($"{LocalDirFullPath}/file1.txt"));
Assert.IsTrue(result1.Success, result1.UserResultMessage);
Assert.IsTrue(result2.Success, result2.UserResultMessage);
Assert.AreEqual(1, result2.SuccessfulTransferCount);
Assert.AreEqual("mycontentmycontent", File.ReadAllText($"{LocalDirFullPath}/file1.txt"));
}

[Test]
public void DestinationAction_Append_WithRenameInTransfer()
{
[Test]
public void DestinationAction_Append_WithRenameInTransfer()
{
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt", "mycontent");

var result1 = CallDownloadFiles(
DestinationAction.Overwrite,
"file1.txt",
LocalDirFullPath,
true);
var result2 = CallDownloadFiles(
DestinationAction.Append,
"file1.txt",
LocalDirFullPath,
var result1 = CallDownloadFiles(
DestinationAction.Overwrite,
"file1.txt",
LocalDirFullPath,
true);
var result2 = CallDownloadFiles(
DestinationAction.Append,
"file1.txt",
LocalDirFullPath,
true);

Assert.IsTrue(result1.Success, result1.UserResultMessage);
Assert.IsTrue(result2.Success, result2.UserResultMessage);
Assert.AreEqual(1, result2.SuccessfulTransferCount);
Assert.AreEqual("mycontentmycontent", File.ReadAllText($"{LocalDirFullPath}/file1.txt"));
Assert.IsTrue(result1.Success, result1.UserResultMessage);
Assert.IsTrue(result2.Success, result2.UserResultMessage);
Assert.AreEqual(1, result2.SuccessfulTransferCount);
Assert.AreEqual("mycontentmycontent", File.ReadAllText($"{LocalDirFullPath}/file1.txt"));
}

private Result CallDownloadFiles(
DestinationAction destinationAction, string sourceFileName, string targetDir,
bool renameDestinationFilesDuringTransfer)
{
var source = new Source
{
Directory = FtpDir,
FileName = sourceFileName,
Operation = SourceOperation.Nothing
};

var destination = new Destination { Directory = targetDir, Action = destinationAction };
var options = new Options { CreateDestinationDirectories = true, RenameDestinationFileDuringTransfer = renameDestinationFilesDuringTransfer };
var connection = FtpHelper.GetFtpsConnection();

var result = FTP.DownloadFiles(source, destination, connection, options, new Info(), new CancellationToken());
return result;
}
private Result CallDownloadFiles(
DestinationAction destinationAction, string sourceFileName, string targetDir,
bool renameDestinationFilesDuringTransfer)
{
var source = new Source
{
Directory = FtpDir,
FileName = sourceFileName,
Operation = SourceOperation.Nothing
};

var destination = new Destination { Directory = targetDir, Action = destinationAction };
var options = new Options { CreateDestinationDirectories = true, RenameDestinationFileDuringTransfer = renameDestinationFilesDuringTransfer };
var connection = FtpHelper.GetFtpsConnection();

var result = FTP.DownloadFiles(source, destination, connection, options, new Info(), new CancellationToken());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public void DownloadFTP_DestinationFileExists_ExceptionThrown()

// Test and assert
var ex = Assert.Throws<Exception>(() => FTP.DownloadFiles(source, destination, connection, options, new Info(), new CancellationToken()));
Console.WriteLine(ex.Message);
Assert.IsTrue(ex.Message.Contains($"Error: Unable to transfer file. Destination file already exists: {destination.FileName}"));
}

Expand Down Expand Up @@ -218,5 +217,25 @@ public void DownloadFTPS_CurrentUserHasNoCertificates()
Assert.AreEqual(typeof(AuthenticationException), ex.InnerExceptions[0].GetType());

}

[Test]
public void DownloadFTP_LargeFiles()
{
FtpHelper.CreateLargeFileOnFTP(FtpDir, 5);
var source = new Source { Directory = FtpDir, FileName = "*.bin", Operation = SourceOperation.Delete };
var destination = new Destination { Directory = LocalDirFullPath, Action = DestinationAction.Overwrite };
var connection = new Connection
{
Address = FtpHelper.FtpHost,
UserName = FtpHelper.FtpUsername,
Password = FtpHelper.FtpPassword,
Port = FtpHelper.FtpPort
};

// Test and assert
var result = FTP.DownloadFiles(source, destination, connection, new Options(), new Info(), new CancellationToken());
Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(5, result.SuccessfulTransferCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
using System;
using System.IO;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests.Lib;

public class DownloadFilesTestBase
{
protected string LocalDirFullPath;
protected static FtpHelper FtpHelper;
protected string FtpDir;

[OneTimeSetUp]
public void OneTimeSetUp()
{
FtpHelper = new FtpHelper();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
FtpHelper.Dispose();
using System;
using System.IO;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests.Lib;

public class DownloadFilesTestBase
{
protected string LocalDirFullPath;
protected static FtpHelper FtpHelper;
protected string FtpDir;

[OneTimeSetUp]
public void OneTimeSetUp()
{
FtpHelper = new FtpHelper();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
FtpHelper.Dispose();
}

protected bool LocalFileExists(string fileName, string subDir = null)
{
var path = subDir == null ? LocalDirFullPath : Path.Combine(Path.GetTempPath(), subDir);
return File.Exists(Path.Combine(path, fileName));
}

protected static string CreateLocalDir(string name)
{
var dir = Path.Combine(Path.GetTempPath(), name);
Directory.CreateDirectory(dir);
return dir;
protected bool LocalFileExists(string fileName, string subDir = null)
{
var path = subDir == null ? LocalDirFullPath : Path.Combine(Path.GetTempPath(), subDir);
return File.Exists(Path.Combine(path, fileName));
}

[SetUp]
public void SetUp()
{
LocalDirFullPath = CreateLocalDir(Guid.NewGuid().ToString());
FtpDir = Guid.NewGuid().ToString();
FtpHelper.CreateDirectoryOnFTP(FtpDir);
}

[TearDown]
public void TearDown()
{
Directory.Delete(LocalDirFullPath, true);
FtpHelper.DeleteDirectoryOnFTP(FtpDir);
}
protected static string CreateLocalDir(string name)
{
var dir = Path.Combine(Path.GetTempPath(), name);
Directory.CreateDirectory(dir);
return dir;
}

[SetUp]
public void SetUp()
{
LocalDirFullPath = CreateLocalDir(Guid.NewGuid().ToString());
FtpDir = Guid.NewGuid().ToString();
FtpHelper.CreateDirectoryOnFTP(FtpDir);
}

[TearDown]
public void TearDown()
{
Directory.Delete(LocalDirFullPath, true);
FtpHelper.DeleteDirectoryOnFTP(FtpDir);
}
}
Loading

0 comments on commit 090dee8

Please sign in to comment.