diff --git a/EvenBetterFastSim.Tests/ObservableLoggerTests.cs b/EvenBetterFastSim.Tests/ObservableLoggerTests.cs new file mode 100644 index 0000000..80fa434 --- /dev/null +++ b/EvenBetterFastSim.Tests/ObservableLoggerTests.cs @@ -0,0 +1,81 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using EvenBetterFastSim.Logging; +using Microsoft.Extensions.Logging; +using Xunit; + +namespace EvenBetterFastSim.Tests; + +/// +/// The System Log logger queues its entries for the UI thread instead of waiting for it +/// (GitLab secsgem/evencoolerfastsim#26). With Dispatcher.Invoke every log call from the scenario engine or the receive +/// loop took as long as the UI was busy, and a caller the UI thread itself was waiting for blocked for seconds. +/// +public class ObservableLoggerTests +{ + [Fact] + public async Task Log_does_not_wait_for_a_busy_UI_thread() + { + using var ui = new UiThread(); + var log = new LogService(); + var logger = new ObservableLogger(log, "test", () => ui.Dispatcher); + using var busy = new ManualResetEventSlim(); + using var release = new ManualResetEventSlim(); + _ = ui.Dispatcher.InvokeAsync(() => + { + busy.Set(); + release.Wait(TimeSpan.FromSeconds(30)); + }); + var cancellation = TestContext.Current.CancellationToken; + Assert.True(busy.Wait(TimeSpan.FromSeconds(10), cancellation), "the UI thread did not start"); + try + { + var logging = Task.Run(() => logger.LogWarning("while the UI thread is busy"), cancellation); + var finished = await Task.WhenAny(logging, Task.Delay(TimeSpan.FromSeconds(5), cancellation)); + + Assert.True(finished == logging, "the log call waited for the UI thread"); + await logging; + // The UI thread is still busy, so nothing has reached the collection yet. + Assert.Empty(log.LogMessages); + } + finally + { + release.Set(); + } + + var entry = Assert.Single(ui.AfterQueuedWork(() => log.LogMessages.ToList())); + Assert.Equal("while the UI thread is busy", entry.Message); + Assert.Equal(LogLevel.Warning, entry.Level); + } + + [Fact] + public void Entries_arrive_on_the_UI_thread_in_the_order_of_the_calls() + { + using var ui = new UiThread(); + var log = new LogService(); + var addedOn = new System.Collections.Generic.HashSet(); + log.LogMessages.CollectionChanged += (_, _) => addedOn.Add(Environment.CurrentManagedThreadId); + var logger = new ObservableLogger(log, "test", () => ui.Dispatcher); + + for (var i = 0; i < 300; i++) + logger.LogInformation("entry {Number}", i); + + var messages = ui.AfterQueuedWork(() => log.LogMessages.Select(m => m.Message).ToList()); + Assert.Equal(Enumerable.Range(0, 300).Select(i => $"entry {i}"), messages); + Assert.Equal([ui.ThreadId], addedOn); + } + + /// No Application (unit tests, shutdown): the entry is dropped; until #26 the log call threw. + [Fact] + public void Without_a_dispatcher_the_entry_is_dropped() + { + var log = new LogService(); + + new ObservableLogger(log, "test", () => null).LogError("nobody shows this"); + new ObservableLogger(log, "test").LogError("no Application in the test process"); + + Assert.Empty(log.LogMessages); + } +} diff --git a/EvenBetterFastSim.Tests/ScenarioRunTests.cs b/EvenBetterFastSim.Tests/ScenarioRunTests.cs new file mode 100644 index 0000000..08ea18d --- /dev/null +++ b/EvenBetterFastSim.Tests/ScenarioRunTests.cs @@ -0,0 +1,378 @@ +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Threading; +using EvenBetterFastSim.Services; +using Logging.Interfaces; +using SecsGemBaseItems.Data_Containers; +using SecsGemBaseItems.Data_Containers.Serialization; +using SecsGemScenarioEngine.Models; +using SecsGemScenarioEngine.Services; +using Xunit; + +namespace EvenBetterFastSim.Tests; + +/// +/// Scenarios run on the thread pool instead of the WPF UI thread, with a copy of the graph that nothing on the UI +/// thread can change under the engine (GitLab secsgem/evencoolerfastsim#26). +/// +public class ScenarioRunTests +{ + /// + /// The caller is a thread with a running Dispatcher, like the UI thread. The graph is copied on that thread, where + /// the library may be read. The engine then runs on the thread pool and must not see the caller's context: not when + /// it starts and not after its first await, or its continuations would go back to that thread. + /// + /// + /// A plain SynchronizationContext would not do as the caller's context: await treats the base type like no context, + /// so an implementation that awaits before it calls the engine would end up on the thread pool in the test and on + /// the UI thread in the app. + /// + [Fact] + public async Task Graph_is_copied_on_the_callers_thread_and_the_engine_runs_on_the_thread_pool() + { + using var ui = new UiThread(); + var engine = new RecordingEngine(); + var transaction = Transaction(6, 11); + var library = new RecordingLibrary(transaction); + var graph = new ScenarioGraph + { + Nodes = [new ScenarioNode { Type = NodeType.Send, TransactionName = transaction.PrimaryMessage.Name }] + }; + using var cancellation = new CancellationTokenSource(); + var consumed = new ConcurrentDictionary(); + var deadline = TimeSpan.FromSeconds(7); + var progress = new Progress(); + SynchronizationContext? callerContext = null; + + var run = ui.Dispatcher.Invoke(() => + { + callerContext = SynchronizationContext.Current; + return ScenarioRun.RunAsync(engine, graph, library, cancellation.Token, consumed, deadline, progress); + }); + var result = await run.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken); + + Assert.IsType(callerContext); + Assert.Same(engine.Result, result); + Assert.Equal(ui.ThreadId, library.EnumeratedOnThread); + Assert.True(engine.OnThreadPool); + Assert.NotEqual(ui.ThreadId, engine.ThreadId); + Assert.Null(engine.ContextAtStart); + Assert.Null(engine.ContextAfterAwait); + // The engine got the copy, with the transaction the node only named. + Assert.NotSame(graph, engine.Graph); + Assert.NotNull(engine.Graph!.Nodes.Single().TransactionJson); + Assert.Null(graph.Nodes.Single().TransactionJson); + Assert.Equal(cancellation.Token, engine.Token); + Assert.Same(consumed, engine.Consumed); + Assert.Equal(deadline, engine.Deadline); + Assert.Same(progress, engine.Progress); + } + + /// + /// The engine keeps a run's cancellation source in a field while the run starts. Runs that start at the same time + /// enter the engine one after the other. + /// + [Fact] + public async Task Runs_enter_the_engine_one_at_a_time() + { + var engine = new SlowStartEngine(); + var graph = new ScenarioGraph(); + + var runs = Enumerable.Range(0, 4) + .Select(_ => ScenarioRun.RunAsync(engine, graph, [], CancellationToken.None, null, null, null)) + .ToArray(); + await Task.WhenAll(runs).WaitAsync(TimeSpan.FromSeconds(30), TestContext.Current.CancellationToken); + + Assert.Equal(4, engine.Starts); + Assert.Equal(1, engine.MostAtOnce); + } + + /// + /// Opening a scenario while it runs clears TransactionJson of the stored graph's nodes (LoadGraphToCanvas). The + /// engine reads a node only when it reaches it, so it must have nodes of its own. + /// + [Fact] + public void Snapshot_does_not_change_when_the_stored_graph_changes() + { + var json = SecsGemTransactionJsonConverter.Serialize(Transaction(1, 13)); + var start = new ScenarioNode { Type = NodeType.Start }; + var send = new ScenarioNode { Type = NodeType.Send, TransactionName = "S1F13", TransactionJson = json }; + var edge = new ScenarioEdge { SourceNodeId = start.Id, TargetNodeId = send.Id, IsFailurePath = true }; + var graph = new ScenarioGraph { Name = "n", Description = "d", Nodes = [start, send], Edges = [edge] }; + + var run = ScenarioRun.Snapshot(graph, []); + + Assert.NotSame(graph, run); + Assert.Equal(("n", "d"), (run.Name, run.Description)); + Assert.Equal([start.Id, send.Id], run.Nodes.Select(n => n.Id)); + Assert.All(run.Nodes, node => Assert.DoesNotContain(node, graph.Nodes)); + var runEdge = Assert.Single(run.Edges); + Assert.NotSame(edge, runEdge); + Assert.Equal((start.Id, send.Id, true), (runEdge.SourceNodeId, runEdge.TargetNodeId, runEdge.IsFailurePath)); + + send.TransactionJson = null; + send.TransactionName = "changed"; + edge.TargetNodeId = "changed"; + graph.Nodes.Clear(); + graph.Edges.Clear(); + + Assert.Equal(json, run.Nodes[1].TransactionJson); + Assert.Equal("S1F13", run.Nodes[1].TransactionName); + Assert.Equal(send.Id, run.Edges.Single().TargetNodeId); + Assert.Equal(2, run.Nodes.Count); + } + + [Theory] + [InlineData(NodeType.Send)] + [InlineData(NodeType.SendAndWait)] + [InlineData(NodeType.Receive)] + public void Node_that_only_names_its_transaction_gets_its_own_copy(NodeType type) + { + var transaction = Transaction(6, 11); + // Found like in the engine's own lookup: by the primary message's name, whatever its case. + var name = transaction.PrimaryMessage.Name.ToLowerInvariant(); + Assert.NotEqual(transaction.PrimaryMessage.Name, name); + var named = new ScenarioNode { Type = type, TransactionName = name }; + + var run = ScenarioRun.Snapshot(new ScenarioGraph { Nodes = [named] }, [Transaction(1, 1), transaction]); + + // The graph that gets saved keeps the name-only node. + Assert.Null(named.TransactionJson); + var copy = SecsGemTransactionJsonConverter.Deserialize(run.Nodes.Single().TransactionJson!); + Assert.NotNull(copy); + Assert.NotSame(transaction, copy); + Assert.Equal((6, 11), (copy.PrimaryMessage.Stream, copy.PrimaryMessage.Function)); + Assert.Equal((6, 12), (copy.ReplyMessage.Stream, copy.ReplyMessage.Function)); + } + + /// + /// The converter writes values with the current culture and reads F4/F8 invariant: with a decimal comma the copy of + /// 1,5 would hold 15 (GitLab secsgem/secsgembase#68). + /// + [Fact] + public void Copy_holds_the_same_float_values_with_a_decimal_comma_culture() + { + var transaction = new SecsGemTransaction + { + PrimaryMessage = new MessageFactory(2, 41, true, "primary", new ItemFactory() + .AddList(list => list.AddFloat(1.5f).AddDouble(-2.25).AddI4(-7).AddU4(1000))).Build(), + ReplyMessage = new MessageFactory(2, 42, false, "reply").Build() + }; + var named = new ScenarioNode { Type = NodeType.Send, TransactionName = transaction.PrimaryMessage.Name }; + var culture = CultureInfo.CurrentCulture; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE"); + ScenarioGraph run; + try + { + Assert.Equal("1,5", 1.5f.ToString()); + run = ScenarioRun.Snapshot(new ScenarioGraph { Nodes = [named] }, [transaction]); + Assert.Equal(CultureInfo.GetCultureInfo("de-DE"), CultureInfo.CurrentCulture); + } + finally + { + CultureInfo.CurrentCulture = culture; + } + + var copy = SecsGemTransactionJsonConverter.Deserialize(run.Nodes.Single().TransactionJson!); + var values = ((SecsGemItem)copy!.PrimaryMessage.Children.Single()).Children.Cast() + .Select(item => item.GetBoxedValues().Single()).ToList(); + Assert.Equal([1.5f, -2.25, -7, 1000u], values); + } + + /// The copied node differs from the original at most in TransactionJson, whatever ScenarioNode holds. + [Fact] + public void Copy_keeps_every_other_property_of_the_node() + { + var transaction = Transaction(6, 11); + var node = new ScenarioNode(); + FillWithDistinctValues(node); + node.Type = NodeType.Send; + node.TransactionJson = null; + node.TransactionName = transaction.PrimaryMessage.Name; + + var copy = ScenarioRun.Snapshot(new ScenarioGraph { Nodes = [node] }, [transaction]).Nodes.Single(); + + Assert.NotSame(node, copy); + Assert.NotNull(copy.TransactionJson); + foreach (var property in typeof(ScenarioNode).GetProperties().Where(p => p.Name != nameof(ScenarioNode.TransactionJson))) + Assert.Equal(property.GetValue(node), property.GetValue(copy)); + } + + /// + /// Snapshot copies exactly these. A property that a later engine package adds to ScenarioGraph would be dropped + /// without notice, so it has to show up here first. + /// + [Fact] + public void Snapshot_knows_every_property_of_a_graph() => + Assert.Equal(["Description", "Edges", "Name", "Nodes"], + typeof(ScenarioGraph).GetProperties().Select(property => property.Name).Order()); + + [Fact] + public void Copy_keeps_every_property_of_an_edge() + { + var edge = new ScenarioEdge(); + FillWithDistinctValues(edge); + + var copy = ScenarioRun.Snapshot(new ScenarioGraph { Edges = [edge] }, []).Edges.Single(); + + Assert.NotSame(edge, copy); + foreach (var property in typeof(ScenarioEdge).GetProperties()) + Assert.Equal(property.GetValue(edge), property.GetValue(copy)); + } + + /// + /// No library transaction is copied into nodes that carry their own, nodes whose TransactionName is a parameter + /// (Wait: ms), and nodes with a name the library does not know (the engine reports those). + /// + [Fact] + public void Other_nodes_keep_what_they_have() + { + var transaction = Transaction(6, 11); + var own = SecsGemTransactionJsonConverter.Serialize(Transaction(1, 13)); + var graph = new ScenarioGraph + { + Nodes = + [ + new ScenarioNode { Type = NodeType.Send, TransactionName = transaction.PrimaryMessage.Name, TransactionJson = own }, + new ScenarioNode { Type = NodeType.Wait, TransactionName = transaction.PrimaryMessage.Name }, + new ScenarioNode { Type = NodeType.Receive, TransactionName = "not in the library" }, + new ScenarioNode { Type = NodeType.Send, TransactionName = " " } + ] + }; + + var run = ScenarioRun.Snapshot(graph, [transaction]); + + Assert.Equal(own, run.Nodes[0].TransactionJson); + Assert.All(run.Nodes.Skip(1), node => Assert.Null(node.TransactionJson)); + Assert.Equal(graph.Nodes.Select(n => (n.Id, n.Type, n.TransactionName)), + run.Nodes.Select(n => (n.Id, n.Type, n.TransactionName))); + } + + /// + /// With branches running in parallel, an And node's "1/2 branches" report can arrive after its Succeeded report. + /// Only that case is dropped; every other report is shown. + /// + [Theory] + [InlineData(ScenarioNodeRunState.Succeeded, ScenarioNodeRunState.Waiting, true)] + [InlineData(ScenarioNodeRunState.Succeeded, ScenarioNodeRunState.Succeeded, false)] + [InlineData(ScenarioNodeRunState.Succeeded, ScenarioNodeRunState.Failed, false)] + [InlineData(ScenarioNodeRunState.Succeeded, ScenarioNodeRunState.Running, false)] + [InlineData(ScenarioNodeRunState.Waiting, ScenarioNodeRunState.Waiting, false)] + [InlineData(ScenarioNodeRunState.Running, ScenarioNodeRunState.Waiting, false)] + [InlineData(ScenarioNodeRunState.Idle, ScenarioNodeRunState.Waiting, false)] + [InlineData(ScenarioNodeRunState.Waiting, ScenarioNodeRunState.Succeeded, false)] + public void Only_a_waiting_report_after_success_is_stale(ScenarioNodeRunState shown, ScenarioNodeRunState reported, + bool stale) => + Assert.Equal(stale, ScenarioRun.IsStale(shown, new ScenarioNodeProgress("node", reported, "1/2 branches"))); + + /// + /// Sets every property to a value that differs from its default and from the other properties. A property type this + /// does not know fails the test: then the copy in ScenarioRun.Snapshot needs a look as well. + /// + private static void FillWithDistinctValues(object target) + { + var index = 0; + foreach (var property in target.GetType().GetProperties()) + { + index++; + Assert.True(property.CanWrite, $"{target.GetType().Name}.{property.Name} cannot be set: check how Snapshot copies it"); + object value = property.PropertyType switch + { + var t when t == typeof(string) => $"value {index} of {property.Name}", + var t when t == typeof(bool) => !(bool)property.GetValue(target)!, + var t when t == typeof(double) => 12.5 + index, + var t when t == typeof(NodeType) => NodeType.Receive, + var t => throw new InvalidOperationException( + $"{target.GetType().Name}.{property.Name} has the type {t}: extend this test and check Snapshot") + }; + property.SetValue(target, value); + } + } + + private static SecsGemTransaction Transaction(byte stream, byte function) => new() + { + PrimaryMessage = new MessageFactory(stream, function, true, "primary").Build(), + ReplyMessage = new MessageFactory(stream, (byte)(function + 1), false, "reply").Build() + }; + + /// A library that notes the thread it is read on. + private sealed class RecordingLibrary(params SecsGemTransaction[] transactions) : IEnumerable + { + public int EnumeratedOnThread { get; private set; } + + public IEnumerator GetEnumerator() + { + EnumeratedOnThread = Environment.CurrentManagedThreadId; + return ((IEnumerable)transactions).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + private sealed class RecordingEngine : IScenarioExecutionService + { + public ScenarioExecutionResult Result { get; } = new() { Success = true, CompletedSteps = 3 }; + public bool OnThreadPool { get; private set; } + public int ThreadId { get; private set; } + public SynchronizationContext? ContextAtStart { get; private set; } = new(); + public SynchronizationContext? ContextAfterAwait { get; private set; } = new(); + public ScenarioGraph? Graph { get; private set; } + public CancellationToken Token { get; private set; } + public ConcurrentDictionary? Consumed { get; private set; } + public TimeSpan? Deadline { get; private set; } + public IProgress? Progress { get; private set; } + + public async Task ExecuteAsync(ScenarioGraph scenario, + CancellationToken cancellation = default, + ConcurrentDictionary? consumedAcrossRuns = null, TimeSpan? deadline = null, + IProgress? progress = null) + { + OnThreadPool = Thread.CurrentThread.IsThreadPoolThread; + ThreadId = Environment.CurrentManagedThreadId; + ContextAtStart = SynchronizationContext.Current; + (Graph, Token, Consumed, Deadline, Progress) = (scenario, cancellation, consumedAcrossRuns, deadline, progress); + // Like the engine: no ConfigureAwait(false). + await Task.Delay(20, CancellationToken.None); + ContextAfterAwait = SynchronizationContext.Current; + return Result; + } + + public void Cancel() { } + } + + /// Its synchronous start takes a while and counts how many callers are in it at once. + private sealed class SlowStartEngine : IScenarioExecutionService + { + private int inside; + private int mostAtOnce; + private int starts; + + public int MostAtOnce => Volatile.Read(ref mostAtOnce); + public int Starts => Volatile.Read(ref starts); + + public Task ExecuteAsync(ScenarioGraph scenario, + CancellationToken cancellation = default, + ConcurrentDictionary? consumedAcrossRuns = null, TimeSpan? deadline = null, + IProgress? progress = null) + { + var now = Interlocked.Increment(ref inside); + Interlocked.Increment(ref starts); + int seen; + while ((seen = Volatile.Read(ref mostAtOnce)) < now && Interlocked.CompareExchange(ref mostAtOnce, now, seen) != seen) + { + } + + Thread.Sleep(150); + Interlocked.Decrement(ref inside); + return Task.FromResult(new ScenarioExecutionResult { Success = true }); + } + + public void Cancel() { } + } +} diff --git a/EvenBetterFastSim.Tests/TestSupport.cs b/EvenBetterFastSim.Tests/TestSupport.cs index 6093e32..0d68cda 100644 --- a/EvenBetterFastSim.Tests/TestSupport.cs +++ b/EvenBetterFastSim.Tests/TestSupport.cs @@ -4,6 +4,8 @@ using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; +using System.Threading; +using System.Windows.Threading; using EvenBetterFastSim.Services; using EvenBetterFastSim.WPF.ViewModels; using Microsoft.Extensions.Logging; @@ -99,3 +101,41 @@ private static string ResolveLibraryDir() @"Could not locate 'EvenBetterFastSim\Library' above " + AppContext.BaseDirectory); } } + +/// A thread with a running , like the WPF UI thread. +internal sealed class UiThread : IDisposable +{ + private readonly Thread thread; + + public UiThread() + { + using var started = new ManualResetEventSlim(); + Dispatcher? dispatcher = null; + thread = new Thread(() => + { + dispatcher = Dispatcher.CurrentDispatcher; + started.Set(); + Dispatcher.Run(); + }) { IsBackground = true, Name = "test UI thread" }; + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + if (!started.Wait(TimeSpan.FromSeconds(10))) throw new TimeoutException("The dispatcher thread did not start"); + Dispatcher = dispatcher!; + ThreadId = thread.ManagedThreadId; + } + + public Dispatcher Dispatcher { get; } + public int ThreadId { get; } + + /// + /// Runs on the UI thread once everything queued so far is done. A plain Invoke has the priority Send and + /// overtakes queued work such as log entries. + /// + public T AfterQueuedWork(Func read) => Dispatcher.Invoke(read, DispatcherPriority.ContextIdle); + + public void Dispose() + { + Dispatcher.InvokeShutdown(); + thread.Join(TimeSpan.FromSeconds(10)); + } +} diff --git a/EvenBetterFastSim/Logging/ObservableLogger.cs b/EvenBetterFastSim/Logging/ObservableLogger.cs index 114e0e5..7976e09 100644 --- a/EvenBetterFastSim/Logging/ObservableLogger.cs +++ b/EvenBetterFastSim/Logging/ObservableLogger.cs @@ -2,19 +2,23 @@ using Microsoft.Extensions.Logging; using System; using System.Windows; +using System.Windows.Threading; namespace EvenBetterFastSim.Logging; /// /// Logs messages to the UI /// -public class ObservableLogger(ILogService logService, string _) : ILogger +/// The UI thread's dispatcher at the time of a log call; by default the application's. +/// Without one (no : unit tests, shutdown) the entry is dropped. +public class ObservableLogger(ILogService logService, string _, Func? dispatcher = null) : ILogger { /// /// Gets the associated ObservableCollection of logged strings /// private readonly ILogService logService = logService; private readonly string _ = _; + private readonly Func dispatcher = dispatcher ?? (static () => Application.Current?.Dispatcher); public void Log(LogLevel logLevel, EventId eventId, @@ -43,10 +47,10 @@ public void Log(LogLevel logLevel, private void AddItemToCollection(LoggedString messageToLog) { - // Invoke the UI thread to add the message to the collection - Application.Current.Dispatcher.Invoke(() => - { - logService.LogMessages.Add(messageToLog); - }); + // Queued for the UI thread, like SecsMessageLogger; entries keep the order of their calls. With Invoke every + // caller waited for the UI thread: the scenario engine and the receive loop took as long as the UI was busy, + // and a caller that the UI thread itself was waiting for (a connection being closed) blocked for seconds + // (GitLab secsgem/evencoolerfastsim#26). + dispatcher()?.InvokeAsync(() => logService.LogMessages.Add(messageToLog)); } } \ No newline at end of file diff --git a/EvenBetterFastSim/Services/ScenarioRun.cs b/EvenBetterFastSim/Services/ScenarioRun.cs new file mode 100644 index 0000000..a1ce5bc --- /dev/null +++ b/EvenBetterFastSim/Services/ScenarioRun.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Logging.Interfaces; +using SecsGemBaseItems.Data_Containers; +using SecsGemBaseItems.Data_Containers.Serialization; +using SecsGemScenarioEngine.Models; +using SecsGemScenarioEngine.Services; + +namespace EvenBetterFastSim.Services; + +/// +/// Hands a scenario to the engine so that it runs on the thread pool instead of the WPF UI thread +/// (GitLab secsgem/evencoolerfastsim#26). +/// +/// +/// The engine awaits without ConfigureAwait(false). Started on the UI thread, every node therefore ran there: the +/// time between two nodes depended on what the UI was busy with, and a node that blocks froze the window. +/// +public static class ScenarioRun +{ + /// + /// One run enters the engine at a time. While it starts, the engine keeps the run's cancellation source in a field + /// and reads it back; two runs that start at the same instant on two threads could end up with each other's source + /// and ignore their own Stop and deadline (GitLab secsgem/secsgembase#69). On the UI thread that could not happen. + /// Only the synchronous start is covered, not the run. + /// + private static readonly Lock EngineStart = new(); + + /// + /// Takes a of the graph on the calling thread (the UI thread), then runs the engine with it on + /// the thread pool, where the engine has no SynchronizationContext to return to. An awaiting caller continues on its + /// own context as usual, and a created on the UI thread still reports there. + /// + /// + /// The token is not passed to Task.Run on purpose: a run that is cancelled before it starts still goes through the + /// engine, which reports it as cancelled like any other cancelled run. + /// + public static Task RunAsync( + IScenarioExecutionService engine, + ScenarioGraph graph, + IEnumerable library, + CancellationToken token, + ConcurrentDictionary? consumed, + TimeSpan? deadline, + IProgress? progress) + { + var run = Snapshot(graph, library); + return Task.Run(() => + { + lock (EngineStart) + return engine.ExecuteAsync(run, token, consumed, deadline, progress); + }); + } + + /// + /// The graph for one run: a copy that nothing else refers to, in which a Send or Receive node that only names its + /// transaction has its own copy of that library transaction. Call it on the UI thread. + /// + /// + /// + /// The stored graph keeps changing on the UI thread while a run is in flight: opening a scenario clears + /// of its nodes. The engine reads a node when it reaches it, so it gets + /// nodes of its own. + /// Nodes made on the canvas carry their transaction as TransactionJson. A node from an older file has only + /// ; the engine then looks the transaction up in the library and writes + /// response bindings into it. The library is bound to the UI, where such a write from another thread throws. With + /// the copy the engine does not touch the library for that node, and the run no longer changes the library + /// transaction. + /// A name the library does not know stays unresolved: the engine reports it when it reaches the node. For + /// that it still searches the library, from its own thread. + /// + /// + public static ScenarioGraph Snapshot(ScenarioGraph graph, IEnumerable library) + { + List? transactions = null; + var run = new ScenarioGraph + { + Name = graph.Name, + Description = graph.Description, + Edges = graph.Edges.Select(edge => new ScenarioEdge + { + SourceNodeId = edge.SourceNodeId, + TargetNodeId = edge.TargetNodeId, + IsFailurePath = edge.IsFailurePath + }).ToList() + }; + + foreach (var node in graph.Nodes) + { + var copy = new ScenarioNode + { + Id = node.Id, + Type = node.Type, + TransactionName = node.TransactionName, + DisplayName = node.DisplayName, + TransactionJson = node.TransactionJson, + UseReplyMessage = node.UseReplyMessage, + MatchConditionsJson = node.MatchConditionsJson, + ResponseBindingsJson = node.ResponseBindingsJson, + X = node.X, + Y = node.Y + }; + run.Nodes.Add(copy); + + if (node.Type is not (NodeType.Send or NodeType.SendAndWait or NodeType.Receive) || + !string.IsNullOrWhiteSpace(node.TransactionJson) || + string.IsNullOrWhiteSpace(node.TransactionName)) + continue; + + transactions ??= library.ToList(); + // Same match as the engine's own lookup. + var transaction = transactions.FirstOrDefault(t => + string.Equals(t.PrimaryMessage?.Name, node.TransactionName, StringComparison.OrdinalIgnoreCase)); + if (transaction is not null) + copy.TransactionJson = Serialize(transaction); + } + + return run; + } + + /// + /// A node keeps "Succeeded" for the rest of the run. The engine's branches really run in parallel now, so the report + /// of a branch that reached an And node early ("1/2 branches", Waiting) can come in after the report that the And + /// has fired (GitLab secsgem/secsgembase#69); the node would end the run with the hourglass. + /// + public static bool IsStale(ScenarioNodeRunState shown, ScenarioNodeProgress report) => + shown is ScenarioNodeRunState.Succeeded && report.State is ScenarioNodeRunState.Waiting; + + /// + /// The converter writes item values with the current culture but reads F4/F8 values with the invariant culture: with + /// a decimal comma, 1,5 came back as 15 (GitLab secsgem/secsgembase#68). Written invariant, the copy holds what the + /// library transaction holds. + /// + private static string Serialize(SecsGemTransaction transaction) + { + var culture = CultureInfo.CurrentCulture; + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + try + { + return SecsGemTransactionJsonConverter.Serialize(transaction); + } + finally + { + CultureInfo.CurrentCulture = culture; + } + } +} diff --git a/EvenBetterFastSim/WPF/ViewModels/ScenariosViewModel.cs b/EvenBetterFastSim/WPF/ViewModels/ScenariosViewModel.cs index b0fae40..77791e1 100644 --- a/EvenBetterFastSim/WPF/ViewModels/ScenariosViewModel.cs +++ b/EvenBetterFastSim/WPF/ViewModels/ScenariosViewModel.cs @@ -11,8 +11,6 @@ using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; -using EvenBetterFastSim.Logging; -using EvenBetterFastSim.Logging.Interfaces; using EvenBetterFastSim.WPF.ViewModels.Graph; using Microsoft.Extensions.Logging; using Microsoft.Win32; @@ -50,7 +48,6 @@ public partial class ScenariosViewModel : ObservableObject private readonly ScenarioExecutionService scenarioExecutionService; private readonly ISecsGemLibraryManager libraryManager; private readonly DataMessageHandler dataMessageHandler; - private readonly ILogService logService; private readonly ILogger logger; [ObservableProperty] @@ -90,7 +87,6 @@ public ScenariosViewModel( ScenarioExecutionService scenarioExecutionService, ISecsGemLibraryManager libraryManager, DataMessageHandler dataMessageHandler, - ILogService logService, ILogger logger, Services.IWindowManager windowManager, Services.ViewModelLocator viewModelLocator) @@ -98,7 +94,6 @@ public ScenariosViewModel( this.scenarioExecutionService = scenarioExecutionService; this.libraryManager = libraryManager; this.dataMessageHandler = dataMessageHandler; - this.logService = logService; this.logger = logger; this.windowManager = windowManager; this.viewModelLocator = viewModelLocator; @@ -128,11 +123,12 @@ public ScenariosViewModel( LoadFromJson(); } - private void LogInfo(string message) => - logService.LogMessages.Add(new LoggedString { Level = LogLevel.Information, Message = message }); + // Through the logger, like every other System Log entry: the logger queues its entries for the UI thread, and a line + // added to the collection directly would overtake engine entries that are still queued (after a cancel the line + // "cancelled by the user" came before the engine's "was cancelled"; GitLab secsgem/evencoolerfastsim#26). + private void LogInfo(string message) => logger.LogInformation("{Message}", message); - private void LogError(string message) => - logService.LogMessages.Add(new LoggedString { Level = LogLevel.Error, Message = message }); + private void LogError(string message) => logger.LogError("{Message}", message); /// /// When a run is cancelled while a Receive/Wait step is still pending, the execution service @@ -324,12 +320,17 @@ private async Task RunOnceAsync( : new Progress(p => { if (canvasNodes.FirstOrDefault(n => n.Id == p.NodeId) is not { } vm) return; + if (Services.ScenarioRun.IsStale(vm.RunState, p)) return; vm.RunState = p.State; vm.RunDetail = p.Detail; }); TimeSpan? deadline = RunTimeoutSeconds > 0 ? TimeSpan.FromSeconds(RunTimeoutSeconds) : null; - var result = await scenarioExecutionService.ExecuteAsync(graph, token, consumed, deadline, progress); + // The engine runs on the thread pool, so node timing does not depend on the UI and a blocking node cannot + // freeze it (GitLab secsgem/evencoolerfastsim#26). It gets its own copy of the graph, taken here on the UI + // thread; this method continues on the UI thread after the await. + var result = await Services.ScenarioRun.RunAsync( + scenarioExecutionService, graph, libraryManager.Library, token, consumed, deadline, progress); if (result.Success) LogInfo($"Scenario '{scenario.Name}' completed ({result.CompletedSteps} steps)"); diff --git a/specification/features.md b/specification/features.md index 0d249a5..25759ed 100644 --- a/specification/features.md +++ b/specification/features.md @@ -2,6 +2,29 @@ Related docs: [project_overview.md](project_overview.md), [secsgembase_library.md](secsgembase_library.md), [dialog_pattern.md](dialog_pattern.md), [SESSION_REFACTORING_LOG.md](../SESSION_REFACTORING_LOG.md) +## Scenario runs leave the UI thread + +A scenario (Run or a loop) now executes on the thread pool. Until then every node ran on the WPF UI thread, because +the engine awaits without `ConfigureAwait(false)`: the time between two nodes depended on what the UI was busy with, +and a node that blocks froze the window (internal board, issue 26). + +**Files:** `Services/ScenarioRun.cs` (`RunAsync`, `Snapshot`, `IsStale`), `WPF/ViewModels/ScenariosViewModel.cs` +(`RunOnceAsync`, `LogInfo`/`LogError`), `Logging/ObservableLogger.cs`. + +- `ScenarioRun.RunAsync` takes a `Snapshot` of the graph on the UI thread and starts the engine with `Task.Run`. + `RunOnceAsync` continues on the UI thread after the await; the `Progress` for the canvas is created there. +- The snapshot is a copy nothing else refers to. The stored graph changes on the UI thread during a run (opening a + scenario clears `TransactionJson` of its nodes), and a node from an older file that only names its transaction gets + a copy of the library transaction, so the engine does not read or write the UI-bound library for it. That copy is + written with the invariant culture (the SecsGemBase converter otherwise turns 1,5 into 15 on a decimal-comma system). +- The System Log logger (`ObservableLogger`) queues its entries with `Dispatcher.InvokeAsync`, like + `SecsMessageLogger`, instead of waiting for the UI thread. `ScenariosViewModel` writes its own lines through the + logger too, so they keep their place between the engine's lines. Without a dispatcher (unit tests, shutdown) an + entry is dropped; pass a dispatcher provider as third constructor argument to observe entries in a test. +- Two things the engine did not need on a single thread: runs enter the engine one at a time (it keeps the run's + cancellation source in a field while it starts), and an And node's late "1/2 branches" report no longer replaces + its Succeeded state. + ## Responder panel — edit the message shape The node responder dialog (`NodeResponderView`, opened by double-click on a Send / Receive node) now has a