コード例 #1
0
        public void TestGetConditions()
        {
            // Initialize
            WaitSet        waitSet             = new WaitSet();
            GuardCondition guardCondition      = new GuardCondition();
            GuardCondition otherGuardCondition = new GuardCondition();
            ReturnCode     result = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);
            result = waitSet.AttachCondition(otherGuardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null parameter
            result = waitSet.GetConditions(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with correct parameter
            List <Condition> conditions = new List <Condition>()
            {
                null
            };

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(2, conditions.Count);
            Assert.IsNotNull(conditions[0]);
            Assert.IsNotNull(conditions[1]);
            Assert.IsTrue(conditions[0] != conditions[1]);
            Assert.IsTrue(guardCondition == conditions[0] || guardCondition == conditions[1]);
            Assert.IsTrue(otherGuardCondition == conditions[0] || otherGuardCondition == conditions[1]);
        }
コード例 #2
0
        public void TestAttachCondition()
        {
            // Initialize
            WaitSet        waitSet        = new WaitSet();
            GuardCondition guardCondition = new GuardCondition();

            // Test with null parameter
            ReturnCode result = waitSet.AttachCondition(null);

            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with correct parameter
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Attach again should return OK but not actually add another condition to the WaitSet
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            List <Condition> conditions = new List <Condition>();

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(1, conditions.Count);
            Assert.AreEqual(guardCondition, conditions[0]);
        }
コード例 #3
0
        public void TestDetachConditions()
        {
            // Initialize
            WaitSet        waitSet               = new WaitSet();
            GuardCondition guardCondition        = new GuardCondition();
            GuardCondition otherGuardCondition   = new GuardCondition();
            GuardCondition anotherGuardCondition = new GuardCondition();
            ReturnCode     result = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);
            result = waitSet.AttachCondition(otherGuardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);
            result = waitSet.AttachCondition(anotherGuardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null parameter
            result = waitSet.DetachConditions(null);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with empty list parameter
            List <Condition> conditions = new List <Condition>();

            result = waitSet.DetachConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with single element list parameter
            conditions = new List <Condition>()
            {
                guardCondition
            };
            result = waitSet.DetachConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);

            conditions = new List <Condition>()
            {
                null
            };
            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(2, conditions.Count);
            Assert.IsNotNull(conditions[0]);
            Assert.IsNotNull(conditions[1]);
            Assert.IsTrue(conditions[0] != conditions[1]);
            Assert.IsTrue(anotherGuardCondition == conditions[0] || anotherGuardCondition == conditions[1]);
            Assert.IsTrue(otherGuardCondition == conditions[0] || otherGuardCondition == conditions[1]);

            // Detach the other conditions
            conditions = new List <Condition>()
            {
                otherGuardCondition, anotherGuardCondition
            };
            result = waitSet.DetachConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);

            conditions = new List <Condition>()
            {
                null
            };
            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(0, conditions.Count);

            // Detach a not attached conditions
            GuardCondition notAttachedCondition = new GuardCondition();

            conditions = new List <Condition>()
            {
                notAttachedCondition
            };
            result = waitSet.DetachConditions(conditions);
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);
        }
コード例 #4
0
        public void TestWait()
        {
            // Initialize
            WaitSet        waitSet        = new WaitSet();
            GuardCondition guardCondition = new GuardCondition();
            ReturnCode     result         = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null conditions
            result = waitSet.Wait(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Attach again should return OK but not actually add another condition to the WaitSet
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            List <Condition> conditions = new List <Condition>();

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(1, conditions.Count);
            Assert.AreEqual(guardCondition, conditions[0]);

            // Test thread wait infinite
            int    count  = 0;
            Thread thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions);
                Assert.AreEqual(ReturnCode.Ok, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(1, conditions.Count);
                Assert.AreEqual(guardCondition, conditions[0]);
                guardCondition.TriggerValue = false;
                count++;
            });

            thread.Start();

            guardCondition.TriggerValue = true;
            thread.Join();
            Assert.AreEqual(1, count);

            // Test timeout
            count  = 0;
            thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions, new Duration {
                    Seconds = 0, NanoSeconds = 100000000
                });
                Assert.AreEqual(ReturnCode.Timeout, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(0, conditions.Count);
                count++;
            });
            thread.Start();
            thread.Join();
            Assert.AreEqual(1, count);

            // Test exit before timeout
            count  = 0;
            thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions, new Duration {
                    Seconds = 5
                });
                Assert.AreEqual(ReturnCode.Ok, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(1, conditions.Count);
                Assert.AreEqual(guardCondition, conditions[0]);
                Assert.IsTrue(guardCondition.TriggerValue);
                guardCondition.TriggerValue = false;
                count++;
            });
            thread.Start();

            guardCondition.TriggerValue = true;
            thread.Join();
            Assert.AreEqual(1, count);
        }