public void InvokeAsEventHandlerWhileIgnoringErrorsWithNullThis() { var e = Assert.Throws <ArgumentNullException>(() => DelegateExtensions.InvokeAsEventHandlerWhileIgnoringErrors(null, new object(), EventArgs.Empty)); Assert.Equal("del", e.ParamName); }
private object delegateEvent(Delegate eventHandler, object[] args) { string delegateSignature = DelegateExtensions.DelegateSignature(eventHandler); DebugLog(String.Format("SynchronizationContext's target thread; EventHandler={0}; args={1}", delegateSignature, args)); if (SwallowExceptionsInEvents) { return(fireEventsNoThrow(eventHandler, args)); } else { /// http://msdn2.microsoft.com/en-us/library/system.delegate(VS.80).aspx /// If an invoked method throws an exception, the method stops executing, /// the exception is passed back to the caller of the delegate, /// and remaining methods in the invocation list are not invoked. /// Catching the exception in the caller does not alter this behavior. try { return(eventHandler.DynamicInvoke(args)); } catch (Exception ex) { logException(eventHandler, ex); } return(null); } }
private static void logException(Delegate eventHandler, Exception ex) { string delegateSignature = DelegateExtensions.DelegateSignature(eventHandler); DebugLog("Dynamic Invoke Error in " + delegateSignature); DebugLog(ex.ToString()); }
/// <summary> /// Attempts to create an index writer, it will retry on failure 5 times and on the last time will try to forcefully unlock the index files /// </summary> /// <param name="baseLuceneDirectory"></param> /// <param name="analyzer"></param> /// <returns></returns> private Attempt <IndexWriter> TryCreateWriterWithRetry(Lucene.Net.Store.Directory baseLuceneDirectory, Analyzer analyzer) { var maxTries = 5; var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { //last try... if (currentTry == maxTries) { LogHelper.Info <LocalTempStorageIndexer>("Could not acquire index lock, attempting to force unlock it..."); //unlock it! IndexWriter.Unlock(baseLuceneDirectory); } var writerAttempt = TryCreateWriter(baseLuceneDirectory, analyzer); if (writerAttempt) { return(writerAttempt); } LogHelper.Info <LocalTempStorageIndexer>("Could not create writer on {0}, retrying ....", baseLuceneDirectory.ToString); return(Attempt <IndexWriter> .Fail()); }, 5, TimeSpan.FromSeconds(1)); return(result); }
public void It_should_have_default_ctor() { // Act Func <Exception> act = Activator.CreateInstance <T>; // Assert DelegateExtensions.Should(act).NotThrow("a default constructor is expected"); }
public string GetErrorMessage() { var loginScreen = GetLoginScreen(); var errorLabel = DelegateExtensions.ExecuteWithResult(() => loginScreen.Get <Label>(SearchCriteria.ByAutomationId("Login_FailureTextBlock")), r => r.Text, new[] { null, string.Empty }); return(errorLabel.Text); }
internal Window GetShell() { var application = ApplicationContext.Application; var shellScreen = DelegateExtensions.ExecuteWithResult( () => application.GetWindow(SearchCriteria.ByAutomationId("Shell_Window"), InitializeOption.NoCache)); shellScreen.WaitWhileBusy(); return(shellScreen); }
public void AsEnumerable_OnArray_CastsToEnumerable() { // ----------------------- Arrange ----------------------- int[] data = { 1, 2, 3 }; // ----------------------- Act ----------------------- IEnumerable <int> dataEnumerable = DelegateExtensions.AsEnumerable(1, 2, 3); // ----------------------- Assert ----------------------- Assert.True(dataEnumerable.SequenceEqual(data)); }
static MaiSong() { AddCustomFunc(nameof(ChartRating), (song, input) => { if (input is string inputStr) { return(inputStr.EqualsAny(song.ChartBasicRating, song.ChartAdvancedRating, song.ChartExpertRating, song.ChartMasterRating, song.ChartRemasterRating)); } return(false); }); AddCustomFunc(nameof(SplashChartRating), (song, input) => { if (input is string inputStr) { return(inputStr.EqualsAny(song.SplashChartBasicRating, song.SplashChartAdvancedRating, song.SplashChartExpertRating, song.SplashChartMasterRating, song.SplashChartRemasterRating)); } return(false); }); AddCustomFunc(nameof(ChartConstant), (song, input) => { if (input is IntervalDoublePair pair) { return(DelegateExtensions.SatisfyAny(pair.IsInInterval, song.ChartBasicConstant, song.ChartAdvancedConstant, song.ChartExpertConstant, song.ChartMasterConstant, song.ChartRemasterConstant)); } return(false); }); AddCustomComparison(nameof(ChartConstant), isDesc => { return((song, maimaiSong) => (song.ChartRemasterConstant ?? song.ChartMasterConstant).CompareToObj( maimaiSong.ChartRemasterConstant ?? maimaiSong.ChartMasterConstant, isDesc)); }); AddCustomFunc(nameof(SplashChartConstant), (song, input) => { if (input is IntervalDoublePair pair) { return(DelegateExtensions.SatisfyAny(pair.IsInInterval, song.SplashChartBasicConstant, song.SplashChartAdvancedConstant, song.SplashChartExpertConstant, song.SplashChartMasterConstant, song.SplashChartRemasterConstant)); } return(false); }); AddCustomComparison(nameof(SplashChartConstant), isDesc => { return((song, maimaiSong) => (song.SplashChartRemasterConstant ?? song.SplashChartMasterConstant).CompareToObj( maimaiSong.SplashChartRemasterConstant ?? maimaiSong.SplashChartMasterConstant, isDesc)); }); }
public void Quits_On_Success_Count() { var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { totalTries = currentTry; return(totalTries == 2 ? Attempt <string> .Succeed() : Attempt <string> .Fail()); }, 5, TimeSpan.FromMilliseconds(10)); Assert.AreEqual(2, totalTries); }
public void Only_Executes_Specific_Count() { const int maxTries = 5; var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { totalTries = currentTry; return(Attempt <IndexWriter> .Fail()); }, 5, TimeSpan.FromMilliseconds(10)); Assert.AreEqual(maxTries, totalTries); }
internal Window GetShell() { var application = ApplicationContext.Application; application.WaitWhileBusy(); using (var automation = new UIA3Automation()) { var shellScreen = DelegateExtensions.ExecuteWithResult( () => application.GetAllTopLevelWindows(automation) .FirstOrDefault(t => t.Properties.AutomationId == "Shell_Window")); return(shellScreen); } }
private bool TryWaitForDirectoryUnlock(Lucene.Net.Store.Directory dir) { var maxTries = 5; var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { //last try... if (currentTry == maxTries) { LogHelper.Info <LocalTempStorageIndexer>("Could not acquire directory lock, attempting to force unlock it..."); //unlock it! IndexWriter.Unlock(dir); } if (IndexWriter.IsLocked(dir) == false) { return(Attempt.Succeed(true)); } LogHelper.Info <LocalTempStorageIndexer>("Could not acquire directory lock for {0} writer, retrying ....", dir.ToString); return(Attempt <bool> .Fail()); }, 5, TimeSpan.FromSeconds(1)); return(result); }
public void Initialize(NameValueCollection config, string configuredPath, FSDirectory baseLuceneDirectory, Analyzer analyzer, LocalStorageType localStorageType) { var codegenPath = HttpRuntime.CodegenDir; TempPath = Path.Combine(codegenPath, configuredPath.TrimStart('~', '/').Replace("/", "\\")); switch (localStorageType) { case LocalStorageType.Sync: var success = InitializeLocalIndexAndDirectory(baseLuceneDirectory, analyzer, configuredPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //flag to disable the mirrored folder if not successful (int)success >= 100); //If the master index simply doesn't exist, we don't continue to try to open anything since there will // actually be nothing there. if (success == InitializeDirectoryFlags.SuccessNoIndexExists) { return; } //Try to open the reader, this will fail if the index is corrupt and we'll need to handle that var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts(i => { try { using (IndexReader.Open( LuceneDirectory, DeletePolicyTracker.Current.GetPolicy(LuceneDirectory), true)) { } return(Attempt.Succeed(true)); } catch (Exception ex) { LogHelper.WarnWithException <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... retrying... {0}", configuredPath), ex); } return(Attempt.Fail(false)); }, 5, TimeSpan.FromSeconds(1)); if (result.Success == false) { LogHelper.Warn <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... attempting to clear index files in local temp storage, will operate from main storage only {0}", configuredPath)); ClearFilesInPath(TempPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //Disable mirrored index, we're kind of screwed here only use master index true); } break; case LocalStorageType.LocalOnly: if (Directory.Exists(TempPath) == false) { Directory.CreateDirectory(TempPath); } LuceneDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(TempPath)); break; default: throw new ArgumentOutOfRangeException("localStorageType"); } }
public void Initialize(NameValueCollection config, string configuredPath, FSDirectory baseLuceneDirectory, Analyzer analyzer, LocalStorageType localStorageType) { //this is the default ILocalStorageDirectory localStorageDir = new CodeGenLocalStorageDirectory(); if (config["tempStorageDirectory"] != null) { //try to get the type var dirType = BuildManager.GetType(config["tempStorageDirectory"], false); if (dirType != null) { try { localStorageDir = (ILocalStorageDirectory)Activator.CreateInstance(dirType); } catch (Exception ex) { LogHelper.Error <LocalTempStorageIndexer>( string.Format("Could not create a temp storage location of type {0}, reverting to use the " + typeof(CodeGenLocalStorageDirectory).FullName, dirType), ex); } } } var tempPath = localStorageDir.GetLocalStorageDirectory(config, configuredPath); if (tempPath == null) { throw new InvalidOperationException("Could not resolve a temp location from the " + localStorageDir.GetType() + " specified"); } TempPath = tempPath.FullName; switch (localStorageType) { case LocalStorageType.Sync: var success = InitializeLocalIndexAndDirectory(baseLuceneDirectory, analyzer, configuredPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //flag to disable the mirrored folder if not successful (int)success >= 100); //If the master index simply doesn't exist, we don't continue to try to open anything since there will // actually be nothing there. if (success == InitializeDirectoryFlags.SuccessNoIndexExists) { return; } //Try to open the reader, this will fail if the index is corrupt and we'll need to handle that var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts(i => { try { using (IndexReader.Open( LuceneDirectory, DeletePolicyTracker.Current.GetPolicy(LuceneDirectory), true)) { } return(Attempt.Succeed(true)); } catch (Exception ex) { LogHelper.WarnWithException <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... retrying... {0}", configuredPath), ex); } return(Attempt.Fail(false)); }, 5, TimeSpan.FromSeconds(1)); if (result.Success == false) { LogHelper.Warn <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... attempting to clear index files in local temp storage, will operate from main storage only {0}", configuredPath)); ClearFilesInPath(TempPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //Disable mirrored index, we're kind of screwed here only use master index true); } break; case LocalStorageType.LocalOnly: if (Directory.Exists(TempPath) == false) { Directory.CreateDirectory(TempPath); } LuceneDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(TempPath)); break; default: throw new ArgumentOutOfRangeException("localStorageType"); } }