Fixed: Memory leak in Ensure.That

pull/3113/head
kayone 11 years ago
parent f1f13e6248
commit a7d5b3761b

@ -12,7 +12,7 @@ namespace NzbDrone.Api.ClientSchema
{ {
public static List<Field> ToSchema(object model) public static List<Field> ToSchema(object model)
{ {
Ensure.That(() => model).IsNotNull(); Ensure.That(model, () => model).IsNotNull();
var properties = model.GetType().GetSimpleProperties(); var properties = model.GetType().GetSimpleProperties();
@ -57,7 +57,7 @@ namespace NzbDrone.Api.ClientSchema
public static object ReadFormSchema(List<Field> fields, Type targetType) public static object ReadFormSchema(List<Field> fields, Type targetType)
{ {
Ensure.That(() => targetType).IsNotNull(); Ensure.That(targetType, () => targetType).IsNotNull();
var properties = targetType.GetSimpleProperties(); var properties = targetType.GetSimpleProperties();
@ -96,7 +96,7 @@ namespace NzbDrone.Api.ClientSchema
public static T ReadFormSchema<T>(List<Field> fields) public static T ReadFormSchema<T>(List<Field> fields)
{ {
return (T)ReadFormSchema(fields, typeof (T)); return (T)ReadFormSchema(fields, typeof(T));
} }
private static List<SelectOption> GetSelectOptions(Type selectOptions) private static List<SelectOption> GetSelectOptions(Type selectOptions)

@ -12,7 +12,7 @@ namespace NzbDrone.Common.Test.EnsureTest
public void EnsureWindowsPath(string path) public void EnsureWindowsPath(string path)
{ {
WindowsOnly(); WindowsOnly();
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
} }
@ -20,7 +20,7 @@ namespace NzbDrone.Common.Test.EnsureTest
public void EnsureLinuxPath(string path) public void EnsureLinuxPath(string path)
{ {
LinuxOnly(); LinuxOnly();
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
} }
} }
} }

@ -24,7 +24,7 @@ namespace NzbDrone.Common.Cache
public ICached<T> GetCache<T>(Type host) public ICached<T> GetCache<T>(Type host)
{ {
Ensure.That(() => host).IsNotNull(); Ensure.That(host, () => host).IsNotNull();
return GetCache<T>(host, host.FullName); return GetCache<T>(host, host.FullName);
} }
@ -37,8 +37,8 @@ namespace NzbDrone.Common.Cache
public ICached<T> GetCache<T>(Type host, string name) public ICached<T> GetCache<T>(Type host, string name)
{ {
Ensure.That(() => host).IsNotNull(); Ensure.That(host, () => host).IsNotNull();
Ensure.That(() => name).IsNotNullOrWhiteSpace(); Ensure.That(name, () => name).IsNotNullOrWhiteSpace();
return (ICached<T>)_cache.Get(host.FullName + "_" + name, () => new Cached<T>()); return (ICached<T>)_cache.Get(host.FullName + "_" + name, () => new Cached<T>());
} }

@ -38,7 +38,7 @@ namespace NzbDrone.Common.Cache
public void Set(string key, T value, TimeSpan? lifetime = null) public void Set(string key, T value, TimeSpan? lifetime = null)
{ {
Ensure.That(() => key).IsNotNullOrWhiteSpace(); Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
_store[key] = new CacheItem(value, lifetime); _store[key] = new CacheItem(value, lifetime);
} }
@ -69,7 +69,7 @@ namespace NzbDrone.Common.Cache
public T Get(string key, Func<T> function, TimeSpan? lifeTime = null) public T Get(string key, Func<T> function, TimeSpan? lifeTime = null)
{ {
Ensure.That(() => key).IsNotNullOrWhiteSpace(); Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
CacheItem cacheItem; CacheItem cacheItem;
T value; T value;

@ -67,7 +67,7 @@ namespace NzbDrone.Common
public DateTime GetLastFolderWrite(string path) public DateTime GetLastFolderWrite(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
if (!FolderExists(path)) if (!FolderExists(path))
{ {
@ -87,7 +87,7 @@ namespace NzbDrone.Common
public DateTime GetLastFileWrite(string path) public DateTime GetLastFileWrite(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
if (!FileExists(path)) if (!FileExists(path))
{ {
@ -107,13 +107,13 @@ namespace NzbDrone.Common
public bool FolderExists(string path) public bool FolderExists(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return Directory.Exists(path); return Directory.Exists(path);
} }
public bool FileExists(string path) public bool FileExists(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return File.Exists(path); return File.Exists(path);
} }
@ -129,28 +129,28 @@ namespace NzbDrone.Common
public string[] GetDirectories(string path) public string[] GetDirectories(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return Directory.GetDirectories(path); return Directory.GetDirectories(path);
} }
public string[] GetFiles(string path, SearchOption searchOption) public string[] GetFiles(string path, SearchOption searchOption)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return Directory.GetFiles(path, "*.*", searchOption); return Directory.GetFiles(path, "*.*", searchOption);
} }
public long GetFolderSize(string path) public long GetFolderSize(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length); return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length);
} }
public long GetFileSize(string path) public long GetFileSize(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
if (!FileExists(path)) if (!FileExists(path))
{ {
@ -163,22 +163,22 @@ namespace NzbDrone.Common
public void CreateFolder(string path) public void CreateFolder(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
} }
public void CopyFolder(string source, string destination) public void CopyFolder(string source, string destination)
{ {
Ensure.That(() => source).IsValidPath(); Ensure.That(source, () => source).IsValidPath();
Ensure.That(() => destination).IsValidPath(); Ensure.That(destination, () => destination).IsValidPath();
TransferFolder(source, destination, TransferAction.Copy); TransferFolder(source, destination, TransferAction.Copy);
} }
public void MoveFolder(string source, string destination) public void MoveFolder(string source, string destination)
{ {
Ensure.That(() => source).IsValidPath(); Ensure.That(source, () => source).IsValidPath();
Ensure.That(() => destination).IsValidPath(); Ensure.That(destination, () => destination).IsValidPath();
try try
{ {
@ -195,8 +195,8 @@ namespace NzbDrone.Common
private void TransferFolder(string source, string target, TransferAction transferAction) private void TransferFolder(string source, string target, TransferAction transferAction)
{ {
Ensure.That(() => source).IsValidPath(); Ensure.That(source, () => source).IsValidPath();
Ensure.That(() => target).IsValidPath(); Ensure.That(target, () => target).IsValidPath();
Logger.Trace("{0} {1} -> {2}", transferAction, source, target); Logger.Trace("{0} {1} -> {2}", transferAction, source, target);
@ -237,7 +237,7 @@ namespace NzbDrone.Common
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
Logger.Trace("Deleting file: {0}", path); Logger.Trace("Deleting file: {0}", path);
RemoveReadOnly(path); RemoveReadOnly(path);
@ -247,8 +247,8 @@ namespace NzbDrone.Common
public void MoveFile(string source, string destination) public void MoveFile(string source, string destination)
{ {
Ensure.That(() => source).IsValidPath(); Ensure.That(source, () => source).IsValidPath();
Ensure.That(() => destination).IsValidPath(); Ensure.That(destination, () => destination).IsValidPath();
if (source.PathEquals(destination)) if (source.PathEquals(destination))
{ {
@ -267,14 +267,14 @@ namespace NzbDrone.Common
public void DeleteFolder(string path, bool recursive) public void DeleteFolder(string path, bool recursive)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
Directory.Delete(path, recursive); Directory.Delete(path, recursive);
} }
public void InheritFolderPermissions(string filename) public void InheritFolderPermissions(string filename)
{ {
Ensure.That(() => filename).IsValidPath(); Ensure.That(filename, () => filename).IsValidPath();
try try
{ {
@ -293,7 +293,7 @@ namespace NzbDrone.Common
public long? GetAvailableSpace(string path) public long? GetAvailableSpace(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
var root = GetPathRoot(path); var root = GetPathRoot(path);
@ -319,28 +319,28 @@ namespace NzbDrone.Common
public string ReadAllText(string filePath) public string ReadAllText(string filePath)
{ {
Ensure.That(() => filePath).IsValidPath(); Ensure.That(filePath, () => filePath).IsValidPath();
return File.ReadAllText(filePath); return File.ReadAllText(filePath);
} }
public void WriteAllText(string filename, string contents) public void WriteAllText(string filename, string contents)
{ {
Ensure.That(() => filename).IsValidPath(); Ensure.That(filename, () => filename).IsValidPath();
RemoveReadOnly(filename); RemoveReadOnly(filename);
File.WriteAllText(filename, contents); File.WriteAllText(filename, contents);
} }
public void FileSetLastWriteTimeUtc(string path, DateTime dateTime) public void FileSetLastWriteTimeUtc(string path, DateTime dateTime)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
File.SetLastWriteTimeUtc(path, dateTime); File.SetLastWriteTimeUtc(path, dateTime);
} }
public void FolderSetLastWriteTimeUtc(string path, DateTime dateTime) public void FolderSetLastWriteTimeUtc(string path, DateTime dateTime)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
Directory.SetLastWriteTimeUtc(path, dateTime); Directory.SetLastWriteTimeUtc(path, dateTime);
} }
@ -362,14 +362,14 @@ namespace NzbDrone.Common
public string GetPathRoot(string path) public string GetPathRoot(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
return Path.GetPathRoot(path); return Path.GetPathRoot(path);
} }
public string GetParentFolder(string path) public string GetParentFolder(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
var parent = Directory.GetParent(path); var parent = Directory.GetParent(path);
@ -448,7 +448,7 @@ namespace NzbDrone.Common
public void EmptyFolder(string path) public void EmptyFolder(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
foreach (var file in GetFiles(path, SearchOption.TopDirectoryOnly)) foreach (var file in GetFiles(path, SearchOption.TopDirectoryOnly))
{ {
@ -468,7 +468,7 @@ namespace NzbDrone.Common
public long? GetTotalSize(string path) public long? GetTotalSize(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
var root = GetPathRoot(path); var root = GetPathRoot(path);

@ -10,13 +10,9 @@ namespace NzbDrone.Common.EnsureThat
return new Param<T>(name, value); return new Param<T>(name, value);
} }
public static Param<T> That<T>(Expression<Func<T>> expression) public static Param<T> That<T>(T value, Expression<Func<T>> expression)
{ {
var memberExpression = expression.GetRightMostMember(); return new Param<T>(expression.GetPath(), value);
return new Param<T>(
memberExpression.ToPath(),
expression.Compile().Invoke());
} }
public static TypeParam ThatTypeFor<T>(T value, string name = Param.DefaultName) public static TypeParam ThatTypeFor<T>(T value, string name = Param.DefaultName)

@ -1,4 +1,5 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using NzbDrone.Common.Cache;
namespace NzbDrone.Common.EnsureThat namespace NzbDrone.Common.EnsureThat
{ {
@ -15,30 +16,35 @@ namespace NzbDrone.Common.EnsureThat
return path + e.Member.Name; return path + e.Member.Name;
} }
internal static MemberExpression GetRightMostMember(this Expression e) internal static string GetPath(this Expression expression)
{ {
if (e is LambdaExpression) return GetRightMostMember(expression).ToPath();
return GetRightMostMember(((LambdaExpression)e).Body); }
private static MemberExpression GetRightMostMember(Expression e)
{
if (e is LambdaExpression)
return GetRightMostMember(((LambdaExpression)e).Body);
if (e is MemberExpression) if (e is MemberExpression)
return (MemberExpression)e; return (MemberExpression)e;
if (e is MethodCallExpression) if (e is MethodCallExpression)
{ {
var callExpression = (MethodCallExpression)e; var callExpression = (MethodCallExpression)e;
if (callExpression.Object is MethodCallExpression || callExpression.Object is MemberExpression) if (callExpression.Object is MethodCallExpression || callExpression.Object is MemberExpression)
return GetRightMostMember(callExpression.Object); return GetRightMostMember(callExpression.Object);
var member = callExpression.Arguments.Count > 0 ? callExpression.Arguments[0] : callExpression.Object; var member = callExpression.Arguments.Count > 0 ? callExpression.Arguments[0] : callExpression.Object;
return GetRightMostMember(member); return GetRightMostMember(member);
} }
if (e is UnaryExpression) if (e is UnaryExpression)
{ {
var unaryExpression = (UnaryExpression)e; var unaryExpression = (UnaryExpression)e;
return GetRightMostMember(unaryExpression.Operand); return GetRightMostMember(unaryExpression.Operand);
} }
return null; return null;
} }

@ -23,8 +23,8 @@ namespace NzbDrone.Common
public static string CleanFilePath(this string path) public static string CleanFilePath(this string path)
{ {
Ensure.That(() => path).IsNotNullOrWhiteSpace(); Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
Ensure.That(() => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();
var info = new FileInfo(path.Trim()); var info = new FileInfo(path.Trim());

@ -29,8 +29,8 @@ namespace NzbDrone.Core.Download
public void DownloadReport(RemoteEpisode remoteEpisode) public void DownloadReport(RemoteEpisode remoteEpisode)
{ {
Ensure.That(() => remoteEpisode.Series).IsNotNull(); Ensure.That(remoteEpisode.Series, () => remoteEpisode.Series).IsNotNull();
Ensure.That(() => remoteEpisode.Episodes).HasItems(); Ensure.That(remoteEpisode.Episodes, () => remoteEpisode.Episodes).HasItems();
var downloadTitle = remoteEpisode.Release.Title; var downloadTitle = remoteEpisode.Release.Title;
var downloadClient = _downloadClientProvider.GetDownloadClient(); var downloadClient = _downloadClientProvider.GetDownloadClient();

@ -25,7 +25,7 @@ namespace NzbDrone.Core.IndexerSearch.Definitions
private static string GetQueryTitle(string title) private static string GetQueryTitle(string title)
{ {
Ensure.That(() => title).IsNotNullOrWhiteSpace(); Ensure.That(title,() => title).IsNotNullOrWhiteSpace();
var cleanTitle = BeginningThe.Replace(title, String.Empty); var cleanTitle = BeginningThe.Replace(title, String.Empty);

@ -57,9 +57,9 @@ namespace NzbDrone.Core.MediaFiles
private void MoveFile(EpisodeFile episodeFile, Series series, string destinationFilename) private void MoveFile(EpisodeFile episodeFile, Series series, string destinationFilename)
{ {
Ensure.That(() => episodeFile).IsNotNull(); Ensure.That(episodeFile, () => episodeFile).IsNotNull();
Ensure.That(() => series).IsNotNull(); Ensure.That(series,() => series).IsNotNull();
Ensure.That(() => destinationFilename).IsValidPath(); Ensure.That(destinationFilename, () => destinationFilename).IsValidPath();
if (!_diskProvider.FileExists(episodeFile.Path)) if (!_diskProvider.FileExists(episodeFile.Path))
{ {

@ -33,7 +33,7 @@ namespace NzbDrone.Core.Messaging.Commands
public void PublishCommand<TCommand>(TCommand command) where TCommand : Command public void PublishCommand<TCommand>(TCommand command) where TCommand : Command
{ {
Ensure.That(() => command).IsNotNull(); Ensure.That(command, () => command).IsNotNull();
_logger.Trace("Publishing {0}", command.GetType().Name); _logger.Trace("Publishing {0}", command.GetType().Name);
@ -56,7 +56,7 @@ namespace NzbDrone.Core.Messaging.Commands
public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command
{ {
Ensure.That(() => command).IsNotNull(); Ensure.That(command, () => command).IsNotNull();
_logger.Trace("Publishing {0}", command.GetType().Name); _logger.Trace("Publishing {0}", command.GetType().Name);

@ -23,26 +23,26 @@ namespace NzbDrone.Core.Messaging.Events
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent
{ {
Ensure.That(() => @event).IsNotNull(); Ensure.That(@event, () => @event).IsNotNull();
var eventName = GetEventName(@event.GetType()); var eventName = GetEventName(@event.GetType());
/* /*
int workerThreads; int workerThreads;
int completionPortThreads; int completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
int maxCompletionPortThreads; int maxCompletionPortThreads;
int maxWorkerThreads; int maxWorkerThreads;
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
int minCompletionPortThreads; int minCompletionPortThreads;
int minWorkerThreads; int minWorkerThreads;
ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads); ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
_logger.Warn("Thread pool state WT:{0} PT:{1} MAXWT:{2} MAXPT:{3} MINWT:{4} MINPT:{5}", workerThreads, completionPortThreads, maxWorkerThreads, maxCompletionPortThreads, minWorkerThreads, minCompletionPortThreads); _logger.Warn("Thread pool state WT:{0} PT:{1} MAXWT:{2} MAXPT:{3} MINWT:{4} MINPT:{5}", workerThreads, completionPortThreads, maxWorkerThreads, maxCompletionPortThreads, minWorkerThreads, minCompletionPortThreads);
*/ */
_logger.Trace("Publishing {0}", eventName); _logger.Trace("Publishing {0}", eventName);

@ -13,15 +13,15 @@ namespace NzbDrone.Core.Rest
public static IRestResponse ValidateResponse(this IRestResponse response, IRestClient restClient) public static IRestResponse ValidateResponse(this IRestResponse response, IRestClient restClient)
{ {
Ensure.That(() => response).IsNotNull(); Ensure.That(response, () => response).IsNotNull();
if (response.Request == null && response.ErrorException != null) if (response.Request == null && response.ErrorException != null)
{ {
throw response.ErrorException; throw response.ErrorException;
} }
Ensure.That(() => response.Request).IsNotNull(); Ensure.That(response.Request, () => response.Request).IsNotNull();
Ensure.That(() => restClient).IsNotNull(); Ensure.That(restClient, () => restClient).IsNotNull();
Logger.Trace("Validating Responses from [{0}] [{1}] status: [{2}]", response.Request.Method, restClient.BuildUri(response.Request), response.StatusCode); Logger.Trace("Validating Responses from [{0}] [{1}] status: [{2}]", response.Request.Method, restClient.BuildUri(response.Request), response.StatusCode);

@ -58,7 +58,7 @@ namespace NzbDrone.Core.Tv
public Series AddSeries(Series newSeries) public Series AddSeries(Series newSeries)
{ {
Ensure.That(() => newSeries).IsNotNull(); Ensure.That(newSeries, () => newSeries).IsNotNull();
if (String.IsNullOrWhiteSpace(newSeries.Path)) if (String.IsNullOrWhiteSpace(newSeries.Path))
{ {
@ -124,7 +124,7 @@ namespace NzbDrone.Core.Tv
public Series UpdateSeries(Series series) public Series UpdateSeries(Series series)
{ {
var storedSeries = GetSeries(series.Id); var storedSeries = GetSeries(series.Id);
foreach (var season in series.Seasons) foreach (var season in series.Seasons)
{ {
var storedSeason = storedSeries.Seasons.SingleOrDefault(s => s.SeasonNumber == season.SeasonNumber); var storedSeason = storedSeries.Seasons.SingleOrDefault(s => s.SeasonNumber == season.SeasonNumber);

Loading…
Cancel
Save