private IEnumerable<Event> Car(string name, Environment env, Resource gasStation, Container fuelPump) { /* * A car arrives at the gas station for refueling. * * It requests one of the gas station's fuel pumps and tries to get the * desired amount of gas from it. If the stations reservoir is * depleted, the car has to wait for the tank truck to arrive. */ var fuelTankLevel = env.RandUniform(MinFuelTankLevel, MaxFuelTankLevel + 1); env.Log("{0} arriving at gas station at {1}", name, env.Now); using (var req = gasStation.Request()) { var start = env.Now; // Request one of the gas pumps yield return req; // Get the required amount of fuel var litersRequired = FuelTankSize - fuelTankLevel; yield return fuelPump.Get(litersRequired); // The "actual" refueling process takes some time yield return env.Timeout(TimeSpan.FromSeconds(litersRequired / RefuelingSpeed)); env.Log("{0} finished refueling in {1} seconds.", name, (env.Now - start).TotalSeconds); } }
private IEnumerable<Event> TestContainerPutter(Environment env, Container buf, List<Tuple<char, DateTime>> log) { yield return env.Timeout(TimeSpan.FromSeconds(1)); while (true) { yield return buf.Put(2); log.Add(Tuple.Create('p', env.Now)); yield return env.Timeout(TimeSpan.FromSeconds(1)); } }
private IEnumerable<Event> TestDiscreteTimeSteps(Environment env, List<DateTime> log) { while (true) { log.Add(env.Now); yield return env.Timeout(TimeSpan.FromSeconds(1)); } }
public void TestRunResume() { /* Stopped simulation can be resumed. */ var env = new Environment(); var events = new List<Event>() { env.Timeout(TimeSpan.FromMinutes(5)), env.Timeout(TimeSpan.FromMinutes(10)), env.Timeout(TimeSpan.FromMinutes(15)), env.Timeout(TimeSpan.FromMinutes(20)) }; Assert.AreEqual(env.Now, new DateTime(1970, 1, 1)); Assert.AreEqual(env.Peek(), new DateTime(1970, 1, 1, 0, 5, 0)); Assert.IsFalse(events.Any(x => x.IsProcessed)); env.Run(TimeSpan.FromMinutes(10)); Assert.AreEqual(env.Now, new DateTime(1970, 1, 1, 0, 10, 0)); Assert.AreEqual(env.Peek(), new DateTime(1970, 1, 1, 0, 10, 0)); Assert.IsTrue(events[0].IsProcessed); Assert.IsFalse(events[1].IsProcessed); Assert.IsFalse(events[2].IsProcessed); env.Run(TimeSpan.FromMinutes(5)); Assert.AreEqual(env.Now, new DateTime(1970, 1, 1, 0, 15, 0)); Assert.AreEqual(env.Peek(), new DateTime(1970, 1, 1, 0, 15, 0)); Assert.IsTrue(events[0].IsProcessed); Assert.IsTrue(events[1].IsProcessed); Assert.IsFalse(events[2].IsProcessed); env.Run(TimeSpan.FromMinutes(1)); Assert.AreEqual(env.Now, new DateTime(1970, 1, 1, 0, 16, 0)); Assert.AreEqual(env.Peek(), new DateTime(1970, 1, 1, 0, 20, 0)); Assert.IsTrue(events[0].IsProcessed); Assert.IsTrue(events[1].IsProcessed); Assert.IsTrue(events[2].IsProcessed); env.Run(); Assert.AreEqual(env.Now, new DateTime(1970, 1, 1, 0, 20, 0)); Assert.AreEqual(env.Peek(), DateTime.MaxValue); }
public void TestSharedTimeout() { var start = new DateTime(2014, 4, 1); var env = new Environment(start); var timeout = env.Timeout(TimeSpan.FromSeconds(1)); var log = new Dictionary<int, DateTime>(); for (int i = 0; i < 3; i++) env.Process(TestSharedTimeout(env, timeout, i, log)); env.Run(); Assert.AreEqual(3, log.Count); foreach (var l in log.Values) Assert.AreEqual(start + TimeSpan.FromSeconds(1), l); }
private IEnumerable<Event> TestConditionWithUncaughtError_Process(Environment env) { yield return env.Timeout(TimeSpan.FromDays(1)) | env.Process(TestConditionWithUncaughtError_Explode(env, 2)); }
private IEnumerable<Event> TestConditionWithUncaughtError_Explode(Environment env, int delay) { yield return env.Timeout(TimeSpan.FromDays(delay)); env.ActiveProcess.Fail(); }
private IEnumerable<Event> FilterStoreConsumerB(Environment env, FilterStore sto) { while (true) { yield return sto.Get(x => x == FilterStoreObjB); env.Log("{0}: Consume B", env.Now.Second); yield return env.Timeout(TimeSpan.FromSeconds(3)); } }
private IEnumerable<Event> FilterStoreProducer(Environment env, FilterStore sto) { while (true) { yield return env.Timeout(TimeSpan.FromSeconds(4)); yield return sto.Put(FilterStoreObjA); env.Log("{0}: Produce A", env.Now.Second); yield return env.Timeout(TimeSpan.FromSeconds(2)); yield return sto.Put(FilterStoreObjB); env.Log("{0}: Produce B", env.Now.Second); } }
private IEnumerable<Event> TestMixedPreemtion(int id, Environment env, PreemptiveResource res, int delay, int prio, bool preempt, List<Tuple<int, int>> log) { yield return env.Timeout(TimeSpan.FromSeconds(delay)); using (var req = res.Request(priority: prio, preempt: preempt)) { yield return req; yield return env.Timeout(TimeSpan.FromSeconds(5)); if (!env.ActiveProcess.HandleFault()) log.Add(Tuple.Create(env.Now.Second, id)); } }
private IEnumerable<Event> TestResourceSlots(Environment env, string name, Resource resource, Dictionary<string, DateTime> log) { using (var req = resource.Request()) { yield return req; log.Add(name, env.Now); yield return env.Timeout(TimeSpan.FromSeconds(1)); } }
private IEnumerable<Event> TestResourceImmediateRequests_Child(Environment env, Resource res) { var result = new List<int>(); for (var i = 0; i < 3; i++) { using (var req = res.Request()) { yield return req; result.Add(env.Now.Second); yield return env.Timeout(TimeSpan.FromSeconds(1)); } } env.ActiveProcess.Succeed(result); }
private IEnumerable<Event> TestResourceReleaseAfterInterruptBlocker(Environment env, Resource res) { using (var req = res.Request()) { yield return req; yield return env.Timeout(TimeSpan.FromSeconds(1)); } }
private IEnumerable<Event> TestResourceContinueAfterInterruptWaitingVictim(Environment env, Resource res) { var req = res.Request(); yield return req; Assert.IsFalse(req.IsOk); env.ActiveProcess.HandleFault(); yield return env.Timeout(TimeSpan.FromSeconds(2)); yield return req; res.Release(req); Assert.AreEqual(new DateTime(2014, 4, 1, 0, 0, 2), env.Now); }
private IEnumerable<Event> TestPreemtiveResource(int id, Environment env, PreemptiveResource res, int delay, int prio, Dictionary<DateTime, int> log) { yield return env.Timeout(TimeSpan.FromSeconds(delay)); using (var req = res.Request(priority: prio, preempt: true)) { yield return req; yield return env.Timeout(TimeSpan.FromSeconds(5)); if (!env.ActiveProcess.HandleFault()) log.Add(env.Now, id); } }
private IEnumerable<Event> TestPreemptiveResourceTimeoutA(Environment env, PreemptiveResource res, int prio) { using (var req = res.Request(priority: prio, preempt: true)) { yield return req; Assert.IsTrue(env.ActiveProcess.HandleFault()); yield return env.Timeout(TimeSpan.FromSeconds(1)); Assert.IsFalse(env.ActiveProcess.HandleFault()); } }
private IEnumerable<Event> TestOperatorOr(Environment env) { var timeout = new List<Event>(); for (int i = 0; i < 3; i++) timeout.Add(env.Timeout(TimeSpan.FromSeconds(i))); yield return timeout[0] | timeout[1] | timeout[2]; Assert.IsTrue(timeout[0].IsProcessed); Assert.IsFalse(timeout[1].IsProcessed); Assert.IsFalse(timeout[2].IsProcessed); }
private IEnumerable<Event> TestResourceWithCondition(Environment env, Resource res) { using (var req = res.Request()) { var timeout = env.Timeout(TimeSpan.FromSeconds(1)); yield return req | timeout; Assert.IsTrue(req.IsOk); Assert.IsFalse(timeout.IsProcessed); } }
private static IEnumerable<Event> AProcess(Environment env, List<string> log) { while (env.Now < new DateTime(1970, 1, 1, 0, 2, 0)) { log.Add(env.Now.ToString("mm")); yield return env.Timeout(TimeSpan.FromMinutes(1)); } }
private IEnumerable<Event> TestNegativeTimeout(Environment env) { yield return env.Timeout(TimeSpan.FromSeconds(-1)); }
private IEnumerable<Event> TestTriggeredTimeout(Environment env) { var @event = env.Timeout(TimeSpan.FromSeconds(1)); // Start the child after the timeout already happened yield return env.Timeout(TimeSpan.FromSeconds(2)); yield return env.Process(TestTriggeredTimeoutChild(env, @event)); }
private IEnumerable<Event> GasStationControl(Environment env, Container fuelPump) { /* * Periodically check the level of the *fuel_pump* and call the tank * truck if the level falls below a threshold. */ while (true) { if (fuelPump.Level / fuelPump.Capacity * 100 < Threshold) { // We need to call the tank truck now! env.Log("Calling tank truck at {0}", env.Now); // Wait for the tank truck to arrive and refuel the station yield return env.Process(TankTruck(env, fuelPump)); } yield return env.Timeout(TimeSpan.FromSeconds(10)); // Check every 10 seconds } }
private IEnumerable<Event> TestResourceWithPriorityQueue(Environment env, int delay, PriorityResource resource, int priority, int resTime) { yield return env.Timeout(TimeSpan.FromSeconds(delay)); var req = resource.Request(priority); yield return req; Assert.AreEqual(new DateTime(2014, 4, 1) + TimeSpan.FromSeconds(resTime), env.Now); yield return env.Timeout(TimeSpan.FromSeconds(5)); resource.Release(req); }
private IEnumerable<Event> TestFilterStoreGetAfterMismatch_Putter(Environment env, FilterStore store) { yield return store.Put(2); yield return env.Timeout(TimeSpan.FromSeconds(1)); yield return store.Put(1); }
private IEnumerable<Event> CarGenerator(Environment env, Resource gasStation, Container fuelPump) { // Generate new cars that arrive at the gas station. var i = 0; while (true) { i++; yield return env.Timeout(env.RandUniform(MinTInter, MaxTInter)); env.Process(Car("Car " + i, env, gasStation, fuelPump)); } }
private IEnumerable<Event> TestResourceWithUsing(Environment env, string name, Resource resource, Dictionary<string, DateTime> log) { using (var req = resource.Request()) { yield return req; Assert.AreEqual(1, resource.InUse); yield return env.Timeout(TimeSpan.FromSeconds(1)); } log.Add(name, env.Now); }
private IEnumerable<Event> TankTruck(Environment env, Container fuelPump) { // Arrives at the gas station after a certain delay and refuels it. yield return env.Timeout(TankTruckTime); env.Log("Tank truck arriving at time {0}", env.Now); var amount = fuelPump.Capacity - fuelPump.Level; env.Log("Tank truck refuelling {0} liters.", amount); yield return fuelPump.Put(amount); }
private IEnumerable<Event> TestConditionWithError_Explode(Environment env, TimeSpan delay) { var timeout = env.Timeout(delay); yield return timeout; env.ActiveProcess.Fail(string.Format("Onoes, failed after {0} delay!", delay.Ticks)); }
private IEnumerable<Event> TestConditionWithError_Process(Environment env) { var proc = env.Process(TestConditionWithError_Explode(env, TimeSpan.FromSeconds(0))); yield return proc | env.Timeout(TimeSpan.FromSeconds(1)); Assert.IsTrue(!proc.IsOk); Assert.AreEqual(proc.Value, "Onoes, failed after 0 delay!"); env.ActiveProcess.HandleFault(); }
private IEnumerable<Event> WaitForProcFinisher(Environment env) { yield return env.Timeout(TimeSpan.FromSeconds(5)); }