Beispiel #1
0
        public void TestAssertLiveliness()
        {
            // Initialize entities
            DataWriterQos qos = new DataWriterQos();

            qos.Liveliness.Kind          = LivelinessQosPolicyKind.ManualByTopicLivelinessQos;
            qos.Liveliness.LeaseDuration = new Duration
            {
                Seconds = 1,
            };
            DataWriter writer = _publisher.CreateDataWriter(_topic, qos);

            Assert.IsNotNull(writer);

            // Manually assert liveliness
            for (int i = 0; i < 5; i++)
            {
                ReturnCode assertResult = writer.AssertLiveliness();
                Assert.AreEqual(ReturnCode.Ok, assertResult);
                System.Threading.Thread.Sleep(500);
            }

            //// Check that no liveliness has been lost
            LivelinessLostStatus status = default;
            ReturnCode           result = writer.GetLivelinessLostStatus(ref status);

            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(0, status.TotalCount);
            Assert.AreEqual(0, status.TotalCountChange);
        }
        public void TestOnLivelinessChanged()
        {
            // Attach to the event
            int count = 0;

            _listener.LivelinessChanged += (r, s) =>
            {
                Assert.AreEqual(_reader, r);

                if (count == 0)
                {
                    // Liveliness alive
                    Assert.AreEqual(1, s.AliveCount);
                    Assert.AreEqual(1, s.AliveCountChange);
                    Assert.AreEqual(0, s.NotAliveCount);
                    Assert.AreEqual(0, s.NotAliveCountChange);
                    Assert.AreEqual(_writer.InstanceHandle, s.LastPublicationHandle);
                }
                else
                {
                    // Liveliness not alive
                    Assert.AreEqual(0, s.AliveCount);
                    Assert.AreEqual(-1, s.AliveCountChange);
                    Assert.AreEqual(1, s.NotAliveCount);
                    Assert.AreEqual(1, s.NotAliveCountChange);
                    Assert.AreEqual(_writer.InstanceHandle, s.LastPublicationHandle);
                }

                count++;
            };

            // Prepare the QoS for the test
            DataReaderQos drQos = new DataReaderQos();

            drQos.Liveliness.LeaseDuration = new Duration {
                Seconds = 1
            };
            ReturnCode result = _reader.SetQos(drQos);

            Assert.AreEqual(ReturnCode.Ok, result);

            DataWriterQos dwQos = new DataWriterQos();

            dwQos.Liveliness.Kind          = LivelinessQosPolicyKind.ManualByTopicLivelinessQos;
            dwQos.Liveliness.LeaseDuration = new Duration {
                Seconds = 1
            };
            result = _writer.SetQos(dwQos);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Enable entities
            result = _writer.Enable();
            Assert.AreEqual(ReturnCode.Ok, result);

            result = _reader.Enable();
            Assert.AreEqual(ReturnCode.Ok, result);

            // Wait for discovery
            bool found = _writer.WaitForSubscriptions(1, 1000);

            Assert.IsTrue(found);

            // Assert liveliness in the writer
            result = _writer.AssertLiveliness();
            Assert.AreEqual(ReturnCode.Ok, result);

            // After half second liveliness should not be lost yet
            System.Threading.Thread.Sleep(500);
            Assert.AreEqual(1, count);

            // After one second and a half one liveliness should be lost
            System.Threading.Thread.Sleep(1000);
            Assert.AreEqual(2, count);

            // Remove the listener to avoid extra messages
            result = _participant.SetListener(null);
            Assert.AreEqual(ReturnCode.Ok, result);
        }
Beispiel #3
0
        public void TestOnLivelinessChanged()
        {
            int count = 0;

            using (ManualResetEventSlim evt1 = new ManualResetEventSlim(false))
                using (ManualResetEventSlim evt2 = new ManualResetEventSlim(false))
                {
                    DataReader     firstDataReader            = null;
                    int            firstAliveCount            = 0;
                    int            firstAliveCountChange      = 0;
                    int            firstNotAliveCount         = 1;
                    int            firstNotAliveCountChange   = 1;
                    InstanceHandle firstLastPublicationHandle = InstanceHandle.HandleNil;

                    DataReader     secondDataReader            = null;
                    int            secondAliveCount            = 1;
                    int            secondAliveCountChange      = 1;
                    int            secondNotAliveCount         = 0;
                    int            secondNotAliveCountChange   = 0;
                    InstanceHandle secondLastPublicationHandle = InstanceHandle.HandleNil;

                    // Attach to the event
                    _listener.LivelinessChanged += (r, s) =>
                    {
                        if (count == 0)
                        {
                            // Liveliness alive
                            firstDataReader            = r;
                            firstAliveCount            = s.AliveCount;
                            firstAliveCountChange      = s.AliveCountChange;
                            firstNotAliveCount         = s.NotAliveCount;
                            firstNotAliveCountChange   = s.NotAliveCountChange;
                            firstLastPublicationHandle = s.LastPublicationHandle;
                            evt1.Set();
                        }
                        else
                        {
                            // Liveliness not alive
                            secondDataReader            = r;
                            secondAliveCount            = s.AliveCount;
                            secondAliveCountChange      = s.AliveCountChange;
                            secondNotAliveCount         = s.NotAliveCount;
                            secondNotAliveCountChange   = s.NotAliveCountChange;
                            secondLastPublicationHandle = s.LastPublicationHandle;
                            evt2.Set();
                        }

                        count++;
                    };

                    // Prepare the QoS for the test
                    DataReaderQos drQos = new DataReaderQos();
                    drQos.Liveliness.LeaseDuration = new Duration {
                        Seconds = 1
                    };
                    ReturnCode result = _reader.SetQos(drQos);
                    Assert.AreEqual(ReturnCode.Ok, result);

                    DataWriterQos dwQos = new DataWriterQos();
                    dwQos.Liveliness.Kind          = LivelinessQosPolicyKind.ManualByTopicLivelinessQos;
                    dwQos.Liveliness.LeaseDuration = new Duration {
                        Seconds = 1
                    };
                    result = _writer.SetQos(dwQos);
                    Assert.AreEqual(ReturnCode.Ok, result);

                    // Enable entities
                    result = _writer.Enable();
                    Assert.AreEqual(ReturnCode.Ok, result);

                    result = _reader.Enable();
                    Assert.AreEqual(ReturnCode.Ok, result);

                    // Wait for discovery
                    bool found = _writer.WaitForSubscriptions(1, 1000);
                    Assert.IsTrue(found);
                    found = _reader.WaitForPublications(1, 1000);
                    Assert.IsTrue(found);

                    // Assert liveliness in the writer
                    result = _writer.AssertLiveliness();
                    Assert.AreEqual(ReturnCode.Ok, result);

                    // Check alive reader
                    Assert.IsTrue(evt1.Wait(5000));
                    Assert.AreEqual(1, count);
                    Assert.IsNotNull(firstDataReader);
                    Assert.AreEqual(_reader, firstDataReader);
                    Assert.AreEqual(1, firstAliveCount);
                    Assert.AreEqual(1, firstAliveCountChange);
                    Assert.AreEqual(0, firstNotAliveCount);
                    Assert.AreEqual(0, firstNotAliveCountChange);
                    Assert.AreEqual(_writer.InstanceHandle, firstLastPublicationHandle);

                    // Check lost reader
                    Assert.IsTrue(evt2.Wait(5000));
                    Assert.AreEqual(2, count);
                    Assert.IsNotNull(secondDataReader);
                    Assert.AreEqual(_reader, secondDataReader);
                    Assert.AreEqual(0, secondAliveCount);
                    Assert.AreEqual(-1, secondAliveCountChange);
                    Assert.AreEqual(1, secondNotAliveCount);
                    Assert.AreEqual(1, secondNotAliveCountChange);
                    Assert.AreEqual(_writer.InstanceHandle, secondLastPublicationHandle);

                    // Remove the listener to avoid extra messages
                    result = _reader.SetListener(null);
                    Assert.AreEqual(ReturnCode.Ok, result);
                }
        }