public void TestMessageSizeDoesNotExceedsLimit()
        {
            var maxMessageSize = Int32.Parse(Twin.GetConfiguration("maxMessageSizeInBytes"));

            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.High)
            .UpdateAgentTwinConfiguration();

            var expectedOperationalEvent = FakesEventsFactory.CreateFakeOperationalEvent();
            var expectedPeriodicEvent    = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
            var expectedTriggeredEvent   = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.High);

            FakeOperationalEventGenerator.SetEvents(new[] { expectedOperationalEvent });
            FakeSnapshotEventGenerator.SetEvents(new[] { expectedPeriodicEvent });
            FakeTriggeredEventGenerator.SetEvents(new[] { expectedTriggeredEvent });

            RepeatAction(_eventProducer, 100);
            RepeatAction(_messageBuilderConsumer, 100);

            Assert.IsTrue(MessageHelper.WaitUntilAsyncMessageSendsComplete(ClientMock));

            var sentMessages = ClientMock.GetMessages();

            Assert.IsTrue(sentMessages.Count > 0);

            foreach (var msg in sentMessages)
            {
                Assert.IsTrue(msg.GetBytes().Length < maxMessageSize);
            }
        }
        public void TestOperationalEventsAreFiredWithFirestMessage()
        {
            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.Operational)
            .ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.Low)
            .UpdateAgentTwinConfiguration();

            var expectedOperationalEvent = FakesEventsFactory.CreateFakeOperationalEvent();
            var expectedPeriodicEvent    = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
            var expectedTriggeredEvent   = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.Low);

            FakeOperationalEventGenerator.SetEvents(new[] { expectedOperationalEvent });
            FakeSnapshotEventGenerator.SetEvents(new[] { expectedPeriodicEvent });
            FakeTriggeredEventGenerator.SetEvents(new[] { expectedTriggeredEvent });

            _eventProducer.ExecuteTask();
            _eventProducer.ExecuteTask();

            _messageBuilderConsumer.ExecuteTask();

            var sentMessages = ClientMock.GetMessages();

            Assert.AreEqual(2, sentMessages.Count);

            var firstMsg = MessageHelper.GetEventsFromMsg(sentMessages[0].GetBytes());

            Assert.AreEqual(2, firstMsg.Count(ev => ev.EventType == EventType.Operational));

            var secondMessage = MessageHelper.GetEventsFromMsg(sentMessages[1].GetBytes());

            Assert.AreEqual(0, secondMessage.Count(ev => ev.EventType == EventType.Operational));
        }
Ejemplo n.º 3
0
        public void TestSnapshotEventsIsFiredOnSnapshotInerval()
        {
            var snapshotInterval = TimeSpan.FromSeconds(10);

            Twin.ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.Low)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.High)
            .ChangeConfiguration("snapshotFrequency", XmlConvert.ToString(snapshotInterval))
            .UpdateAgentTwinConfiguration();

            Assert.AreEqual(snapshotInterval, AgentConfiguration.RemoteConfiguration.SnapshotFrequency);

            using (SingleThreadedAgent agent = new SingleThreadedAgent())
            {
                var taskUnderTest = new Task(agent.Start);
                taskUnderTest.Start();

                var expectedEvent = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
                FakeSnapshotEventGenerator.SetEvents(new[] { expectedEvent });

                while (ClientMock.GetMessages().Count == 0)
                {
                    ;
                }
                VerifySnapshot(ClientMock.GetMessages(), new[] { expectedEvent });
                ClientMock.Reset();
                var expectedSecondEvent = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
                FakeSnapshotEventGenerator.SetEvents(new[] { expectedSecondEvent });

                Thread.Sleep(snapshotInterval + AgentConfiguration.RemoteConfiguration.HighPriorityMessageFrequency);

                VerifySnapshot(ClientMock.GetMessages(), new[] { expectedSecondEvent });
            }
        }
        public void TestMessageWithOperationalEventDoesNotExceedMessageLimit()
        {
            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.Operational)
            .ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.Low)
            .UpdateAgentTwinConfiguration();

            var expectedOperationalEvent = FakesEventsFactory.CreateFakeOperationalEvent();
            var expectedPeriodicEvent    = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
            var expectedTriggeredEvent   = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.Low);

            FakeOperationalEventGenerator.SetEvents(new[] { expectedOperationalEvent });
            FakeSnapshotEventGenerator.SetEvents(new[] { expectedPeriodicEvent });
            FakeTriggeredEventGenerator.SetEvents(new[] { expectedTriggeredEvent });

            for (int i = 0; i < 10; i++)
            {
                _eventProducer.ExecuteTask();
            }
            _messageBuilderConsumer.ExecuteTask();

            Assert.IsTrue(MessageHelper.WaitUntilAsyncMessageSendsComplete(ClientMock));

            var sentMessages = ClientMock.GetMessages();

            Assert.AreEqual(3, sentMessages.Count);

            foreach (var msg in sentMessages)
            {
                Assert.IsTrue(msg.BodyStream.Length < AgentConfiguration.RemoteConfiguration.MaxMessageSize);
            }
        }
 public override void Init()
 {
     base.Init();
     Twin.ChangeEventPriority(nameof(NetstatEventGenerator), EventPriority.High);
     _mockedShell       = new Mock <IProcessUtil>();
     _genertorUnderTest = new NetstatEventGenerator(_mockedShell.Object);
 }
        public void TestLowPrioEventFromSingleGenerator()
        {
            Twin.ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.Low)
            .UpdateAgentTwinConfiguration();

            var expectedEvents = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.High);

            FakeTriggeredEventGenerator.SetEvents(new[] { expectedEvents });

            TestSingleEvent(expectedEvents);
        }
Ejemplo n.º 7
0
        public void TesTDeviceTwinConfigurationIsChangedIfTwinIsChanged()
        {
            NameValueCollection generalConfig = (NameValueCollection)ConfigurationManager.GetSection("General");

            var generatorUnderTest = new FakeOperationalEventGenerator();

            Assert.AreEqual(EventPriority.Off, generatorUnderTest.Priority);

            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.High);


            Assert.AreEqual(EventPriority.High, generatorUnderTest.Priority);
        }
        public void TestEventAreFiredCorrectlyFromMultipleGenerators()
        {
            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.High)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.Low)
            .UpdateAgentTwinConfiguration();

            var expectedOperationalEvent = FakesEventsFactory.CreateFakeOperationalEvent();
            var expectedPeriodicEvent    = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
            var expectedTriggeredEvent   = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.Low);

            FakeOperationalEventGenerator.SetEvents(new [] { expectedOperationalEvent });
            FakeSnapshotEventGenerator.SetEvents(new [] { expectedPeriodicEvent });
            FakeTriggeredEventGenerator.SetEvents(new [] { expectedTriggeredEvent });

            _eventProducer.ExecuteTask();
            _eventProducer.ExecuteTask();

            _messageBuilderConsumer.ExecuteTask();

            var sentMessages = ClientMock.GetMessages();

            Assert.AreEqual(2, sentMessages.Count);

            var sentEvents = new List <Event>();

            foreach (var msg in sentMessages)
            {
                byte[] msgBytes = msg.GetBytes();
                MessageHelper.VerifyMessageHeader(msgBytes);
                sentEvents.AddRange(MessageHelper.GetEventsFromMsg(msgBytes));
            }

            foreach (var ev in sentEvents)
            {
                if (ev.Name == typeof(FakePeriodicEvent).Name)
                {
                    MessageHelper.ValidateEventsAreEqual(ev, expectedPeriodicEvent);
                }
                else if (ev.Name == typeof(FakeTriggeredEvent).Name)
                {
                    MessageHelper.ValidateEventsAreEqual(ev, expectedTriggeredEvent);
                }
                else if (ev.Name == typeof(FakeOperationalEvent).Name)
                {
                    MessageHelper.ValidateEventsAreEqual(ev, expectedOperationalEvent);
                }
            }
        }
        public override void Init()
        {
            base.Init();

            Twin.ChangeEventPriority(nameof(FirewallConfigurationSnapshotGenerator), EventPriority.High);

            _mockedShell = new Mock <IProcessUtil>();
            _mockedShell.Setup(m => m.ExecuteProcess(
                                   It.IsAny <string>(),
                                   It.Is <string>(x => x.Contains("which")),
                                   It.IsAny <ErrorHandler>(),
                                   It.IsAny <IEnumerable <int> >()))
            .Returns("iptables");

            _genertorUnderTest = new FirewallConfigurationSnapshotGenerator(_mockedShell.Object);
        }
        public void TestEventNotFiredIfEventIsTooBig()
        {
            var maxMessageSize = Int32.Parse(Twin.GetConfiguration("maxMessageSizeInBytes"));

            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.High)
            .UpdateAgentTwinConfiguration();
            Assert.AreEqual((uint)maxMessageSize, AgentConfiguration.RemoteConfiguration.MaxMessageSize);

            var expectedEvent = FakesEventsFactory.CreateFakeOperationalEvent(GenerateBigFakePayload(maxMessageSize));

            FakeOperationalEventGenerator.SetEvents(new[] { expectedEvent });

            RepeatAction(_eventProducer, 100);

            _messageBuilderConsumer.ExecuteTask();
            var sentMessages = ClientMock.GetMessages();

            Assert.AreEqual(0, sentMessages.Count);
        }
Ejemplo n.º 11
0
        public override void Init()
        {
            base.Init();

            Twin.ChangeEventPriority(nameof(LocalUsersSnapshotGenerator), EventPriority.High);

            _mockedShell = new Mock <IProcessUtil>();
            _mockedShell.Setup(m => m.ExecuteProcess(
                                   It.IsAny <string>(),
                                   It.Is <string>(x => x.Contains("passwd")),
                                   It.IsAny <ErrorHandler>(),
                                   It.IsAny <IEnumerable <int> >()))
            .Returns(Passwd);
            _mockedShell.Setup(m => m.ExecuteProcess(
                                   It.IsAny <string>(),
                                   It.Is <string>(x => x.Contains("group")),
                                   It.IsAny <ErrorHandler>(),
                                   It.IsAny <IEnumerable <int> >()))
            .Returns(Group);

            _genertorUnderTest = new LocalUsersSnapshotGenerator(_mockedShell.Object);
        }
        public void TestOperationalEventIsNotFiredIfAMessageIsNotFired()
        {
            Twin.ChangeEventPriority(typeof(FakeOperationalEvent).Name, EventPriority.Operational)
            .ChangeEventPriority(typeof(FakePeriodicEvent).Name, EventPriority.Off)
            .ChangeEventPriority(typeof(FakeTriggeredEvent).Name, EventPriority.Off)
            .UpdateAgentTwinConfiguration();

            var expectedOperationalEvent = FakesEventsFactory.CreateFakeOperationalEvent();
            var expectedPeriodicEvent    = FakesEventsFactory.CreateFakePeriodicEvent(EventPriority.High);
            var expectedTriggeredEvent   = FakesEventsFactory.CreateFakeTriggeredEvent(EventPriority.Low);

            FakeOperationalEventGenerator.SetEvents(new[] { expectedOperationalEvent });
            FakeSnapshotEventGenerator.SetEvents(new[] { expectedPeriodicEvent });
            FakeTriggeredEventGenerator.SetEvents(new[] { expectedTriggeredEvent });

            _eventProducer.ExecuteTask();
            _eventProducer.ExecuteTask();

            _messageBuilderConsumer.ExecuteTask();

            var sentMessages = ClientMock.GetMessages();

            Assert.AreEqual(0, sentMessages.Count);
        }
 /// <inheritdoc />
 protected override void SetEventPriority(EventPriority priority)
 {
     Twin.ChangeEventPriority(nameof(ProcessTerminate), priority);
 }
Ejemplo n.º 14
0
 /// <inheritdoc />
 protected override void SetEventPriority(EventPriority priority)
 {
     Twin.ChangeEventPriority(nameof(ConnectionCreate), priority);
 }
Ejemplo n.º 15
0
 /// <inheritdoc />
 protected override void SetEventPriority(EventPriority priority)
 {
     Twin.ChangeEventPriority(typeof(ProcessCreate).Name, priority);
 }