public static void PredictInput() { const string Input = "Hello world"; MyPredictor slow = MyPredictor.SlowPredictor; MyPredictor fast = MyPredictor.FastPredictor; Ast ast = Parser.ParseInput(Input, out Token[] tokens, out _); // Returns null when no predictor implementation registered List <PredictionResult> results = CommandPrediction.PredictInput(Client, ast, tokens).Result; Assert.Null(results); try { // Register 2 predictor implementations SubsystemManager.RegisterSubsystem <ICommandPredictor, MyPredictor>(slow); SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, fast); // Expect the results from 'fast' predictor only b/c the 'slow' one // cannot finish before the specified timeout. // The specified timeout is exaggerated to make the test reliable. // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small. results = CommandPrediction.PredictInput(Client, ast, tokens, millisecondsTimeout: 1000).Result; Assert.Single(results); PredictionResult res = results[0]; Assert.Equal(fast.Id, res.Id); Assert.Equal(Session, res.Session); Assert.Equal(2, res.Suggestions.Count); Assert.Equal($"'{Input}' from '{Client}' - TEST-1 from {fast.Name}", res.Suggestions[0].SuggestionText); Assert.Equal($"'{Input}' from '{Client}' - TeSt-2 from {fast.Name}", res.Suggestions[1].SuggestionText); // Expect the results from both 'slow' and 'fast' predictors // Same here -- the specified timeout is exaggerated to make the test reliable. // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small. results = CommandPrediction.PredictInput(Client, ast, tokens, millisecondsTimeout: 4000).Result; Assert.Equal(2, results.Count); PredictionResult res1 = results[0]; Assert.Equal(slow.Id, res1.Id); Assert.Equal(Session, res1.Session); Assert.Equal(2, res1.Suggestions.Count); Assert.Equal($"'{Input}' from '{Client}' - TEST-1 from {slow.Name}", res1.Suggestions[0].SuggestionText); Assert.Equal($"'{Input}' from '{Client}' - TeSt-2 from {slow.Name}", res1.Suggestions[1].SuggestionText); PredictionResult res2 = results[1]; Assert.Equal(fast.Id, res2.Id); Assert.Equal(Session, res2.Session); Assert.Equal(2, res2.Suggestions.Count); Assert.Equal($"'{Input}' from '{Client}' - TEST-1 from {fast.Name}", res2.Suggestions[0].SuggestionText); Assert.Equal($"'{Input}' from '{Client}' - TeSt-2 from {fast.Name}", res2.Suggestions[1].SuggestionText); } finally { SubsystemManager.UnregisterSubsystem <ICommandPredictor>(slow.Id); SubsystemManager.UnregisterSubsystem(SubsystemKind.CommandPredictor, fast.Id); } }
public static void Feedback() { MyPredictor slow = MyPredictor.SlowPredictor; MyPredictor fast = MyPredictor.FastPredictor; try { // Register 2 predictor implementations SubsystemManager.RegisterSubsystem <ICommandPredictor, MyPredictor>(slow); SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, fast); var history = new[] { "hello", "world" }; var ids = new HashSet <Guid> { slow.Id, fast.Id }; CommandPrediction.OnCommandLineAccepted(Client, history); CommandPrediction.OnSuggestionDisplayed(Client, slow.Id, Session, 2); CommandPrediction.OnSuggestionDisplayed(Client, fast.Id, Session, -1); CommandPrediction.OnSuggestionAccepted(Client, slow.Id, Session, "Yeah"); // The calls to 'StartEarlyProcessing' and 'OnSuggestionAccepted' are queued in thread pool, // so we wait a bit to make sure the calls are done. while (slow.History.Count == 0 || slow.AcceptedSuggestions.Count == 0) { Thread.Sleep(10); } Assert.Equal(2, slow.History.Count); Assert.Equal($"{Client}-{history[0]}", slow.History[0]); Assert.Equal($"{Client}-{history[1]}", slow.History[1]); Assert.Equal(2, fast.History.Count); Assert.Equal($"{Client}-{history[0]}", fast.History[0]); Assert.Equal($"{Client}-{history[1]}", fast.History[1]); Assert.Single(slow.DisplayedSuggestions); Assert.Equal($"{Client}-{Session}-2", slow.DisplayedSuggestions[0]); Assert.Single(fast.DisplayedSuggestions); Assert.Equal($"{Client}-{Session}--1", fast.DisplayedSuggestions[0]); Assert.Single(slow.AcceptedSuggestions); Assert.Equal($"{Client}-{Session}-Yeah", slow.AcceptedSuggestions[0]); Assert.Empty(fast.AcceptedSuggestions); } finally { SubsystemManager.UnregisterSubsystem <ICommandPredictor>(slow.Id); SubsystemManager.UnregisterSubsystem(SubsystemKind.CommandPredictor, fast.Id); } }
static MyPredictor() { SlowPredictor = new MyPredictor( Guid.NewGuid(), "Test Predictor #1", "Description for #1 predictor.", delay: true); FastPredictor = new MyPredictor( Guid.NewGuid(), "Test Predictor #2", "Description for #2 predictor.", delay: false); }
static SubsystemTests() { predictor1 = MyPredictor.FastPredictor; predictor2 = MyPredictor.SlowPredictor; }