Example #1
0
        public void TryPromoteToSubscribedConnectionTwiceTest()
        {
            var connectionRegistry = new ConnectionRegistry(new PusherRegistry(new DateTimeProvider()), new DateTimeProvider());

            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);
            connectionRegistry.PromoteToSubscribedConnection(persistentConnection.DeviceId, SubscriptionType.PeekAndCommit);
        }
Example #2
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();
        }
Example #3
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();
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
0
        public void PromoteToSubscribedConnectionTest()
        {
            var connectionRegistry = new ConnectionRegistry(new PusherRegistry(new DateTimeProvider()), new DateTimeProvider());

            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);
            Assert.AreEqual(ConnectionState.LoggedIn | ConnectionState.Subscribed, persistentConnection.ConnectionState);
            Assert.AreEqual(SubscriptionType.PeekAndCommit, persistentConnection.SubscriptionType);
            persistentConnection.Received().Heartbeat();
        }
        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();
        }