Opc.Ua.ServerTest.SessionTest.DoReconnectTest C# (CSharp) Method

DoReconnectTest() private method

Tests the session reconnect.
private DoReconnectTest ( ) : bool
return bool
        private bool DoReconnectTest()
        {
            double increment = MaxProgress/6;
            double position  = 0;

            bool success = true;
            
            lock (m_messages)
            {
                m_messages.Clear();
            }

            int currentKeepAlive = Session.KeepAliveInterval;
            List<Subscription> subscriptions = new List<Subscription>();
            KeepAliveEventHandler keepAliveHandler = new KeepAliveEventHandler(Session_Reconnect);
            NotificationEventHandler notificationHandler = new NotificationEventHandler(Session_Notification);

            try
            {
                Session.KeepAlive += keepAliveHandler;
                Session.Notification += notificationHandler;

                for (int publishingInterval = 1000; publishingInterval <= 10000; publishingInterval += 1000)
                {
                    Subscription subscription = new Subscription();

                    subscription.MaxMessageCount = 100;
                    subscription.LifetimeCount = 100;
                    subscription.KeepAliveCount = 10;
                    subscription.PublishingEnabled = true;
                    subscription.PublishingInterval = publishingInterval;

                    MonitoredItem monitoredItem = new MonitoredItem();

                    monitoredItem.StartNodeId = VariableIds.Server_ServerStatus_CurrentTime;
                    monitoredItem.AttributeId = Attributes.Value;
                    monitoredItem.SamplingInterval = -1;
                    monitoredItem.QueueSize = 0;
                    monitoredItem.DiscardOldest = true;

                    subscription.AddItem(monitoredItem);
                    Session.AddSubscription(subscription);
                    subscription.Create();
                    subscriptions.Add(subscription);
                }

                m_keepAliveCount = 0;
                Session.KeepAliveInterval = 1000;
                Log("Setting keep alive interval to {0}ms.", Session.KeepAliveInterval);

                int testDuration = 3000;

                for (int ii = 0; ii < 6; ii++)
                {
                    Session.Reconnect();

                    Log("Session reconnected. KeepAlives={0}", m_keepAliveCount);

                    if (m_errorEvent.WaitOne(testDuration, false))
                    {
                        Log("Unexpected error waiting for session keep alives. {0}", m_error.ToLongString());
                        return false;
                    }

                    position += increment;
                    ReportProgress(position);
                }
            }
            finally
            {
                Session.RemoveSubscriptions(subscriptions);
                Session.KeepAliveInterval = currentKeepAlive;
                Session.KeepAlive -= keepAliveHandler;
                Session.Notification -= notificationHandler;
            } 
                
            ReportProgress(MaxProgress);

            lock (m_messages)
            {
                foreach (KeyValuePair<uint,List<uint>> entry in m_messages)
                {
                    entry.Value.Sort();

                    for (int ii = 0; ii < entry.Value.Count-1; ii++)
                    {
                        if (entry.Value[ii+1] - entry.Value[ii] > 1)
                        {
                            Log("Missing message. Subscription={0}, SequenceNumber={1}-{2}", entry.Key, entry.Value[ii]+1, entry.Value[ii+1]-1);
                        }

                        if (entry.Value[ii+1] == entry.Value[ii])
                        {
                            // Log("Duplicate message. Subscription={0}, SequenceNumber={1}", entry.Key, entry.Value[ii]);
                        }
                    }
                }
            }

            return success;
        }