コード例 #1
0
        public void TelemetryDataLoginRequiredTest()
        {
            var connection                = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator       = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations          = Substitute.For <IDeviceOperations>();
            var dateTimeProvider          = Substitute.For <IDateTimeProvider>();
            var telemetryDataSinkResolver = Substitute.For <ITelemetryDataSinkResolver>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            telemetryDataSinkResolver.ResolveIncoming(null).ReturnsForAnyArgs(new List <ITelemetryDataSink>());
            var telemetryDataService = new DirectTelemetryDataService(telemetryDataSinkResolver);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, telemetryDataService);

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            commandExecutor.Execute(connection, new TelemetryDataCommand("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}"));
            connection.Received(1).Reply("telemetrydata unauthorized");
        }
コード例 #2
0
        public void SendToErrorTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();
            var dateTimeProvider    = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(deviceId + " " + Identity.Next()));

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            commandExecutor.Execute(connection, new SendToCommand("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}"));
            connection.Received(1).Reply("sendto error");
        }
コード例 #3
0
        public void CommitNotLoggedInTest()
        {
            var connection            = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator   = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations      = Substitute.For <IDeviceOperations>();
            var dateTimeProvider      = Substitute.For <IDateTimeProvider>();
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, outgoingMessageReader, deviceAuthenticator, deviceOperations, null, null);

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            pusherRegistry.SetAsCommitNeededConnections(new[] { connection });

            outgoingMessageReader.Commit((long)0).ReturnsForAnyArgs(OutgoingState.Ok);

            connection.ClearReceivedCalls();

            commandExecutor.Execute(connection, new CommitCommand());
            connection.DidNotReceive().Heartbeat();
        }
コード例 #4
0
        public void GetReceiveAndForgetConnectionHeartbeatOkTest()
        {
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var persistentConnection = Substitute.For <IPersistentConnection>();

            persistentConnection.ConnectionId.Returns(Guid.NewGuid());

            connectionRegistry.RegisterInitiatedConnection(persistentConnection);
            connectionRegistry.PromoteToLoggedInConnection(persistentConnection, Identity.Next(), 1);

            connectionRegistry.PromoteToSubscribedConnection(persistentConnection.DeviceId, SubscriptionType.ReceiveAndForget);

            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow.AddSeconds(-20.0));
            persistentConnection.NextReceiveAndForgetTime.Returns(DateTime.UtcNow.AddSeconds(-0.1));

            var conn = pusherRegistry.GetReceiveAndForgetConnection();

            Assert.AreEqual(persistentConnection, conn);
            persistentConnection.ReceivedWithAnyArgs().NextReceiveAndForgetTime = new DateTime();
        }
コード例 #5
0
        public void GetPeekConnectionHeartbeatTooEarlyTest()
        {
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var persistentConnection = Substitute.For <IPersistentConnection>();

            persistentConnection.ConnectionId.Returns(Guid.NewGuid());

            connectionRegistry.RegisterInitiatedConnection(persistentConnection);
            connectionRegistry.PromoteToLoggedInConnection(persistentConnection, Identity.Next(), 1);

            connectionRegistry.PromoteToSubscribedConnection(persistentConnection.DeviceId, SubscriptionType.PeekAndCommit);

            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow.AddSeconds(-20.0));
            persistentConnection.NextPeekTime.Returns(DateTime.UtcNow.AddSeconds(0.1));

            var nothing = pusherRegistry.GetPeekConnection();

            Assert.IsNull(nothing);
        }
コード例 #6
0
        public void GetPeekConnectionRequeueFromCommitToPeekTest()
        {
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var persistentConnection = Substitute.For <IPersistentConnection>();

            persistentConnection.ConnectionId.Returns(Guid.NewGuid());

            connectionRegistry.RegisterInitiatedConnection(persistentConnection);
            connectionRegistry.PromoteToLoggedInConnection(persistentConnection, Identity.Next(), 1);

            connectionRegistry.PromoteToSubscribedConnection(persistentConnection.DeviceId, SubscriptionType.PeekAndCommit);

            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow.AddSeconds(-20.0));
            persistentConnection.NextPeekTime.Returns(DateTime.UtcNow.AddSeconds(-0.1));

            var conn = pusherRegistry.GetPeekConnection();

            pusherRegistry.SetAsCommitNeededConnections(new[] { conn });

            persistentConnection.LastCommitTime = DateTime.UtcNow.AddSeconds(-20);

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow.AddMilliseconds(200));
            pusherRegistry.GetPeekConnection();
        }
コード例 #7
0
        public void GetReceiveAndForgetConnectionNotYetSubscribedTest()
        {
            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var persistentConnection = Substitute.For <IPersistentConnection>();

            persistentConnection.ConnectionId.Returns(Guid.NewGuid());

            connectionRegistry.RegisterInitiatedConnection(persistentConnection);
            connectionRegistry.PromoteToLoggedInConnection(persistentConnection, Identity.Next(), 1);

            connectionRegistry.PromoteToSubscribedConnection(persistentConnection.DeviceId, SubscriptionType.ReceiveAndForget);
            connectionRegistry.UnsubscribeConnection(persistentConnection.DeviceId);

            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow.AddMinutes(-10.1));

            var nothing = pusherRegistry.GetReceiveAndForgetConnection();

            Assert.IsNull(nothing);
        }
コード例 #8
0
        public void StartStopWithReceiveAndForgetProcessingElementTest()
        {
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();
            var persistentConnection  = Substitute.For <IPersistentConnection>();

            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var batchParameters = Substitute.For <IBatchParameters>();

            batchParameters.PersistentConnectionMessageReceiveAndForgetCollectionTime.Returns(
                TimeSpan.FromMilliseconds(100));
            batchParameters.PersistentConnectionMessageReceiveAndForgetCollectionBatch.Returns(100);

            var pusherRegistry = new PusherRegistry(dateTimeProvider);

            persistentConnection.DeviceId         = "1234";
            persistentConnection.NumericDeviceId  = 1;
            persistentConnection.ConnectionState  = ConnectionState.LoggedIn | ConnectionState.Subscribed;
            persistentConnection.SubscriptionType = SubscriptionType.ReceiveAndForget;
            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow);
            outgoingMessageReader.ReceiveAndForgetMany(null)
            .ReturnsForAnyArgs(new Dictionary <long, OutgoingMessageToStoreWithState>()
            {
                {
                    1,
                    new OutgoingMessageToStoreWithState(new OutgoingMessageToStore(1, Encoding.UTF8.GetBytes("{}"), 1,
                                                                                   DateTime.UtcNow, "sender"), OutgoingState.Ok)
                }
            });

            pusherRegistry.AddConnection(persistentConnection);

            persistentConnection.NextReceiveAndForgetTime = DateTime.UtcNow.AddSeconds(-2);

            var persistentConnectionWorker = new PersistentConnectionReceiveAndForgetWorker(pusherRegistry, outgoingMessageReader, batchParameters);

            persistentConnectionWorker.Start();
            bool ok = false;

            for (int i = 0; i < 30; i++)
            {
                try
                {
                    persistentConnection.ReceivedWithAnyArgs().SendMessage(null);
                    ok = true;
                    break;
                }
                catch (ReceivedCallsException)
                {
                    Thread.Sleep(100);
                }
            }

            persistentConnectionWorker.Stop();

            Assert.IsTrue(ok);
        }
コード例 #9
0
        public void GetPeekConnectionEmptyTest()
        {
            var pusherRegistry = new PusherRegistry(new DateTimeProvider());

            var nothing = pusherRegistry.GetPeekConnection();

            Assert.IsNull(nothing);
        }
コード例 #10
0
        public void StartStopTest()
        {
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();
            var dateTimeProvider      = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var batchParameters = Substitute.For <IBatchParameters>();

            batchParameters.PersistentConnectionMessageReceiveAndForgetCollectionTime.Returns(
                TimeSpan.FromMilliseconds(100));
            batchParameters.PersistentConnectionMessageReceiveAndForgetCollectionBatch.Returns(100);

            var pusherRegistry = new PusherRegistry(dateTimeProvider);

            var persistentConnectionWorker = new PersistentConnectionReceiveAndForgetWorker(pusherRegistry, outgoingMessageReader, batchParameters);

            persistentConnectionWorker.Start();
            persistentConnectionWorker.Stop();
        }
コード例 #11
0
        public void CommitSuccessTest()
        {
            var connection            = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator   = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations      = Substitute.For <IDeviceOperations>();
            var dateTimeProvider      = Substitute.For <IDateTimeProvider>();
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, outgoingMessageReader, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(deviceId + " " + Identity.Next()));

            commandExecutor.Execute(connection, new SubscribeCommand("peekandcommit"));

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            pusherRegistry.SetAsCommitNeededConnections(new[] { connection });

            outgoingMessageReader.Commit((long)0).ReturnsForAnyArgs(OutgoingState.Ok);

            Assert.IsTrue(pusherRegistry.IsCommitable(deviceId));

            commandExecutor.Execute(connection, new CommitCommand());

            Assert.IsFalse(pusherRegistry.IsCommitable(deviceId));

            commandExecutor.Execute(connection, new CommitCommand());
        }
コード例 #12
0
        public void CommitNotApplicableTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();
            var dateTimeProvider    = Substitute.For <IDateTimeProvider>();

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device("1234", "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(Identity.Next() + " " + Identity.Next()));

            commandExecutor.Execute(connection, new SubscribeCommand("peekandcommit"));

            commandExecutor.Execute(connection, new CommitCommand());
        }
コード例 #13
0
        public void RecordOutgoingAndPeekMessageRealTest()
        {
            var environmentFactory = EnvironmentFactoryFactory.Create();

            MessagingWorkers.Start(new TestBatchParameters(), environmentFactory.MessagingEnvironment.MessagingServiceClient);

            var pltDeviceOperations = environmentFactory.ManagementEnvironment.ObjDeviceOperations;
            var messagingOperations = new MessagingOperations();

            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            var batchParameters = Substitute.For <IBatchParameters>();

            batchParameters.PersistentConnectionMessagePeekCollectionTime.Returns(
                TimeSpan.FromMilliseconds(100));
            batchParameters.PersistentConnectionMessagePeekCollectionBatch.Returns(100);

            var pusherRegistry = new PusherRegistry(dateTimeProvider);

            var persistentConnectionWorker = new PersistentConnectionPeekWorker(pusherRegistry, messagingOperations, batchParameters);

            persistentConnectionWorker.Start();

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var success = messagingService.RecordOutgoingMessage(_deviceId, _deviceId, "32412341243");

            Assert.AreEqual(OutgoingState.Ok, success);


            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var connection = Substitute.For <IPersistentConnection>();

            connectionRegistry.RegisterInitiatedConnection(connection);
            connection.LastHeartbeat.Returns(DateTime.UtcNow);

            var device = _deviceService.Get(_deviceId);

            connectionRegistry.PromoteToLoggedInConnection(connection, _deviceId, device.NumericId);
            connectionRegistry.PromoteToSubscribedConnection(_deviceId, SubscriptionType.PeekAndCommit);
            connection.NextPeekTime = DateTime.UtcNow.AddSeconds(-0.1);
            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            bool ok = false;

            for (int i = 0; i < 20; i++)
            {
                try
                {
                    connection.ReceivedWithAnyArgs().SendMessage(null);
                    ok = true;
                }
                catch (ReceivedCallsException)
                {
                    Thread.Sleep(100);
                }
            }

            Assert.IsTrue(ok);

            persistentConnectionWorker.Stop();
            MessagingWorkers.Stop();
        }