public static async Task <List <PredictionResult>?> PredictInput(string client, Ast ast, Token[] astTokens, int millisecondsTimeout) { Requires.Condition(millisecondsTimeout > 0, nameof(millisecondsTimeout)); var predictors = SubsystemManager.GetSubsystems <ICommandPredictor>(); if (predictors.Count == 0) { return(null); } var context = new PredictionContext(ast, astTokens); var tasks = new Task <PredictionResult?> [predictors.Count]; using var cancellationSource = new CancellationTokenSource(); for (int i = 0; i < predictors.Count; i++) { ICommandPredictor predictor = predictors[i]; tasks[i] = Task.Factory.StartNew( state => { var predictor = (ICommandPredictor)state !; SuggestionPackage pkg = predictor.GetSuggestion(client, context, cancellationSource.Token); return(pkg.SuggestionEntries?.Count > 0 ? new PredictionResult(predictor.Id, predictor.Name, pkg.Session, pkg.SuggestionEntries) : null); }, predictor, cancellationSource.Token, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); } await Task.WhenAny( Task.WhenAll(tasks), Task.Delay(millisecondsTimeout, cancellationSource.Token)).ConfigureAwait(false); cancellationSource.Cancel(); var resultList = new List <PredictionResult>(predictors.Count); foreach (Task <PredictionResult?> task in tasks) { if (task.IsCompletedSuccessfully) { PredictionResult?result = task.Result; if (result != null) { resultList.Add(result); } } } return(resultList); }
public static void UnregisterSubsystem() { // Exception expected when no implementation is registered Assert.Throws <InvalidOperationException>(() => SubsystemManager.UnregisterSubsystem <ICommandPredictor>(predictor1.Id)); SubsystemManager.RegisterSubsystem <ICommandPredictor, MyPredictor>(predictor1); SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, predictor2); // Exception is expected when specified id cannot be found Assert.Throws <InvalidOperationException>(() => SubsystemManager.UnregisterSubsystem <ICommandPredictor>(Guid.NewGuid())); // Unregister 'predictor1' SubsystemManager.UnregisterSubsystem <ICommandPredictor>(predictor1.Id); SubsystemInfo ssInfo = SubsystemManager.GetSubsystemInfo(SubsystemKind.CommandPredictor); VerifyCommandPredictorMetadata(ssInfo); Assert.True(ssInfo.IsRegistered); Assert.Single(ssInfo.Implementations); var implInfo = ssInfo.Implementations[0]; Assert.Equal(predictor2.Id, implInfo.Id); Assert.Equal(predictor2.Name, implInfo.Name); Assert.Equal(predictor2.Description, implInfo.Description); Assert.Equal(SubsystemKind.CommandPredictor, implInfo.Kind); Assert.Same(typeof(MyPredictor), implInfo.ImplementationType); ICommandPredictor impl = SubsystemManager.GetSubsystem <ICommandPredictor>(); Assert.Same(impl, predictor2); ReadOnlyCollection <ICommandPredictor> impls = SubsystemManager.GetSubsystems <ICommandPredictor>(); Assert.Single(impls); Assert.Same(predictor2, impls[0]); // Unregister 'predictor2' SubsystemManager.UnregisterSubsystem(SubsystemKind.CommandPredictor, predictor2.Id); VerifyCommandPredictorMetadata(ssInfo); Assert.False(ssInfo.IsRegistered); Assert.Empty(ssInfo.Implementations); impl = SubsystemManager.GetSubsystem <ICommandPredictor>(); Assert.Null(impl); impls = SubsystemManager.GetSubsystems <ICommandPredictor>(); Assert.Empty(impls); }
public static void GetSubsystemInfo() { SubsystemInfo ssInfo = SubsystemManager.GetSubsystemInfo(typeof(ICommandPredictor)); VerifySubsystemMetadata(ssInfo); Assert.False(ssInfo.IsRegistered); Assert.Empty(ssInfo.Implementations); SubsystemInfo ssInfo2 = SubsystemManager.GetSubsystemInfo(SubsystemKind.CommandPredictor); Assert.Same(ssInfo2, ssInfo); ReadOnlyCollection <SubsystemInfo> ssInfos = SubsystemManager.GetAllSubsystemInfo(); Assert.Single(ssInfos); Assert.Same(ssInfos[0], ssInfo); ICommandPredictor impl = SubsystemManager.GetSubsystem <ICommandPredictor>(); Assert.Null(impl); ReadOnlyCollection <ICommandPredictor> impls = SubsystemManager.GetSubsystems <ICommandPredictor>(); Assert.Empty(impls); }