2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.IO;
|
2021-03-19 18:55:07 +09:00
|
|
|
using System.Linq;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public class Utils
|
|
|
|
{
|
2020-01-15 00:22:31 +09:00
|
|
|
public static int _nextPort = 8001;
|
|
|
|
public static object _portLock = new object();
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public static int FreeTcpPort()
|
|
|
|
{
|
2020-01-15 00:22:31 +09:00
|
|
|
lock (_portLock)
|
|
|
|
{
|
2022-01-14 17:50:29 +09:00
|
|
|
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
while (true)
|
2020-01-15 00:22:31 +09:00
|
|
|
{
|
2022-01-14 17:50:29 +09:00
|
|
|
try
|
2020-01-15 00:22:31 +09:00
|
|
|
{
|
2022-01-14 17:50:29 +09:00
|
|
|
var port = _nextPort++;
|
|
|
|
socket.Bind(new IPEndPoint(IPAddress.Loopback, port));
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
catch (SocketException)
|
|
|
|
{
|
|
|
|
// Retry unless exhausted
|
|
|
|
if (_nextPort == 65536)
|
2020-01-15 00:22:31 +09:00
|
|
|
{
|
2022-01-14 17:50:29 +09:00
|
|
|
throw;
|
2020-01-15 00:22:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
// http://stackoverflow.com/a/14933880/2061103
|
|
|
|
public static void DeleteDirectory(string destinationDir)
|
|
|
|
{
|
|
|
|
const int magicDust = 10;
|
|
|
|
for (var gnomes = 1; gnomes <= magicDust; gnomes++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Directory.Delete(destinationDir, true);
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
return; // good!
|
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
|
|
|
if (gnomes == magicDust)
|
|
|
|
throw;
|
|
|
|
// System.IO.IOException: The directory is not empty
|
|
|
|
System.Diagnostics.Debug.WriteLine("Gnomes prevent deletion of {0}! Applying magic dust, attempt #{1}.", destinationDir, gnomes);
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
// see http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true for more magic
|
|
|
|
Thread.Sleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
if (gnomes == magicDust)
|
|
|
|
throw;
|
|
|
|
// Wait, maybe another software make us authorized a little later
|
|
|
|
System.Diagnostics.Debug.WriteLine("Gnomes prevent deletion of {0}! Applying magic dust, attempt #{1}.", destinationDir, gnomes);
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
// see http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true for more magic
|
|
|
|
Thread.Sleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// depending on your use case, consider throwing an exception here
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
}
|