public void Can_deliver_single_batch_of_messages_with_overrun_in_backlog()
        {
            const int count = 10000;

            var config = new DeliveryConfiguration {BacklogFolder = "backlog", MaxDegreeOfParallelism = 1};

            CreateOrCleanDirectory(config.BacklogFolder);

            var harness = new InMemoryEmailService();
            var service = new DeliveryService(harness, config);
            var messages = MessageFactory.EmailWithHtmlAndText(count);
            
            service.Start();
            service.Send(messages);
            Thread.Sleep(10);
            service.Stop(DeliveryCancellationHandling.SendToBacklog);

            var backlogged = Directory.GetFiles(config.BacklogFolder).Length;
            var delivered = harness.Messages.Count;
            Assert.AreEqual(count, backlogged + delivered);
            Trace.WriteLine(string.Format("Backlogged: {0}", backlogged));
            Trace.WriteLine(string.Format("Delivered: {0}", delivered));

            CleanDirectory(config.BacklogFolder);
        }
        public void Can_deliver_single_batch_of_messages_fully()
        {
            const int count = 1000;

            var pickupDirectory = Path.Combine(Path.GetTempPath(), "pickup");
            CreateOrCleanDirectory(pickupDirectory);

            var config = new DeliveryConfiguration {BacklogFolder = "backlog", MaxDegreeOfParallelism = 10};

            var service = new DeliveryService(new DirectoryEmailProvider(pickupDirectory), config);
            var messages = MessageFactory.EmailWithHtmlAndText(count);

            service.Start();
            service.Send(messages);
            service.Stop(DeliveryCancellationHandling.EmptyQueue);
            
            AssertFileCount(pickupDirectory, count);
            CleanDirectory(pickupDirectory);
        }
        public void Can_start_and_seed_with_backlog()
        {
            const int backlog = 500;

            var config = new DeliveryConfiguration {BacklogFolder = "backlog", MaxDegreeOfParallelism = 1};

            CreateOrCleanDirectory(config.BacklogFolder);

            var harness = new InMemoryEmailService();
            var service = new DeliveryService(harness, config);
            var messages = MessageFactory.EmailWithHtmlAndText(backlog);

            foreach(var message in messages)
            {
                service.Backlog(message);
            }

            service.Start();
            service.Stop(DeliveryCancellationHandling.EmptyQueue);
            Assert.AreEqual(backlog, harness.Messages.Count);

            CleanDirectory(config.BacklogFolder);
        }
예제 #4
0
        public void Can_cap_delivery_rate()
        {
            const int count = 2000;
            const int rate = 1000;

            var config = new DeliveryConfiguration { BacklogFolder = "backlog", MaxDegreeOfParallelism = 10, MaxDeliveryRate = rate };

            var inMemory = new InMemoryEmailService();
            var service = new DeliveryService(inMemory, config);
            var messages = MessageFactory.EmailWithHtmlAndText(count);

            service.Start();
            service.Send(messages);
            service.Stop(DeliveryCancellationHandling.EmptyQueue);

            Assert.AreEqual(count, service.Delivered);
            Assert.AreEqual(service.Delivered, inMemory.Messages.Count);
            Assert.IsTrue(service.DeliveryRate <= rate);

            Trace.WriteLine("Delivered: " + service.Delivered);
            Trace.WriteLine("Uptime: " + service.Uptime);
            Trace.WriteLine("Delivery rate: " + service.DeliveryRate + " msgs / second");
        }
        public void Can_schedule_delivery()
        {
            var config = new DeliveryConfiguration();
            
            var inMemory = new InMemoryEmailService();
            var service = new DeliveryService(inMemory, config);
            var messages = MessageFactory.EmailWithHtmlAndText(1).ToList();

            var message = messages.Single();
            message.DeliveryTime = 2.Seconds().FromNow();

            service.Start();
            service.Send(messages);
            Thread.Sleep(TimeSpan.FromSeconds(4));
            service.Stop(DeliveryCancellationHandling.EmptyQueue);

            Assert.AreEqual(1, service.Delivered);
        }
        public void Can_subscribe_to_watch_folder_and_deliver_new_emails()
        {
            CreateOrCleanDirectory("pickup");

            var block = new ManualResetEvent(false);
            const int trials = 500;

            var inMemory = new InMemoryEmailService();
            var config = new DeliveryConfiguration();
            var service = new DeliveryService(inMemory, config);
            
            // Wire up delivery subject and start service
            var collection = new Subject<EmailMessage>();
            service.Send(collection);
            service.Start();
            
            // Shuttles files into the subject
            var handled = 0;
            var folder = new DeliveryFolderObservable("pickup", "*.json", true);
            folder.Subscribe(e =>
            {
                if (File.Exists(e.EventArgs.FullPath))
                {
                    string json = null;
                    var read = false;
                    while (!read)
                    {
                        try
                        {
                            json = File.ReadAllText(e.EventArgs.FullPath);
                            read = true;
                        }
                        catch
                        {
                            // The FileSystemWatcher suuuuuuucks
                        }
                    }

                    var message = _serializer.DeserializeFromString(json);
                    collection.OnNext(message);
                    handled++;

                    if(File.Exists(e.EventArgs.FullPath))
                    {
                        File.Delete(e.EventArgs.FullPath);
                        if (handled == trials)
                        {
                            block.Set();
                        }
                    }
                    else
                    {
                        Console.WriteLine("Mayday! We delivered an email more than once!");
                        block.Set();
                    }
                }
            });
            
            // Meanwhile, back at the ranch, emails are a'brewin'...
            var queued = 0;
            Task.Factory.StartNew(() =>
            {
                var emails = MessageFactory.EmailWithHtmlAndText(trials);
                foreach (var email in emails)
                {
                    File.WriteAllText(Path.Combine("pickup", email.Id + ".json"), _serializer.SerializeToString(email));
                    queued++;
                }
            });
            
            block.WaitOne(); 
            Console.WriteLine(service.DeliveryRate);
            
            Assert.AreEqual(queued, inMemory.Messages.Count, "Could not keep up with demand");
            Assert.AreEqual(trials, inMemory.Messages.Count, "Did not deliver quota");

            Console.WriteLine(queued);
            CleanDirectory("pickup");
        }