public void CanComplete_BeforeAndAfterEvent_ReturnsTrueOnlyAfterEvent()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess:true)){

                var waitInstruction = new WaitNotificationInstruction<object>();
                var process = new InstructionListTestProcess(new List<InstructionBase>(){ waitInstruction});

                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent = new TestNotification();
                var raiseInstruction = new RaiseNotificationInstruction<object>(testEvent);

                long? nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
            }
        }
        public void CanComplete_BeforeAndAfterEvent_ReturnsTrueOnlyAfterEvent()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess: true)){
                var waitInstruction = new WaitNotificationInstruction <object>();
                var process         = new InstructionListTestProcess(new List <InstructionBase>()
                {
                    waitInstruction
                });

                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent        = new TestNotification();
                var raiseInstruction = new RaiseNotificationInstruction <object>(testEvent);

                long?nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
            }
        }
        public void CanComplete_BeforeAndAfterEventWithCondition_ReturnsTrueOnlyAfterEventMatchingCondition()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess: true)){
                var waitInstruction = new WaitNotificationInstruction <TestNotification>((e) => e.Data > 0);
                var process         = new InstructionListTestProcess(new List <InstructionBase>()
                {
                    waitInstruction
                });
                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent1 = new TestNotification()
                {
                    Data = 0
                };
                var raiseInstruction1 = new RaiseNotificationInstruction <TestNotification>(testEvent1);

                var testEvent2 = new TestNotification()
                {
                    Data = 1
                };
                var raiseInstruction2 = new RaiseNotificationInstruction <TestNotification>(testEvent2);

                long?nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction1.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction2.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
                Assert.AreEqual(1, waitInstruction.Notifications.Count);
                Assert.IsTrue(waitInstruction.Notifications.Contains(testEvent2));
            }
        }
示例#4
0
        public override IEnumerator <InstructionBase> Simulate()
        {
            Console.WriteLine($"Going to sleep at time period {Context.TimePeriod}");

            // wait till the alarm rings
            yield return(new WaitNotificationInstruction <AlarmRingingNotification>());

            Console.WriteLine($"Alarm ringing at time period {Context.TimePeriod}");
            Console.WriteLine($"Going back to sleep at time period {Context.TimePeriod}");

            // go back to sleep and wait till it rings again
            yield return(new WaitNotificationInstruction <AlarmRingingNotification>());

            Console.WriteLine($"Alarm ringing again..waking up at time period {Context.TimePeriod}");

            // notify now awake
            var notification = new AwakeNotification();

            yield return(RaiseNotificationInstruction.New(notification));
        }
        public void CanComplete_BeforeAndAfterEventWithCondition_ReturnsTrueOnlyAfterEventMatchingCondition()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess:true)){

                var waitInstruction = new WaitNotificationInstruction<TestNotification>((e)=>e.Data > 0);
                var process = new InstructionListTestProcess(new List<InstructionBase>(){ waitInstruction});
                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent1 = new TestNotification() { Data = 0 };
                var raiseInstruction1 = new RaiseNotificationInstruction<TestNotification>(testEvent1);

                var testEvent2 = new TestNotification() { Data = 1 };
                var raiseInstruction2 = new RaiseNotificationInstruction<TestNotification>(testEvent2);

                long? nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction1.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction2.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
                Assert.AreEqual(1,waitInstruction.Notifications.Count);
                Assert.IsTrue(waitInstruction.Notifications.Contains(testEvent2));
            }
        }
示例#6
0
        public override IEnumerator <InstructionBase> Simulate()
        {
            var notification = new AlarmRingingNotification();

            yield return(RaiseNotificationInstruction.New(notification));
        }