public void ObserveTest13()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                NotificationReregistrationBackoff = 0
            };

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient
            {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            AutoResetEvent reregistered = new AutoResetEvent(false);

            relation.Request.Reregistering += (a, b) => {
                reregistered.Set();
                b.RefreshRequest.IsCancelled = true;
            };


            byte[] tokenBytes = relation.Request.Token;
            bool   dataSent   = false;
            int    requestNo  = 0;
            int    observerNo = 1;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);

                            switch (requestNo)
                            {
                            case 0:
                                Assert.IsFalse(item.Request.HasOption(OptionType.ETag));
                                break;

                            case 1:
                            case 2:
                                Assert.AreEqual(4, item.Request.ETags.ToArray().Length);
                                break;
                            }

                            requestNo += 1;
                            Debug.WriteLine($"Request: {item.Request}");
                        }
                        else if (item.Response != null)
                        {
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(observerNo, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                            Debug.WriteLine($"Response: {item.Response} {item.Response.Observe}");
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    Assert.IsTrue(trigger.WaitOne(1000));
                    Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                    Assert.AreEqual(observerNo, lastResponse.Observe);
                    dataSent     = false;
                    lastResponse = null;

                    if (count == 4)
                    {
                        relation.UpdateETags(new byte[][] { new byte[] { 1 }, new byte[] { 3 }, new byte[] { 5 }, new byte[] { 7 } });
                        count += 1;
                    }
                    else if (count == 8)
                    {
                        Assert.IsTrue(reregistered.WaitOne(20 * 1000));
                        count += 1;
                    }

                    if (count == 9)
                    {
                        count += 1;
                        _resource.UpdateContent($"New string {count}");
                        observerNo += 1;
                        count      += 1;
                    }
                    else if (count < 12)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count      += 1;
                        observerNo += 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Assert.AreEqual(12, count);
        }
        public void ObserveTest3()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                MaxRetransmit = 0
            };

            LogManager.Level = LogLevel.Debug;

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer), typeof(TokenLayer), typeof(ReliabilityLayer) });

            clientEndpoint.Start();

            CreateServer();
            _config.NonTimeout = 100;  // Change this value up - at 10 it cleans up the NON before the RST has a chance to get back.

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            int  emptyCount = 0;
            bool dataSent   = false;

            byte[] tokenBytes = relation.Request.Token;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            Debug.WriteLine($"Request: {item.Request}");
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                        }
                        else if (item.Response != null)
                        {
                            Debug.WriteLine($"Response: {item.Response}");
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Debug.WriteLine($"RST: {item.EmptyMessage}");
                            emptyCount += 1;
                            dataSent    = true;
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    dataSent = false;
                    if (count >= 3)
                    {
                        Assert.IsNull(lastResponse);
                    }
                    else
                    {
                        Assert.IsTrue(trigger.WaitOne(10 * 1000));
                        Assert.IsNotNull(lastResponse);
                        Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                        Assert.AreEqual(count, lastResponse.Observe);
                        lastResponse = null;
                    }

                    if (count < 5)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count += 1;
                        if (count == 3)
                        {
                            relation.ReactiveCancel();
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else if (count == 5)
                {
                    break;
                }
                else if (emptyCount != 0)
                {
                    _resource.UpdateContent($"New string {count}");
                    count += 1;
                }
            }

            Assert.AreEqual(1, emptyCount);
            Assert.AreEqual(5, count);
        }
        public void ObserveTest1()
        {
            CoapConfig clientConfig = new CoapConfig();

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count-1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            byte[] tokenBytes = relation.Request.Token;
            bool   dataSent   = false;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                        }
                        else if (item.Response != null)
                        {
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    Assert.IsTrue(trigger.WaitOne(1000));
                    Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                    Assert.AreEqual(count, lastResponse.Observe);
                    dataSent     = false;
                    lastResponse = null;

                    if (count < 5)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count += 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Assert.AreEqual(5, count);
        }
        public void ObserveTest2()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                NotificationReregistrationBackoff = 0
            };

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse    = null;
            int      count           = 1;
            int      clientObserveNo = 0;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            AutoResetEvent reregistered = new AutoResetEvent(false);

            relation.Request.Reregistering += (a, b) => {
                reregistered.Set();
            };

            bool dataSent = false;

            byte[] tokenBytes = relation.Request.Token;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(clientObserveNo, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                            clientObserveNo += 1;
                        }
                        else if (item.Response != null)
                        {
                            if (count < 4)
                            {
                                Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                                Assert.AreEqual(count, item.Response.Observe);
                            }
                            else
                            {
                                // Assert.IsFalse(item.Response.HasOption(OptionType.Observe));
                            }

                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    dataSent = false;
                    if (count > 4)
                    {
                        Assert.IsNull(lastResponse);
                    }
                    else
                    {
                        Assert.IsTrue(trigger.WaitOne(1000));
                        Assert.IsNotNull(lastResponse);
                        if (count == 4)
                        {
                            Assert.IsFalse(lastResponse.HasOption(OptionType.Observe));
                        }
                        else
                        {
                            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, lastResponse.Observe);
                        }

                        lastResponse = null;
                    }

                    if (count < 5)
                    {
                        if (count == 3)
                        {
                            relation.ProactiveCancel();
                            _resource.UpdateContent($"New string {count}");
                        }
                        else
                        {
                            _resource.UpdateContent($"New string {count}");
                        }
                        count += 1;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (count == 5)
                {
                    break;
                }
            }

            Assert.AreEqual(2, clientObserveNo);
            Assert.AreEqual(5, count);

            //  Total # of seconds is MaxAge = 1 + backoff = 0 + random(2, 15)
            Assert.IsFalse(reregistered.WaitOne(17 * 1000));
        }