using System; using System.Collections.Generic; namespace Ariadne.UnitTests { /// /// Offers a passage that pauses until its gate opens, then hands back a span. /// public partial class DelayWorkerScript : Arc { public TimeSpan Ask(ArcTests.Gates gates) { ask: while (!gates.Open.Contains("answer")) { Yield(); } return TimeSpan.FromSeconds(2); } protected override void Run(ArcTests.Gates gates) { } } /// /// Waits for as long as a passage of another arc says. /// public partial class ForeignAskWaitScript : Arc { [Scratch] public readonly List Log = new List(); public DelayWorkerScript Delay = new DelayWorkerScript(); protected override void Run(ArcTests.Gates gates) { first: Wait(Delay.Ask(gates)); Log.Add("done"); } } /// /// Uses what a passage of another arc hands back in a bigger statement, with a local read after the call. /// public partial class ForeignExpressionScript : Arc { [Scratch] public readonly List Log = new List(); public WorkerScript Worker = new WorkerScript(); protected override void Run(ArcTests.Gates gates) { int before = 4; first: Log.Add("total " + (before + Worker.Add(gates, 5))); } } /// /// Offers a passage that pauses until a gate it is given opens. /// public partial class GatedWorkerScript : Arc { public int Total; public int Add(ArcTests.Gates gates, string gate, int amount) { wait: while (!gates.Open.Contains(gate)) { Yield(); } Total += amount; return Total; } protected override void Run(ArcTests.Gates gates) { } } /// /// Adds what two passages of two arcs hand back, which wait on different gates. /// public partial class GatedSumScript : Arc { [Scratch] public readonly List Log = new List(); public GatedWorkerScript First = new GatedWorkerScript(); public GatedWorkerScript Second = new GatedWorkerScript(); protected override void Run(ArcTests.Gates gates) { sum: Log.Add("sum " + (First.Add(gates, "a", 5) + Second.Add(gates, "b", 7))); } } /// /// Adds what two passages of two arcs hand back. /// public partial class TwoForeignCallsScript : Arc { [Scratch] public readonly List Log = new List(); public WorkerScript First = new WorkerScript(); public WorkerScript Second = new WorkerScript(); protected override void Run(ArcTests.Gates gates) { sum: Log.Add("sum " + (First.Add(gates, 5) + Second.Add(gates, 7))); } } }