using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Clients.Common; using HeuristicLab.Services.Hive.Common; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Clients.Hive.Slave.Tests { [TestClass] public class SlaveTest { // Use ClassInitialize to run code before running the first test in the class [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { PluginLoader.pluginAssemblies.Any(); ServiceLocator.Instance = new MockServiceLocator(); } private List> CreateMsgsForSingleJob() { List> allMsgs = new List>(); //get slave to fetch job List msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob)); allMsgs.Add(msg); //do nothing msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave)); allMsgs.Add(msg); return allMsgs; } [TestMethod] public void TestSingleJob() { Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 }; List jobList = new List(); jobList.Add(testJob); MockJob job = new MockJob(400, false); using (Disposable service = ServiceLocator.Instance.GetService()) { MockHiveService ms = (MockHiveService)service.Obj; ((MockHiveService)service.Obj).Messages = CreateMsgsForSingleJob(); ((MockHiveService)service.Obj).updateJobs(jobList, job); HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core(); core.Start(); Assert.AreEqual(1, ms.ResultJobs.Count); Assert.AreEqual(testJob.Id, ms.ResultJobs[0].Id); } } private List> CreateMsgsForShutdownSlaveWhileJobRunning() { List> allMsgs = new List>(); //get slave to fetch job List msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob)); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave)); allMsgs.Add(msg); return allMsgs; } [TestMethod] public void TestShutdownSlaveWhileJobRunning() { Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 }; List jobList = new List(); jobList.Add(testJob); MockJob job = new MockJob(10000, false); using (Disposable service = ServiceLocator.Instance.GetService()) { MockHiveService ms = (MockHiveService)service.Obj; ((MockHiveService)service.Obj).Messages = CreateMsgsForShutdownSlaveWhileJobRunning(); ((MockHiveService)service.Obj).updateJobs(jobList, job); HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core(); core.Start(); } } private List> CreateMsgsForTwoJobs() { List> allMsgs = new List>(); //get slave to fetch jobs List msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob)); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob)); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave)); allMsgs.Add(msg); return allMsgs; } [TestMethod] public void TestTwoJobs() { Job testJob1 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 }; Job testJob2 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 }; List jobList = new List(); jobList.Add(testJob1); jobList.Add(testJob2); MockJob job = new MockJob(2000, false); using (Disposable service = ServiceLocator.Instance.GetService()) { MockHiveService ms = (MockHiveService)service.Obj; ((MockHiveService)service.Obj).Messages = CreateMsgsForTwoJobs(); ((MockHiveService)service.Obj).updateJobs(jobList, job); HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core(); core.Start(); Assert.AreEqual(2, ms.ResultJobs.Count); } } } }