public async Task AlertingGroupCanTurnOffErrorQueueMonitoring()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string> {
                "pattern1", "pattern1_error"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakePatternConfig();
            var sqs    = config.AlertingGroups[0].Sqs;

            sqs.Errors = new ErrorQueue
            {
                Monitored = false
            };

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);

            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.NoLengthAlarm(alarmCreator, "pattern1_error");
            VerifyQueues.NoOldestMessageAlarm(alarmCreator, "pattern1_error");
        }
        public async Task MainAndErrorQueuesAreMonitoredAtDifferentThresholds()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string> {
                "prod-pattern-queue", "prod-pattern-queue_error"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakePatternConfig();

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);


            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue", 10, false);
            VerifyQueues.EnsureOldestMessageAlarm(alarmCreator, "prod-pattern-queue", 600, false);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue_error", 1, false);
            VerifyQueues.NoOldestMessageAlarm(alarmCreator, "prod-pattern-queue_error");
        }
        public async Task ExtraQueuesInResourcesShouldBeIgnored()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string>
            {
                "aa_nomatch_queue",
                "prod-pattern-queue",
                "prod-pattern-queue-two",
                "zz_nomatch_queue"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakePatternConfig();

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);

            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue", 10, false);
            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue-two", 10, false);
            VerifyQueues.NoLengthAlarm(alarmCreator, "aa_nomatch_queue");
            VerifyQueues.NoLengthAlarm(alarmCreator, "zz_nomatch_queue");
        }
        public async Task GenerateAlarmsRunShouldCallQueueAlarmCreator()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string> {
                "queue1"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakeSimpleConfig();

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);

            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "queue1", false);
        }
        public async Task NamedQueueShouldAlsoMonitorMatchingErrorQueue()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string>
            {
                "prod-pattern-queue",
                "prod-pattern-queue_error",
                "some-other-queue"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config    = MakeSimpleConfig();
            var sqsConfig = config.AlertingGroups[0].Sqs;

            sqsConfig.LengthThreshold = 11;
            sqsConfig.Errors          = new ErrorQueue
            {
                Monitored       = true,
                LengthThreshold = 7
            };

            sqsConfig.Queues = new List <Queue>
            {
                new Queue
                {
                    Name = "prod-pattern-queue"
                }
            };

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);

            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue", 11, false);
            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue_error", 7, false);
            VerifyQueues.NoLengthAlarm(alarmCreator, "some-other-queue");
        }
        public async Task QueueValuesAreUsedAsOverride()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string> {
                "pattern1", "pattern1_error"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakePatternConfig();
            var sqs    = config.AlertingGroups[0].Sqs;

            sqs.Errors = new ErrorQueue
            {
                LengthThreshold        = 142,
                OldestMessageThreshold = 112
            };
            sqs.Queues[0].LengthThreshold        = 42;
            sqs.Queues[0].OldestMessageThreshold = 12;

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);

            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "pattern1", 42, false);
            VerifyQueues.EnsureOldestMessageAlarm(alarmCreator, "pattern1", 12, false);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "pattern1_error", 1, false);
            VerifyQueues.NoOldestMessageAlarm(alarmCreator, "pattern1_error");
        }
        public async Task AlertingGroupThresholdsAreUsedByDefault()
        {
            var queueSource = new Mock <IResourceSource <QueueData> >();

            VerifyQueues.ReturnsQueues(queueSource, new List <string> {
                "prod-pattern-queue", "prod-pattern-queue_error"
            });

            var alarmCreator           = new Mock <IQueueAlarmCreator>();
            var snsTopicCreator        = new Mock <ISnsTopicCreator>();
            var snsSubscriptionCreator = new Mock <ISnsSubscriptionCreator>();

            var snsCreator = new SnsCreator(snsTopicCreator.Object, snsSubscriptionCreator.Object);

            var config = MakePatternConfig();
            var sqs    = config.AlertingGroups[0].Sqs;

            sqs.LengthThreshold        = 32;
            sqs.OldestMessageThreshold = 17;

            sqs.Queues[0].LengthThreshold        = null;
            sqs.Queues[0].OldestMessageThreshold = null;

            var populator = new QueueNamePopulator(new ConsoleAlarmLogger(false),
                                                   queueSource.Object);

            var generator = new SqsAlarmGenerator(
                new ConsoleAlarmLogger(false), queueSource.Object,
                populator, alarmCreator.Object,
                snsCreator);


            await generator.GenerateAlarmsFor(config, RunMode.GenerateAlarms);

            VerifyQueues.EnsureLengthAlarm(alarmCreator, "prod-pattern-queue", 32, false);
            VerifyQueues.EnsureOldestMessageAlarm(alarmCreator, "prod-pattern-queue", 17, false);
        }