private Container CreateContainer(TestableSerializer sender) { var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }, settings => { settings.CivilianCommandInterval = TimeSpan.FromSeconds(0); }).InitializeAndIsolateRepository(); return(containerManager); }
public void TwentyNineCivilianMessages_WithCommands_ExecutesOnlyThree() { var sender = new TestableSerializer(); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }).InitializeAndIsolateRepository(); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.PublicReceivedMessage("User01", "!time"), factory.PublicReceivedMessage("User02", "!time"), factory.PublicReceivedMessage("User03", "!time"), factory.PublicReceivedMessage("User04", "!time"), factory.PublicReceivedMessage("User05", "!time"), factory.PublicReceivedMessage("User06", "!time"), factory.PublicReceivedMessage("User07", "!time"), factory.PublicReceivedMessage("User08", "!time"), factory.PublicReceivedMessage("User09", "!time"), factory.PublicReceivedMessage("User10", "!time"), factory.PublicReceivedMessage("User11", "!time"), factory.PublicReceivedMessage("User12", "!time"), factory.PublicReceivedMessage("User13", "!time"), factory.PublicReceivedMessage("User14", "!time"), factory.PublicReceivedMessage("User15", "!time"), factory.PublicReceivedMessage("User16", "!time"), factory.PublicReceivedMessage("User17", "!time"), factory.PublicReceivedMessage("User18", "!time"), factory.PublicReceivedMessage("User19", "!time"), factory.PublicReceivedMessage("User20", "!time"), factory.PublicReceivedMessage("User21", "!time"), factory.PublicReceivedMessage("User22", "!time"), factory.PublicReceivedMessage("User23", "!time"), factory.PublicReceivedMessage("User24", "!time"), factory.PublicReceivedMessage("User25", "!time"), factory.PublicReceivedMessage("User26", "!time"), factory.PublicReceivedMessage("User27", "!time"), factory.PublicReceivedMessage("User28", "!time"), factory.PublicReceivedMessage("User29", "!time"), }; Task.Delay(2000).Wait(); data.ForEach(x => { pipelineManager.Enqueue(x); Task.Delay(1000).Wait(); }); Task.Delay(1000).Wait(); Assert.AreEqual(3, sender.Outbox.Count); }
public void AddingCommand_WithSpace_GetsResponse() { var sender = new TestableSerializer(); var containerManager = CreateContainer(sender); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!addcommand !hi greetings"), factory.PublicReceivedMessage("! hi"), }; Run(data, pipelineManager, sender); Assert.IsTrue(sender.Outbox.Cast <SendablePublicMessage>().First().Text == "!hi added"); Assert.IsTrue(sender.Outbox.Cast <SendablePublicMessage>().Skip(1).First().Text == "greetings"); }
public void PipelineManager_ReceivedError_SendsIt() { var expectedErrorMessage = "A random error"; var sender = new TestableSerializer(); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }).Container; var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { new ReceivedError(expectedErrorMessage, DateTime.UtcNow), }; Run(data, pipelineManager, sender); Assert.AreEqual(expectedErrorMessage, sender.Outbox.Cast <SendablePublicMessage>().Single().Text); }
public void Aegis_Always_PreventsMoreMutes() { var sender = new TestableSerializer(); var timeService = Substitute.For <ITimeService>(); timeService.UtcNow.Returns(TestHelper.RandomDateTime()); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); var timeServiceRegistration = Lifestyle.Singleton.CreateRegistration(() => timeService, container); container.RegisterConditional(typeof(ITimeService), timeServiceRegistration, pc => !pc.Handled); }).InitializeAndIsolateRepository(); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!nuke !time"), factory.PublicReceivedMessage("User01", "!time"), factory.PublicReceivedMessage("User02", "!time"), factory.PublicReceivedMessage("User03", "!time"), factory.PublicReceivedMessage("User04", "!time"), factory.PublicReceivedMessage("User05", "!time"), factory.ModPublicReceivedMessage("!aegis"), factory.PublicReceivedMessage("User06", "!time"), factory.PublicReceivedMessage("User07", "!time"), factory.PublicReceivedMessage("User08", "!time"), factory.PublicReceivedMessage("User09", "!time"), factory.PublicReceivedMessage("User10", "!time"), }; Task.Delay(1000).Wait(); data.ForEach(x => { Task.Delay(1000).Wait(); pipelineManager.Enqueue(x); }); Task.Delay(1000).Wait(); Assert.IsTrue(sender.Outbox.OfType <SendableMute>().Count() >= 4); Assert.IsTrue(sender.Outbox.OfType <SendableMute>().Count() <= 8); Assert.IsTrue(sender.Outbox.OfType <SendablePardon>().Count() >= 4); Assert.IsTrue(sender.Outbox.OfType <SendablePardon>().Count() <= 8); Assert.IsTrue(sender.Outbox.OfType <SendablePublicMessage>().Single().Text.Contains(timeService.DestinyNow.ToShortTimeString())); }
public void UpdatingCommand_Afterwards_GetsDifferentResponse() { var sender = new TestableSerializer(); var containerManager = CreateContainer(sender); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!addcommand !hi greetings"), factory.PublicReceivedMessage("!hi"), factory.ModPublicReceivedMessage("!addcommand !hi bonjour"), factory.PublicReceivedMessage("!hi"), }; Run(data, pipelineManager, sender); Assert.AreEqual("!hi added", sender.Outbox.Cast <SendablePublicMessage>().First().Text); Assert.AreEqual("greetings", sender.Outbox.Cast <SendablePublicMessage>().Skip(1).First().Text); Assert.AreEqual("!hi updated", sender.Outbox.Cast <SendablePublicMessage>().Skip(2).First().Text); Assert.AreEqual("bonjour", sender.Outbox.Cast <SendablePublicMessage>().Skip(3).First().Text); }
public void DeletingCommand_Afterwards_GetsNoResponse() { var sender = new TestableSerializer(); var containerManager = CreateContainer(sender); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!rules"), factory.ModPublicReceivedMessage("!delcommand !rules"), factory.ModPublicReceivedMessage("!rules"), factory.ModPublicReceivedMessage("!rules"), factory.ModPublicReceivedMessage("!rules"), }; Run(data, pipelineManager, sender); Assert.AreEqual("github.com/destinygg/bot2", sender.Outbox.Cast <SendablePublicMessage>().First().Text); Assert.AreEqual("!rules removed", sender.Outbox.Cast <SendablePublicMessage>().Skip(1).First().Text); Assert.AreEqual(2, sender.Outbox.Count); }
public void NukesExpire_Always_AfterSomeTime() { var sender = new TestableSerializer(); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }, settings => { settings.NukeMaximumLinger = TimeSpan.FromSeconds(5); }).InitializeAndIsolateRepository(); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var timeService = containerManager.GetInstance <ITimeService>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!nuke !time"), factory.PublicReceivedMessage("User01", "!time"), factory.PublicReceivedMessage("User02", "!time"), factory.PublicReceivedMessage("User03", "!time"), factory.PublicReceivedMessage("User04", "!time"), factory.PublicReceivedMessage("User05", "!time"), factory.PublicReceivedMessage("User06", "!time"), factory.PublicReceivedMessage("User07", "!time"), factory.PublicReceivedMessage("User08", "!time"), factory.PublicReceivedMessage("User09", "!time"), factory.PublicReceivedMessage("User10", "!time"), }; Task.Delay(1000).Wait(); data.ForEach(x => { Task.Delay(1000).Wait(); pipelineManager.Enqueue(x); }); Task.Delay(1000).Wait(); Assert.IsTrue(sender.Outbox.OfType <SendableMute>().Count() >= 4); Assert.IsTrue(sender.Outbox.OfType <SendableMute>().Count() <= 8); Assert.IsTrue(sender.Outbox.OfType <SendablePublicMessage>().Single().Text.Contains(timeService.DestinyNow.ToShortTimeString())); }
public void NukePunishes_UsersAfter_Always() { var sender = new TestableSerializer(); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }).InitializeAndIsolateRepository(); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!nuke banplox"), factory.PublicReceivedMessage("User", "banplox"), }; Task.Delay(1000).Wait(); data.ForEach(x => { Task.Delay(1000).Wait(); pipelineManager.Enqueue(x); }); Task.Delay(1000).Wait(); Assert.IsTrue(sender.Outbox.Cast <SendableMute>().First().Target.Nick == "User"); }
public void TwentyFiveCivilianMessages_WithNuke_ProcessesInUnder20ms() { var outputStopwatch = new Stopwatch(); var outputMilliseconds = new List <long>(); var outputStrings = new List <string>(); var sender = new TestableSerializer(s => { outputMilliseconds.Add(outputStopwatch.ElapsedMilliseconds); outputStrings.Add(s); }); var containerManager = new TestContainerManager(container => { var senderRegistration = Lifestyle.Singleton.CreateRegistration(() => sender, container); container.RegisterConditional(typeof(IFactory <IEnumerable <ISendable <ITransmittable> >, IEnumerable <string> >), senderRegistration, _ => true); }).InitializeAndIsolateRepository(); var factory = containerManager.GetInstance <ReceivedFactory>(); var pipelineManager = containerManager.GetInstance <IPipelineManager>(); var data = new List <IReceived <IUser, ITransmittable> > { factory.ModPublicReceivedMessage("!nuke burrito"), factory.PublicReceivedMessage("User01", "burrito"), factory.PublicReceivedMessage("User02", "burrito"), factory.PublicReceivedMessage("User03", "burrito"), factory.PublicReceivedMessage("User04", "burrito"), factory.PublicReceivedMessage("User05", "burrito"), factory.PublicReceivedMessage("User06", "burrito"), factory.PublicReceivedMessage("User07", "burrito"), factory.PublicReceivedMessage("User08", "burrito"), factory.PublicReceivedMessage("User09", "burrito"), factory.PublicReceivedMessage("User10", "burrito"), factory.PublicReceivedMessage("User11", "burrito"), factory.PublicReceivedMessage("User12", "burrito"), factory.PublicReceivedMessage("User13", "burrito"), factory.PublicReceivedMessage("User14", "burrito"), factory.PublicReceivedMessage("User15", "burrito"), factory.PublicReceivedMessage("User16", "burrito"), factory.PublicReceivedMessage("User17", "burrito"), factory.PublicReceivedMessage("User18", "burrito"), factory.PublicReceivedMessage("User19", "burrito"), factory.PublicReceivedMessage("User20", "burrito"), factory.PublicReceivedMessage("User21", "burrito"), factory.PublicReceivedMessage("User22", "burrito"), factory.PublicReceivedMessage("User23", "burrito"), factory.PublicReceivedMessage("User24", "burrito"), factory.PublicReceivedMessage("User25", "burrito"), }; var enqueueStopwatch = new Stopwatch(); enqueueStopwatch.Start(); outputStopwatch.Start(); data.ForEach(x => { pipelineManager.Enqueue(x); }); enqueueStopwatch.Stop(); Task.Delay(10000).Wait(); Console.WriteLine($"Enqueue Total: {enqueueStopwatch.ElapsedMilliseconds}"); Console.WriteLine($"Output Range : {outputMilliseconds.Min()} - {outputMilliseconds.Max()}"); Console.WriteLine($"Output Range : {outputStrings.Min()} - {outputStrings.Max()}"); Assert.IsTrue(outputMilliseconds.Max() - outputMilliseconds.Min() < 20); Assert.IsTrue(enqueueStopwatch.ElapsedMilliseconds < 20); Assert.IsTrue(outputStrings.Count > 10); }
private void Run(List <IReceived <IUser, ITransmittable> > data, IPipelineManager pipelineManager, TestableSerializer sender) { Task.Delay(100).Wait(); data.ForEach(x => { Task.Delay(100).Wait(); pipelineManager.Enqueue(x); }); Task.Delay(100).Wait(); foreach (var sendable in sender.Outbox) { Console.WriteLine(sendable); } }