public void TestSynchronousCall()
        {
            _notifications = 0;

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            String resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            String resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            String resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            String resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);

            // Observe the resource
            _expected = CONTENT_2;
            CoapObserveRelation obs1 = client.Observe(response =>
            {
                Interlocked.Increment(ref _notifications);
                String payload = response.ResponseText;
                Assert.AreEqual(_expected, payload);
                Assert.IsTrue(response.HasOption(OptionType.Observe));
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();

            Thread.Sleep(100);
            _expected = CONTENT_3;
            String resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);

            // Cancel observe relation of obs1 and check that it does no longer receive notifications
            Thread.Sleep(100);
            _expected = null; // The next notification would now cause a failure
            obs1.ReactiveCancel();
            Thread.Sleep(100);
            _resource.Changed();

            // Make another post
            Thread.Sleep(100);
            String resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            String resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.AreEqual(5, _notifications);
            Assert.IsFalse(_failed);
        }
Exemple #2
0
        public void TestSynchronousCall()
        {
            int            notifications = 0;
            AutoResetEvent syncEvent     = new AutoResetEvent(false);

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            string resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            string resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            string resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            string resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);
            Thread.Sleep(100);

            // Observe the resource
            _expected = CONTENT_2;

            object         blockLock    = new object();
            AutoResetEvent trigger      = new AutoResetEvent(false);
            Response       lastResponse = null;

            CoapObserveRelation obs1 = client.Observe(response => {
                lastResponse = response;
                trigger.Set();
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            for (int i = 0; i < 5; i++)
            {
                _resource.Changed();
                Assert.IsTrue(trigger.WaitOne(100));
                Assert.AreEqual(_expected, lastResponse.ResponseText);
                Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
            }

            _expected = CONTENT_3;
            string resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);
            Assert.IsFalse(trigger.WaitOne(100));

            // Cancel observe relation of obs1 and check that it does no longer receive notifications

            obs1.ReactiveCancel();
            Thread.Sleep(100);

            _resource.Changed();
            Assert.IsFalse(trigger.WaitOne(100));

            // Make another post
            Thread.Sleep(100);
            string resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            string resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.IsFalse(_failed);
        }