public void OnCapacity() { const int capacity = 100; var history = new HistoryQueue <int>(capacity); const int numbersToAdd = 100; for (int i = 0; i < numbersToAdd; i++) { history.Enqueue(i); } Assert.IsTrue(history.Count == numbersToAdd); for (int i = 0; i < numbersToAdd; i++) { Assert.AreEqual(i, history[i]); } }
public void OverCapacity() { const int capacity = 100001; var history = new HistoryQueue <int>(capacity); const int numbersToAdd = 200002; for (int i = 0; i < numbersToAdd; i++) { history.Enqueue(i); } Assert.IsTrue(history.Count == capacity); for (int i = 0; i < history.Count; i++) { Assert.AreEqual(numbersToAdd - capacity + i, history[i]); } }
/// <summary> /// Executes the action asynchronously, de-bouncing if necessary. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An awaitable task containing the result.</returns> protected override async Task <ScheduledActionResult> DoExecuteAsync( CancellationToken cancellationToken = default(CancellationToken)) { // Start stopwatch, and mark time the request was made. Stopwatch stopwatch = Stopwatch.StartNew(); Instant started = TimeHelpers.Clock.Now; // Combine cancellation token with Timeout to ensure no action runs beyond the Scheduler's limit. using (ITokenSource tokenSource = cancellationToken.WithTimeout(MaximumDurationMs)) { // Quick cancellation check. // ReSharper disable once PossibleNullReferenceException if (tokenSource.IsCancellationRequested) { // ReSharper disable once AssignNullToNotNullAttribute return(new ScheduledFunctionResult <T>( NextDue, TimeHelpers.Clock.Now, Duration.Zero, null, true, default(T))); } // Always yield to ensure tasks run asynchronously. await Task.Yield(); // Wait until the semaphore says we can go. using (await _lock.LockAsync(tokenSource.Token).ConfigureAwait(false)) { // Quick cancellation check. // ReSharper disable once PossibleNullReferenceException if (tokenSource.IsCancellationRequested) { // ReSharper disable once AssignNullToNotNullAttribute return(new ScheduledFunctionResult <T>( NextDue, TimeHelpers.Clock.Now, Duration.Zero, null, true, default(T))); } ScheduledFunctionResult <T> result; // De-bounce - we started before the last execution finished, so return last result, so long as it wasn't cancelled. if (started <= LastExecutionFinished) { result = HistoryQueue.LastOrDefault() as ScheduledFunctionResult <T>; if (result != null && !result.Cancelled) { return(result); } } // Get the due date, and set to not due. long ndt = Interlocked.Exchange(ref NextDueTicksInternal, Scheduler.MaxTicks); Instant due = new Instant(ndt); try { // Execute function (ensuring cancellation). // ReSharper disable once AssignNullToNotNullAttribute T r = await _function(due, tokenSource.Token) .WithCancellation(tokenSource.Token) .ConfigureAwait(false); stopwatch.Stop(); result = new ScheduledFunctionResult <T>( due, started, Duration.FromTimeSpan(stopwatch.Elapsed), null, tokenSource.IsCancellationRequested, r); } catch (Exception e) { stopwatch.Stop(); bool cancelled = tokenSource.IsCancellationRequested || e is TaskCanceledException || e is OperationCanceledException; // ReSharper disable once AssignNullToNotNullAttribute result = new ScheduledFunctionResult <T>( due, started, Duration.FromTimeSpan(stopwatch.Elapsed), !cancelled ? e : null, cancelled, default(T)); } // Increment the execution count. Interlocked.Increment(ref ExecutionCountInternal); // Mark execution finished LastExecutionFinished = TimeHelpers.Clock.Now; // Enqueue history item. HistoryQueue.Enqueue(result); // Recalculate when we're next due. RecalculateNextDue(due); return(result); } } }
private void SetOptionsInternal(SetPSReadlineOption options) { if (options.ContinuationPrompt != null) { Options.ContinuationPrompt = options.ContinuationPrompt; } if (options._continuationPromptForegroundColor.HasValue) { Options.ContinuationPromptForegroundColor = options.ContinuationPromptForegroundColor; } if (options._continuationPromptBackgroundColor.HasValue) { Options.ContinuationPromptBackgroundColor = options.ContinuationPromptBackgroundColor; } if (options._emphasisBackgroundColor.HasValue) { Options.EmphasisBackgroundColor = options.EmphasisBackgroundColor; } if (options._emphasisForegroundColor.HasValue) { Options.EmphasisForegroundColor = options.EmphasisForegroundColor; } if (options._errorBackgroundColor.HasValue) { Options.ErrorBackgroundColor = options.ErrorBackgroundColor; } if (options._errorForegroundColor.HasValue) { Options.ErrorForegroundColor = options.ErrorForegroundColor; } if (options._historyNoDuplicates.HasValue) { Options.HistoryNoDuplicates = options.HistoryNoDuplicates; } if (options._historySearchCursorMovesToEnd.HasValue) { Options.HistorySearchCursorMovesToEnd = options.HistorySearchCursorMovesToEnd; } if (options._addToHistoryHandlerSpecified) { Options.AddToHistoryHandler = options.AddToHistoryHandler; } if (options._commandValidationHandlerSpecified) { Options.CommandValidationHandler = options.CommandValidationHandler; } if (options._maximumHistoryCount.HasValue) { Options.MaximumHistoryCount = options.MaximumHistoryCount; if (_history != null) { var newHistory = new HistoryQueue<HistoryItem>(Options.MaximumHistoryCount); while (_history.Count > Options.MaximumHistoryCount) { _history.Dequeue(); } while (_history.Count > 0) { newHistory.Enqueue(_history.Dequeue()); } _history = newHistory; _currentHistoryIndex = _history.Count; } } if (options._maximumKillRingCount.HasValue) { Options.MaximumKillRingCount = options.MaximumKillRingCount; // TODO - make _killRing smaller } if (options._editMode.HasValue) { Options.EditMode = options.EditMode; // Switching/resetting modes - clear out chord dispatch table _chordDispatchTable.Clear(); SetDefaultBindings(Options.EditMode); } if (options._showToolTips.HasValue) { Options.ShowToolTips = options.ShowToolTips; } if (options._extraPromptLineCount.HasValue) { Options.ExtraPromptLineCount = options.ExtraPromptLineCount; } if (options._dingTone.HasValue) { Options.DingTone = options.DingTone; } if (options._dingDuration.HasValue) { Options.DingDuration = options.DingDuration; } if (options._bellStyle.HasValue) { Options.BellStyle = options.BellStyle; } if (options._completionQueryItems.HasValue) { Options.CompletionQueryItems = options.CompletionQueryItems; } if (options.WordDelimiters != null) { Options.WordDelimiters = options.WordDelimiters; } if (options._historySearchCaseSensitive.HasValue) { Options.HistorySearchCaseSensitive = options.HistorySearchCaseSensitive; } if (options._historySaveStyle.HasValue) { Options.HistorySaveStyle = options.HistorySaveStyle; } #region vi if (options._viModeIndicator.HasValue) { Options.ViModeIndicator = options.ViModeIndicator; } #endregion if (options.HistorySavePath != null) { Options.HistorySavePath = options.HistorySavePath; if (_historyFileMutex != null) { _historyFileMutex.Dispose(); } _historyFileMutex = new Mutex(false, GetHistorySaveFileMutexName()); _historyFileLastSavedSize = 0; } if (options.ResetTokenColors) { Options.ResetColors(); } if (options._tokenKind.HasValue) { if (options._foregroundColor.HasValue) { Options.SetForegroundColor(options.TokenKind, options.ForegroundColor); } if (options._backgroundColor.HasValue) { Options.SetBackgroundColor(options.TokenKind, options.BackgroundColor); } } }
private void SetOptionsInternal(SetPSReadlineOption options) { if (options.ContinuationPrompt != null) { Options.ContinuationPrompt = options.ContinuationPrompt; } if (options._continuationPromptForegroundColor.HasValue) { Options.ContinuationPromptForegroundColor = options.ContinuationPromptForegroundColor; } if (options._continuationPromptBackgroundColor.HasValue) { Options.ContinuationPromptBackgroundColor = options.ContinuationPromptBackgroundColor; } if (options._emphasisBackgroundColor.HasValue) { Options.EmphasisBackgroundColor = options.EmphasisBackgroundColor; } if (options._emphasisForegroundColor.HasValue) { Options.EmphasisForegroundColor = options.EmphasisForegroundColor; } if (options._historyNoDuplicates.HasValue) { Options.HistoryNoDuplicates = options.HistoryNoDuplicates; if (Options.HistoryNoDuplicates) { _hashedHistory.Clear(); _history.OnEnqueue = null; _history.OnDequeue = null; var newHistory = new HistoryQueue<HistoryItem>(Options.MaximumHistoryCount); while (_history.Count > 0) { var item = _history.Dequeue(); var itemStr = item._line; if (!_hashedHistory.Contains(itemStr)) { newHistory.Enqueue(item); _hashedHistory.Add(itemStr); } } _history = newHistory; _history.OnEnqueue = HistoryOnEnqueueHandler; _history.OnDequeue = HistoryOnDequeueHandler; _currentHistoryIndex = _history.Count; } } if (options._historySearchCursorMovesToEnd.HasValue) { Options.HistorySearchCursorMovesToEnd = options.HistorySearchCursorMovesToEnd; } if (options._addToHistoryHandlerSpecified) { Options.AddToHistoryHandler = options.AddToHistoryHandler; } if (options._maximumHistoryCount.HasValue) { Options.MaximumHistoryCount = options.MaximumHistoryCount; var newHistory = new HistoryQueue<HistoryItem>(Options.MaximumHistoryCount); while (_history.Count > Options.MaximumHistoryCount) { _history.Dequeue(); } _history.OnEnqueue = null; _history.OnDequeue = null; while (_history.Count > 0) { newHistory.Enqueue(_history.Dequeue()); } _history = newHistory; _history.OnEnqueue = HistoryOnEnqueueHandler; _history.OnDequeue = HistoryOnDequeueHandler; _currentHistoryIndex = _history.Count; } if (options._maximumKillRingCount.HasValue) { Options.MaximumKillRingCount = options.MaximumKillRingCount; // TODO - make _killRing smaller } if (options._editMode.HasValue) { Options.EditMode = options.EditMode; // Switching/resetting modes - clear out chord dispatch table _chordDispatchTable.Clear(); switch (options._editMode) { case EditMode.Emacs: SetDefaultEmacsBindings(); break; #if FALSE case EditMode.Vi: //TODO: _dispatchTable = _viKeyMap; break; #endif case EditMode.Windows: SetDefaultWindowsBindings(); break; } } if (options._showToolTips.HasValue) { Options.ShowToolTips = options.ShowToolTips; } if (options._extraPromptLineCount.HasValue) { Options.ExtraPromptLineCount = options.ExtraPromptLineCount; } if (options._dingTone.HasValue) { Options.DingTone = options.DingTone; } if (options._dingDuration.HasValue) { Options.DingDuration = options.DingDuration; } if (options._bellStyle.HasValue) { Options.BellStyle = options.BellStyle; } if (options._completionQueryItems.HasValue) { Options.CompletionQueryItems = options.CompletionQueryItems; } if (options.WordDelimiters != null) { Options.WordDelimiters = options.WordDelimiters; } if (options._historySearchCaseSensitive.HasValue) { Options.HistorySearchCaseSensitive = options.HistorySearchCaseSensitive; } if (options.ResetTokenColors) { Options.ResetColors(); } if (options._tokenKind.HasValue) { if (options._foregroundColor.HasValue) { Options.SetForegroundColor(options.TokenKind, options.ForegroundColor); } if (options._backgroundColor.HasValue) { Options.SetBackgroundColor(options.TokenKind, options.BackgroundColor); } } }