#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 System.ServiceModel; 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 { private static ServiceHost slaveComm; private static SlaveCommListener listener; // 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(); slaveComm = new ServiceHost(typeof(SlaveCommunicationService)); slaveComm.Open(); listener = new SlaveCommListener(); listener.Open(); } [ClassCleanup] public static void MyClassCleanup() { listener.Close(); slaveComm.Close(); } 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(); 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(300, 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); } } } }