public void ShouldThrowExceptionAndLogErrorWhenSubModuleFails() { MemoryLogger logger; IdlerModule module; IWorkerModule workerModule; ITaskModule taskModule; workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(true); logger = new MemoryLogger(); module = new IdlerModule(logger, taskModule, workerModule); Assert.ThrowsException <PIOInternalErrorException>(() => module.BeginIdle(1, 10)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Error) && (item.ComponentName == module.ModuleName))); workerModule = new MockedWorkerModule(true, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new IdlerModule(logger, taskModule, workerModule); Assert.ThrowsException <PIOInternalErrorException>(() => module.BeginIdle(1, 10)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Error) && (item.ComponentName == module.ModuleName))); }
public void ShouldEndTasks() { IdlerModule module; IWorkerModule workerModule; ITaskModule taskModule; workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(false); module = new IdlerModule(NullLogger.Instance, taskModule, workerModule); module.EndIdle(1); // nothing to check }
public void ShouldNotEndTaskWhenWorkerDoesntExists() { MemoryLogger logger; IdlerModule module; IWorkerModule workerModule; ITaskModule taskModule; logger = new MemoryLogger(); workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(false); module = new IdlerModule(logger, taskModule, workerModule); Assert.ThrowsException <PIONotFoundException>(() => module.BeginIdle(2, 10)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); }
public void ShouldNotIdleWhenWorkerIsAlreadyWorking() { MemoryLogger logger; IdlerModule module; IWorkerModule workerModule; ITaskModule taskModule; workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(false, new Task() { WorkerID = 1 }); logger = new MemoryLogger(); module = new IdlerModule(logger, taskModule, workerModule); Assert.ThrowsException <PIOInvalidOperationException>(() => module.BeginIdle(1, 10)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); }
public void ShouldIdle() { IdlerModule module; IWorkerModule workerModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; Task result; workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); taskModule = new MockedTaskModule(false); module = new IdlerModule(NullLogger.Instance, taskModule, workerModule); schedulerModule = new MockedSchedulerModule(false, module); result = module.BeginIdle(1, 10); Assert.IsNotNull(result); Assert.AreEqual(TaskTypeIDs.Idle, result.TaskTypeID); Assert.AreEqual(1, result.WorkerID); Assert.AreEqual(1, schedulerModule.Count); }
static void Main(string[] args) { ILogger logger; VersionControlModule versionControlModule; ServiceHostModule pioServiceHostModule; ServiceHostModule taskCallbackServiceHostModule; SchedulerModule schedulerModule; IPIOService pioService; ITaskCallbackService taskCallbackService; IDatabase database; IConnectionFactory connectionFactory; ICommandBuilder commandBuilder; IDatabaseCreator databaseCreator; IPlanetGeneratorModule planetGeneratorModule; IPhraseModule phraseModule; IPlanetModule planetModule; ICellModule cellModule; IBuildingModule buildingModule; IWorkerModule workerModule; IBuilderModule factoryBuilderModule; IStackModule stackModule; IResourceTypeModule resourceTypeModule; IBuildingTypeModule buildingTypeModule; ITaskTypeModule taskTypeModule; IMaterialModule materialModule; IIngredientModule ingredientModule; IProductModule productModule; ITaskModule taskModule; IIdlerModule idlerModule; IResourceCheckerModule resourceCheckerModule; ILocationCheckerModule locationCheckerModule; IProducerModule producerModule; IHarvesterModule harvesterModule; IMoverModule moverModule; ITakerModule takerModule; IStorerModule storerModule; 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.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 PIOVersionControl(database)); if (!versionControlModule.InitializeDatabase(Properties.Settings.Default.DropDatabase)) { return; } phraseModule = new PhraseModule(logger, database); planetModule = new PlanetModule(logger, database); cellModule = new CellModule(logger, database); buildingModule = new BuildingModule(logger, database); workerModule = new WorkerModule(logger, database); stackModule = new StackModule(logger, database); resourceTypeModule = new ResourceTypeModule(logger, database); buildingTypeModule = new BuildingTypeModule(logger, database); taskTypeModule = new TaskTypeModule(logger, database); materialModule = new MaterialModule(logger, database); ingredientModule = new IngredientModule(logger, database); productModule = new ProductModule(logger, database); taskModule = new TaskModule(logger, database); factoryBuilderModule = new BuilderModule(logger, taskModule, workerModule, buildingModule, buildingTypeModule, stackModule, materialModule);; idlerModule = new IdlerModule(logger, taskModule, workerModule); resourceCheckerModule = new ResourceCheckerModule(logger, buildingModule, stackModule, ingredientModule, materialModule); locationCheckerModule = new LocationCheckerModule(logger, workerModule, buildingModule); producerModule = new ProducerModule(logger, taskModule, workerModule, buildingModule, buildingTypeModule, stackModule, ingredientModule, productModule); harvesterModule = new HarvesterModule(logger, taskModule, workerModule, buildingModule, buildingTypeModule, stackModule, productModule); moverModule = new MoverModule(logger, taskModule, workerModule, buildingModule); takerModule = new TakerModule(logger, taskModule, workerModule, buildingModule, stackModule); storerModule = new StorerModule(logger, taskModule, workerModule, buildingModule, stackModule); planetGeneratorModule = new PlanetGeneratorModule(logger, phraseModule, resourceTypeModule, buildingTypeModule, taskTypeModule, materialModule, ingredientModule, productModule, planetModule, cellModule, buildingModule, workerModule); if (!planetGeneratorModule.Generate()) { return; } schedulerModule = new SchedulerModule(logger, taskModule, idlerModule, producerModule, harvesterModule, moverModule, takerModule, storerModule, factoryBuilderModule); schedulerModule.Start(); pioService = new PIOService( logger, phraseModule, planetModule, cellModule, buildingModule, workerModule, stackModule, resourceTypeModule, buildingTypeModule, taskTypeModule, materialModule, ingredientModule, productModule, taskModule, schedulerModule, resourceCheckerModule, locationCheckerModule, idlerModule, producerModule, harvesterModule, moverModule, takerModule, storerModule, factoryBuilderModule); pioServiceHostModule = new ServiceHostModule(logger, pioService); pioServiceHostModule.Start(); taskCallbackService = new TaskCallbackService(logger, schedulerModule); taskCallbackServiceHostModule = new ServiceHostModule(logger, taskCallbackService); taskCallbackServiceHostModule.Start(); WaitHandle.WaitAny(new WaitHandle[] { quitEvent }, -1); taskCallbackServiceHostModule.Stop(); pioServiceHostModule.Stop(); schedulerModule.Stop(); Console.CancelKeyPress -= new ConsoleCancelEventHandler(Console_CancelKeyPress); }