Beispiel #1
0
        public void HeartbeatTest()
        {
            RegisterDevice(false);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            var privateObject            = new PrivateObject(persistentConnection);
            var lastHeartbeatBeforeLogin = (DateTime)privateObject.GetField("_lastHeartbeatTime");

            persistentConnection.Login(_deviceId, _apiKey);
            var lastHeartbeatAfterLogin = (DateTime)privateObject.GetField("_lastHeartbeatTime");

            Thread.Sleep(1);
            persistentConnection.RecordTelemetryData("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");
            var lastHeartbeatAfterOperation = (DateTime)privateObject.GetField("_lastHeartbeatTime");

            Assert.IsTrue(lastHeartbeatBeforeLogin < lastHeartbeatAfterLogin);
            Assert.IsTrue(lastHeartbeatAfterLogin < lastHeartbeatAfterOperation);

            persistentConnection.Spin();
            var afterNearspin = (DateTime)privateObject.GetField("_lastHeartbeatTime");

            Assert.AreEqual(lastHeartbeatAfterOperation, afterNearspin);

            privateObject.SetField("_lastHeartbeatTime", new DateTime(2015, 1, 1));

            Thread.Sleep(1);
            persistentConnection.Spin();

            var afterFarspin = (DateTime)privateObject.GetField("_lastHeartbeatTime");

            Assert.IsTrue(afterNearspin < afterFarspin);
        }
Beispiel #2
0
        private static void PersistentRecvCommit(string[] lines, int deviceOrder, int sleep)
        {
            var line = lines[deviceOrder];

            var parts = line.Split(' ');

            var deviceId = parts[0];
            var apiKey   = parts[1];

            Log(deviceId);


            PushedMessage pushedMessage = null;
            int           cnt           = 0;
            var           persistentConnectionClient = new PersistentConnectionClient(Config.PlatformApiWS);

            RetryableLogin(persistentConnectionClient, deviceId, apiKey);
            RetryableSubscribe(persistentConnectionClient, SubscriptionType.PeekAndCommit, message =>
            {
                if (message != null)
                {
                    pushedMessage = message;
                    cnt           = message.MessageId;
                    Interlocked.Increment(ref allCounter);
                    Log(cnt.ToString());
                    Log("Allcounter: " + allCounter);
                }
            });

            while (true)
            {
                persistentConnectionClient.Spin();
                Thread.Sleep(sleep);
            }
        }
Beispiel #3
0
        public void SendToAndReceiveTest()
        {
            RegisterDevice(true);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.SendMessageTo(_otherDeviceId, "{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");

            persistentConnection.Close();

            var persistentConnection2 = new PersistentConnectionClient(PlatformWebsocketApi);

            PushedMessage    received = null;
            ManualResetEvent mre      = new ManualResetEvent(false);

            persistentConnection2.Login(_otherDeviceId, _apiKey);
            persistentConnection2.Subscribe(SubscriptionType.ReceiveAndForget, msg => { received = msg;
                                                                                        mre.Set(); });

            mre.WaitOne(TimeSpan.FromSeconds(2));

            Assert.IsNotNull(received);
            Assert.AreEqual(_deviceId, received.SenderDeviceId);
            Assert.IsTrue(received.Payload.StartsWith("{\"Temperature\": 24, \"Time\":"));
        }
Beispiel #4
0
        public void TrySubscribeNotLoggedInTest()
        {
            RegisterDevice();

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Subscribe(SubscriptionType.ReceiveAndForget, message => { });
        }
Beispiel #5
0
        public void TryInvalidCredentialsLoginTest()
        {
            RegisterDevice();

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, "123123");
        }
Beispiel #6
0
        public void LoginTest()
        {
            RegisterDevice();

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);
        }
Beispiel #7
0
        public void TelemetryDataErrorNoSinkTest()
        {
            RegisterDevice(addMessageSinks: false);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.RecordTelemetryData("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");
        }
Beispiel #8
0
 private static void RetryableSubscribe(PersistentConnectionClient persistentConnectionClient, SubscriptionType subscriptionType, Action <PushedMessage> action)
 {
     try
     {
         persistentConnectionClient.Subscribe(subscriptionType, action);
     }
     catch (Exception ex)
     {
         Log(ex);
     }
 }
Beispiel #9
0
 private static void RetryableLogin(PersistentConnectionClient persistentConnectionClient, string deviceId,
                                    string apiKey)
 {
     try
     {
         persistentConnectionClient.Login(deviceId, apiKey);
     }
     catch (Exception ex)
     {
         Log(ex);
     }
 }
Beispiel #10
0
        public void SendToTest()
        {
            RegisterDevice(true);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.SendMessageTo(_otherDeviceId, "{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");

            persistentConnection.Close();
        }
Beispiel #11
0
        public void TryUnsubscribeTwiceTest()
        {
            RegisterDevice();

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.Subscribe(SubscriptionType.ReceiveAndForget, message => { });

            persistentConnection.Unsubscribe();
            persistentConnection.Unsubscribe();
        }
Beispiel #12
0
        private static void PersistentSend(string[] lines, int deviceOrder, int sleep, int maxSend)
        {
            var line = lines[deviceOrder];

            var parts = line.Split(' ');

            var deviceId = parts[0];
            var apiKey   = parts[1];

            Log(deviceId);


            int cnt = 0;
            var rnd = new Random();

            var persistentConnectionClient = new PersistentConnectionClient(Config.PlatformApiWS);

            while (true)
            {
                RetryableLogin(persistentConnectionClient, deviceId, apiKey);

                while (true)
                {
                    var target         = lines[rnd.Next(maxSend)];
                    var targetDeviceId = target.Split(' ')[0];
                    try
                    {
                        persistentConnectionClient.SendMessageTo(targetDeviceId,
                                                                 "{\"Temperature:\": 24, \"Humidity\": 60, \"Time\":" + DateTime.UtcNow.Ticks + "}");
                        cnt++;
                    }
                    catch (Exception ex)
                    {
                        Log(ex);
                    }
                    persistentConnectionClient.Spin();
                    Thread.Sleep(sleep);
                    Log("To " + targetDeviceId + " Cnt: " + cnt);
                }
            }
        }
Beispiel #13
0
        private static void PersistentRecord(string[] lines, int deviceOrder, int sleep)
        {
            var line = lines[deviceOrder];

            var parts = line.Split(' ');

            var deviceId = parts[0];
            var apiKey   = parts[1];

            var rnd = new Random();
            int cnt = 0;

            Log(deviceId);

            var persistentConnectionClient = new PersistentConnectionClient(Config.PlatformApiWS);

            while (true)
            {
                RetryableLogin(persistentConnectionClient, deviceId, apiKey);

                while (true)
                {
                    try
                    {
                        var doit = rnd.Next(100);
                        var a    = doit % 10 == 0 ? string.Format(", \"A\": {0}", rnd.Next(10)) : "";
                        var b    = doit % 15 == 0 ? string.Format(", \"B\": {0}", rnd.Next(20)) : "";

                        persistentConnectionClient.RecordTelemetryData(string.Format("{{\"Temperature\": {0}, \"Humidity\": {1}, \"Time\": {2}{3}}}", rnd.Next(10) + 18, rnd.Next(40) + 40, DateTime.UtcNow.Second, a + b));
                        cnt++;
                    }
                    catch (Exception ex)
                    {
                        Log(ex);
                    }
                    persistentConnectionClient.Spin();
                    Thread.Sleep(sleep);
                    Log(cnt.ToString());
                }
            }
        }