Skip to content

Instantly share code, notes, and snippets.

@smuda
Last active March 9, 2018 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smuda/3ddc8d8a7cf77b249626c7a272a42d71 to your computer and use it in GitHub Desktop.
Save smuda/3ddc8d8a7cf77b249626c7a272a42d71 to your computer and use it in GitHub Desktop.
Not Working coverage code
namespace SshSender
{
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using log4net;
using Renci.SshNet;
/// <summary>
/// The file sender.
/// </summary>
public class FileSender
{
/// <summary>
/// The username.
/// </summary>
private readonly string username;
/// <summary>
/// The password.
/// </summary>
private readonly string password;
/// <summary>
/// The hostname.
/// </summary>
private readonly string hostname;
/// <summary>
/// The port.
/// </summary>
private readonly int port;
/// <summary>
/// The log.
/// </summary>
private readonly ILog log;
/// <summary>
/// The files.
/// </summary>
private readonly ConcurrentQueue<FileToSend> files = new ConcurrentQueue<FileToSend>();
/// <summary>
/// Initializes a new instance of the <see cref="FileSender"/> class.
/// </summary>
/// <param name="username">
/// The username.
/// </param>
/// <param name="password">
/// The password.
/// </param>
/// <param name="hostname">
/// The hostname.
/// </param>
/// <param name="port">
/// The port.
/// </param>
internal FileSender(string username, string password, string hostname, int port)
{
this.log = LogManager.GetLogger(this.GetType());
this.username = username;
this.password = password;
this.hostname = hostname;
this.port = port;
}
/// <summary>
/// Gets a value indicating whether is sending.
/// </summary>
internal bool IsSending { get; private set; }
/// <summary>
/// Add a file to queue and start transmission.
/// </summary>
/// <param name="fileToSend">
/// The file to send.
/// </param>
internal void AddToQueue(FileToSend fileToSend)
{
this.files.Enqueue(fileToSend);
}
/// <summary>
/// The start sending.
/// </summary>
internal void StartSending()
{
if (this.IsSending)
{
return;
}
lock (this)
{
if (!this.files.Any())
{
return;
}
this.IsSending = true;
try
{
using (var sftpClient = new SftpClient(this.hostname, this.port, this.username, this.password))
{
sftpClient.Connect();
while (!this.files.IsEmpty)
{
FileToSend fileToSend;
if (this.files.IsEmpty || !this.files.TryDequeue(out fileToSend))
{
continue;
}
if (!File.Exists(fileToSend.SourceFilePath))
{
continue;
}
this.log.Info("Opening file for transmission: " + fileToSend.SourceFilePath);
try
{
using (
var filestream = new FileStream(
fileToSend.SourceFilePath,
FileMode.Open,
FileAccess.Read))
{
this.log.Info("Sending file to \"" + fileToSend.TargetFilePath + "\".");
sftpClient.UploadFile(filestream, fileToSend.TargetFilePath);
}
this.log.InfoFormat("File successfully sent. Moving file from \"{0}\" to \"{1}\".", fileToSend.SourceFilePath, fileToSend.SuccessFilePath);
FileExtensions.Move(fileToSend.SourceFilePath, fileToSend.SuccessFilePath);
}
catch (Exception exception)
{
this.log.Warn("Exception while sending file: " + exception);
}
}
}
}
catch (Exception exception)
{
this.log.Error("Failed to send files to server:" + exception);
}
finally
{
this.IsSending = false;
}
}
}
}
}
namespace Tests
{
using System;
using System.IO;
using System.Threading;
using SshSender;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests the <see cref="FileSender"/> class.
/// </summary>
[TestClass]
public class FileSenderTests : TestInfrastructure.WpvUnitTestBase
{
/// <summary>
/// The setup of tests.
/// </summary>
[TestInitialize]
public void Setup()
{
this.ClearDirectory(this.TempDir);
}
/// <summary>
/// Tests to send a file to a host.
/// </summary>
[TestMethod]
public void Test1()
{
var filename = Path.Combine(this.TempDir, "filename.txt");
var successfilename = Path.Combine(this.TempDir, "filenamesuccess.txt");
File.WriteAllText(filename, "This is the content of the file.");
const string Username = "john";
const string Password = "gurkan";
const string Hostname = "bidweb.pki.nu";
const int Port = 22;
var filesender = new FileSender(Username, Password, Hostname, Port);
filesender.AddToQueue(new FileToSend
{
SourceFilePath = filename,
TargetFilePath = Guid.NewGuid() + ".txt",
SuccessFilePath = successfilename
});
filesender.StartSending();
Thread.Sleep(100);
while (filesender.IsSending)
{
Thread.Sleep(50);
}
Assert.IsFalse(File.Exists(filename));
}
}
}
internal class FileToSend
{
public string SourceFilePath { get; set; }
public string TargetFilePath { get; set; }
public string SuccessFilePath { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment