Esempio n. 1
0
        public void Acknowledge_Test()
        {
            string deviceId = ConfigurationManager.AppSettings["DeviceId"];

            IIotApi conn = getConnector();

            ServiceClient svcClient = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceConnStr"]);

            int msgId = new Random().Next();

            svcClient.SendAsync(deviceId, createServiceMessage(new { Command = "testrcv", Value = "msg1", MessageId = msgId })).Wait();

            for (int i = 0; i < 5; i++)
            {
                conn.ReceiveAsync((msg) =>
                {
                    if (msg != null)
                    {
                        dynamic obj = JsonConvert.DeserializeObject(Encoding.UTF8.GetString((byte[])msg));

                        // This will remove unexpected messages from queue.
                        if (obj.MessageId != msgId)
                        {
                            return(true);
                        }

                        if (i < 3)
                        {
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                },
                                  (err) =>
                {
                    throw err;
                }, 0).Wait();
            }
        }
Esempio n. 2
0
        public void ReceiveBatch_Test()
        {
            string deviceId = ConfigurationManager.AppSettings["DeviceId"];

            IIotApi conn = getConnector();

            ServiceClient svcClient = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceConnStr"]);

            int msgId = new Random().Next();

            svcClient.SendAsync(deviceId, createServiceMessage(new { Command = "testrcv", Value = "msg1", MessageId = msgId })).Wait();

            conn.ReceiveAsync((msg) =>
            {
                if (msg != null)
                {
                    dynamic obj = JsonConvert.DeserializeObject(Encoding.UTF8.GetString((byte[])msg));

                    Assert.IsTrue(obj.Command == "testrcv");

                    Assert.IsTrue(obj.Value == "msg1");

                    Assert.IsTrue(obj.MessageId == msgId);
                }
                else
                {
                    Assert.Inconclusive("No message received during test. This might be typically timeout issue. Please restart test.");
                }

                return(true);
            },
                              (err) =>
            {
                throw err;
            }, 0).Wait();
        }