public void ShouldNotEndTaskWhenSubModuleFails() { MemoryLogger logger; TakerModule module; IBuildingModule buildingModule; IWorkerModule workerModule; IStackModule stackModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; buildingModule = new MockedBuildingModule(false, new Building() { BuildingID = 2, X = 10, Y = 10 }); workerModule = new MockedWorkerModule(true, new Worker() { WorkerID = 1, PlanetID = 1 }); stackModule = new MockedStackModule(false); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new TakerModule(logger, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); Assert.ThrowsException <PIOInternalErrorException>(() => module.EndTake(1, ResourceTypeIDs.Wood)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Error) && (item.ComponentName == module.ModuleName))); }
public void ShouldNotTakeWhenWorkerDoesntExists() { MemoryLogger logger; TakerModule module; IBuildingModule buildingModule; IWorkerModule workerModule; IStackModule stackModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; buildingModule = new MockedBuildingModule(false, new Building() { X = 10, Y = 10, BuildingID = 2 }); workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1 }); stackModule = new MockedStackModule(false, new Stack() { StackID = 0, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Plank, Quantity = 10 }); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new TakerModule(logger, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); Assert.ThrowsException <PIONotFoundException>(() => module.BeginTake(2, ResourceTypeIDs.Wood)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); Assert.AreEqual(0, schedulerModule.Count); }
public void ShouldTake() { TakerModule module; IBuildingModule buildingModule; IWorkerModule workerModule; IStackModule stackModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; Task result; buildingModule = new MockedBuildingModule(false, new Building() { BuildingID = 2, X = 10, Y = 10 }); workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1, X = 10, Y = 10 }); stackModule = new MockedStackModule(false, new Stack() { StackID = 0, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Plank, Quantity = 10 }, new Stack() { StackID = 1, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Wood, Quantity = 10 }, new Stack() { StackID = 2, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Stone, Quantity = 10 }, new Stack() { StackID = 3, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Coal, Quantity = 10 } ); taskModule = new MockedTaskModule(false); module = new TakerModule(NullLogger.Instance, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); result = module.BeginTake(1, ResourceTypeIDs.Wood); Assert.IsNotNull(result); Assert.AreEqual(1, result.WorkerID); Assert.AreEqual(1, schedulerModule.Count); }
public void ShouldNotTakeWhenWorkerIsAlreadyCarryingItem() { MemoryLogger logger; TakerModule module; IWorkerModule workerModule; ITaskModule taskModule; workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1, ResourceTypeID = ResourceTypeIDs.Wood }); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new TakerModule(logger, taskModule, workerModule, null, null); Assert.ThrowsException <PIOInvalidOperationException>(() => module.BeginTake(1, ResourceTypeIDs.Coal)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); }
public void ShouldEndTask() { TakerModule module; IBuildingModule buildingModule; MockedWorkerModule workerModule; IStackModule stackModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; buildingModule = new MockedBuildingModule(false, new Building() { BuildingID = 2, X = 10, Y = 10 }); workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1, X = 10, Y = 10 }); stackModule = new MockedStackModule(false, new Stack() { StackID = 0, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Plank, Quantity = 10 }, new Stack() { StackID = 1, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Wood, Quantity = 10 }, new Stack() { StackID = 2, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Stone, Quantity = 10 }, new Stack() { StackID = 3, BuildingID = 2, ResourceTypeID = ResourceTypeIDs.Coal, Quantity = 10 } ); taskModule = new MockedTaskModule(false); module = new TakerModule(NullLogger.Instance, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); module.EndTake(1, ResourceTypeIDs.Stone); Assert.AreEqual(ResourceTypeIDs.Stone, workerModule.ResourceTypeID); //Assert.AreEqual(2, workerModule.GetWorker(1).FactoryID); }
public void ShouldNotTakeWhenFactoryHasNotEnoughResourcesToTake() { MemoryLogger logger; TakerModule module; IBuildingModule buildingModule; IWorkerModule workerModule; IStackModule stackModule; ITaskModule taskModule; MockedSchedulerModule schedulerModule; buildingModule = new MockedBuildingModule(false, new Building() { BuildingID = 2, X = 10, Y = 10 }); workerModule = new MockedWorkerModule(false, new Worker() { WorkerID = 1, PlanetID = 1, X = 10, Y = 10 }); #region when all stacks exist stackModule = new MockedStackModule(false, new Stack() { StackID = 0, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Plank, Quantity = 10 }, new Stack() { StackID = 1, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Wood, Quantity = 10 }, new Stack() { StackID = 2, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Stone, Quantity = 0 }, new Stack() { StackID = 3, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Coal, Quantity = 10 } ); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new TakerModule(logger, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); Assert.ThrowsException <PIONoResourcesException>(() => module.BeginTake(1, ResourceTypeIDs.Stone)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); Assert.AreEqual(0, schedulerModule.Count); #endregion #region when a stack is missing stackModule = new MockedStackModule(false, new Stack() { StackID = 0, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Plank, Quantity = 10 }, new Stack() { StackID = 1, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Wood, Quantity = 10 }, new Stack() { StackID = 3, BuildingID = 1, ResourceTypeID = ResourceTypeIDs.Coal, Quantity = 10 } ); taskModule = new MockedTaskModule(false); logger = new MemoryLogger(); module = new TakerModule(logger, taskModule, workerModule, buildingModule, stackModule); schedulerModule = new MockedSchedulerModule(false, module); Assert.ThrowsException <PIONoResourcesException>(() => module.BeginTake(1, ResourceTypeIDs.Stone)); Assert.IsNotNull(logger.Logs.FirstOrDefault(item => (item.Level == LogLevels.Warning) && (item.ComponentName == module.ModuleName))); Assert.AreEqual(0, schedulerModule.Count); #endregion }
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); }