예제 #1
0
        public void StartStopWithPeekProcessingElementTest()
        {
            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.PersistentConnectionMessagePeekCollectionTime.Returns(
                TimeSpan.FromMilliseconds(100));
            batchParameters.PersistentConnectionMessagePeekCollectionBatch.Returns(100);

            var pusherRegistry = new PusherRegistry(dateTimeProvider);

            persistentConnection.DeviceId         = "1234";
            persistentConnection.NumericDeviceId  = 1;
            persistentConnection.ConnectionState  = ConnectionState.LoggedIn | ConnectionState.Subscribed;
            persistentConnection.SubscriptionType = SubscriptionType.PeekAndCommit;
            persistentConnection.LastHeartbeat.Returns(DateTime.UtcNow);
            outgoingMessageReader.PeekMany(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.NextPeekTime = DateTime.UtcNow.AddSeconds(-2);

            var persistentConnectionWorker = new PersistentConnectionPeekWorker(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);
        }
예제 #2
0
        private void StartBackgrounProcesses()
        {
            var settingOperations = _serviceProvider.GetService <ISettingOperations>();

            SetupTelemetryDataSinkMetadataRegistry(settingOperations);

            var messagingServiceClient = _serviceProvider.GetService <IMessagingServiceClient>();

            messagingServiceClient.Setup(settingOperations.Get(Setting.MessagingServiceEndpoint).Value, settingOperations.Get(Setting.MessagingServiceApiKey).Value);

            var batchParameters = _serviceProvider.GetService <IBatchParameters>();

            MessagingWorkers.Start(batchParameters, messagingServiceClient);

            _persistentConnectionReceiveAndForgetWorker = _serviceProvider.GetService <PersistentConnectionReceiveAndForgetWorker>();
            _persistentConnectionReceiveAndForgetWorker.Start();

            _persistentConnectionPeekWorker = _serviceProvider.GetService <PersistentConnectionPeekWorker>();
            _persistentConnectionPeekWorker.Start();

            _connectionRegistry = _serviceProvider.GetService <ConnectionRegistry>();
            _connectionRegistry.Start();
        }
        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();
        }