public void ShouldNotCreateTaskIfWorkerIsNotIdle() { int taskCreated = 0; BotSchedulerModule scheduler; IBotModule botModule; IOrderManagerModule orderManagerModule; PIO.ClientLib.PIOServiceReference.IPIOService client; botModule = Substitute.For <IBotModule>(); botModule.GetBots().Returns((x) => new Bot[] { new Bot() { BotID = 1, WorkerID = 1 } }); botModule.GetBot(Arg.Any <int>()).Returns((x) => new Bot() { BotID = 1, WorkerID = 1 }); orderManagerModule = Substitute.For <IOrderManagerModule>(); orderManagerModule.CreateTask(Arg.Any <int>(), Arg.Any <int>()).Returns(new Task()).AndDoes((x) => taskCreated++); client = Substitute.For <PIO.ClientLib.PIOServiceReference.IPIOService>(); client.GetLastTask(Arg.Any <int>()).Returns(new Task()); client.GetWorkers(Arg.Any <int>()).Returns(new Worker[] { new Worker() { WorkerID = 1 } }); scheduler = new BotSchedulerModule(NullLogger.Instance, client, botModule, orderManagerModule, 1); Assert.IsTrue(scheduler.Start()); Thread.Sleep(2000); Assert.IsTrue(scheduler.Stop()); Assert.AreEqual(0, taskCreated); }
public void ShouldNotCreateTaskIfFailstoRunTask() { int taskCreated = 0; BotSchedulerModule scheduler; IBotModule botModule; IOrderManagerModule orderManagerModule; PIO.ClientLib.PIOServiceReference.IPIOService client; MemoryLogger logger; logger = new MemoryLogger(); botModule = Substitute.For <IBotModule>(); botModule.GetBots().Returns((x) => new Bot[] { new Bot() { BotID = 1, WorkerID = 1 } }); botModule.GetBot(Arg.Any <int>()).Returns((x) => new Bot() { BotID = 1, WorkerID = 1 }); orderManagerModule = Substitute.For <IOrderManagerModule>(); orderManagerModule.CreateTask(Arg.Any <int>(), Arg.Any <int>()).Returns((x) => { throw new Exception(); }); client = Substitute.For <PIO.ClientLib.PIOServiceReference.IPIOService>(); client.GetLastTask(Arg.Any <int>()).Returns((x) => null); client.GetAllWorkers().Returns(new Worker[] { new Worker() { WorkerID = 1 } }); scheduler = new BotSchedulerModule(logger, client, botModule, orderManagerModule, 1); Assert.IsTrue(scheduler.Start()); Thread.Sleep(2000); Assert.IsTrue(scheduler.Stop()); Assert.AreEqual(0, taskCreated); Assert.IsTrue(logger.Logs.Where(item => (item.Level == LogLevels.Error) && (item.ComponentName == scheduler.ModuleName)).Count() > 0); }
static void Main(string[] args) { ILogger logger; VersionControlModule versionControlModule; //ServiceHostModule serviceHostModule; //IBotsService service; RESTServer server; IRouteManager routeManager; IDatabase database; IConnectionFactory connectionFactory; ICommandBuilder commandBuilder; IDatabaseCreator databaseCreator; IBotModule botModule; IOrderModule orderModule; IOrderManagerModule orderManagerModule; IProduceOrderModule produceOrderModule; IHarvestOrderModule harvestOrderModule; IBuildOrderModule buildOrderModule; IBotSchedulerModule botSchedulerModule; PIOServiceClient client; quitEvent = new AutoResetEvent(false); Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); //logger = new ConsoleLogger(new DefaultLogFormatter()); logger = new UnicastLogger(IPAddress.Loopback, Properties.Settings.Default.UnicastPort); //logger = new FileLogger(new DefaultLogFormatter(), "PIO.Bots.ServerHost.Log"); databaseCreator = new SqlDatabaseCreator(Properties.Settings.Default.Server, Properties.Settings.Default.DatabaseName); connectionFactory = new SqlConnectionFactory(Properties.Settings.Default.Server, Properties.Settings.Default.DatabaseName); commandBuilder = new SqlCommandBuilder(); database = new Database(connectionFactory, commandBuilder); versionControlModule = new VersionControlModule(logger, databaseCreator, new BotsVersionControl(database)); if (!versionControlModule.InitializeDatabase(Properties.Settings.Default.DropDatabase)) { Console.ReadLine(); return; } client = new PIOServiceClient(new BasicHttpBinding(), new EndpointAddress($@"http://127.0.0.1:8733/PIO/Service/")); client.Open(); botModule = new BotModule(logger, database); orderModule = new OrderModule(logger, database); produceOrderModule = new ProduceOrderModule(logger, database); harvestOrderModule = new HarvestOrderModule(logger, database); buildOrderModule = new BuildOrderModule(logger, database); orderManagerModule = new OrderManagerModule(logger, client, orderModule, produceOrderModule, harvestOrderModule, buildOrderModule, 10); botSchedulerModule = new BotSchedulerModule(logger, client, botModule, orderManagerModule, 5); botSchedulerModule.Start(); routeManager = new RouteManager(new RouteParser(), new ResponseSerializer()); routeManager.AddRouteHandler(new BotRouteHandler(botModule)); routeManager.AddRouteHandler(new ProduceOrderRouteHandler(produceOrderModule)); routeManager.AddRouteHandler(new HarvestOrderRouteHandler(harvestOrderModule)); routeManager.AddRouteHandler(new BuildOrderRouteHandler(buildOrderModule)); server = new RESTServer(logger, routeManager, "http://127.0.0.1:8734/"); server.Start(); WaitHandle.WaitAny(new WaitHandle[] { quitEvent }, -1); server.Stop(); //serviceHostModule.Stop(); botSchedulerModule.Stop(); client.Close(); logger.Dispose(); Console.CancelKeyPress -= new ConsoleCancelEventHandler(Console_CancelKeyPress); }