#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Clients.Hive; using HeuristicLab.Clients.Hive.Slave.Tests; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Services.Hive.Tests { using System.Threading; using HeuristicLab.Hive; using HeuristicLab.Services.Hive.Common; using DT = HeuristicLab.Services.Hive.Common.DataTransfer; [TestClass] public class ServiceTests { [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { PluginLoader.pluginAssemblies.Any(); ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance); } private IHiveService GetLocalService() { return new HiveService(); } [TestMethod] public void TestJobs() { var service = GetLocalService(); DT.HiveExperiment experiment = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" }; DT.Job job = new DT.Job() { CoresNeeded = 1, MemoryNeeded = 0, Priority = 0 }; DT.JobData jobData = new DT.JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, true)) }; job.Id = service.AddJob(job, jobData, null); experiment.RootJobId = job.Id; DT.Job jobLoaded = service.GetJob(job.Id); Assert.AreEqual(job.Id, jobLoaded.Id); Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded); Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded); Assert.AreEqual(job.Priority, jobLoaded.Priority); Assert.AreEqual(JobState.Waiting, jobLoaded.JobState); Assert.AreEqual(ServiceLocator.Instance.AuthorizationManager.UserId, job.UserId); DT.JobData jobDataLoaded = service.GetJobData(job.Id); Assert.AreEqual(job.Id, jobDataLoaded.JobId); Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data)); experiment.Id = service.AddHiveExperiment(experiment); DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id); Assert.AreEqual(experiment.Id, experimentLoaded.Id); Assert.AreEqual(experiment.Name, experimentLoaded.Name); Assert.AreEqual(experiment.Description, experimentLoaded.Description); Assert.AreEqual(experiment.RootJobId, experimentLoaded.RootJobId); service.DeleteHiveExperiment(experiment.Id); Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id)); Assert.AreEqual(null, service.GetJob(job.Id)); Assert.AreEqual(null, service.GetJobData(job.Id)); } List jobs = new List(); [TestMethod] public void TestHeartbeats() { var service = GetLocalService(); Guid groupId = service.AddSlaveGroup(new SlaveGroup() { Name = "TestGroup", Description = "Used for unit tests" }); for (int i = 0; i < 2; i++) { Job job = new Job() { CoresNeeded = 1, MemoryNeeded = 0 }; JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) }; job.Id = service.AddJob(job, jobData, null); jobs.Add(job); } var slaves = new List(); for (int i = 0; i < 1; i++) { DT.Slave slave = new DT.Slave() { Cores = 2, Memory = 4096, Name = "Slave " + i, IsAllowedToCalculate = true, SlaveState = SlaveState.Idle, CpuSpeed = 2800, FreeCores = 2, FreeMemory = 3000 }; slave.Id = service.AddSlave(slave); service.AddResourceToGroup(groupId, slave.Id); slaves.Add(slave); } foreach (var slave in slaves) { new Thread(new ParameterizedThreadStart(RunSlaveThread)).Start(slave); } // send heartbeats IEnumerable lightweightJobs; do { Thread.Sleep(500); lightweightJobs = service.GetLightweightJobs(jobs.Select(x => x.Id)); } while (!lightweightJobs.All(x => x.JobState == JobState.Finished)); // delete slaves foreach (var slave in slaves) { service.DeleteSlave(slave.Id); Assert.AreEqual(null, service.GetSlave(slave.Id)); } // delete group service.DeleteSlaveGroup(groupId); // delete jobs foreach (var job in jobs) { service.DeleteJob(job.Id); } } public void RunSlaveThread(object slaveobj) { try { var service = GetLocalService(); Slave slave = (Slave)slaveobj; int freeCores = slave.Cores.Value; var messages = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, FreeMemory = 2423, FreeCores = freeCores, JobProgress = new Dictionary() }); if (messages.Count == 0) return; // no more jobs Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.AbortJob).Count() == 0); Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.SayHello).Count() == 0); Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.PauseJob).Count() == 0); if (messages.Where(x => x.Message == MessageContainer.MessageType.AquireJob).Count() > 0) { Guid jobId = messages.Where(x => x.Message == MessageContainer.MessageType.AquireJob).SingleOrDefault().JobId; service.AquireJob(jobId); Job job = service.GetJob(jobId); JobData jobData = service.GetJobData(jobId); IJob deserializedJob = PersistenceUtil.Deserialize(jobData.Data); deserializedJob.Start(); job.JobState = JobState.Finished; jobs.Where(x => x.Id == jobId).Single().JobState = JobState.Finished; jobData.Data = PersistenceUtil.Serialize(deserializedJob); service.UpdateJob(job, jobData); } } catch (Exception e) { Assert.Fail(e.Message, e); } } } }