#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.SlaveCore.ConsoleClient; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Clients.Hive.SlaveCore.Tests { [TestClass] public class SlaveTest { private static SlaveCommListener listener; // Use ClassInitialize to run code before running the first test in the class [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { ServiceLocator.Instance = new MockServiceLocator(); /* listener = new SlaveCommListener(); listener.Open();*/ } [ClassCleanup] public static void MyClassCleanup() { //listener.Close(); } private List> CreateMsgsForSingleJob() { List> allMsgs = new List>(); //get slave to fetch job List msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob)); 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(), CoresNeeded = 1, MemoryNeeded = 0 }; testJob.State = JobState.Waiting; List jobList = new List(); jobList.Add(testJob); MockJob job = new MockJob(300, false); ServiceLocator.Instance.CallHiveService(service => { MockHiveService ms = (MockHiveService)service; ((MockHiveService)service).Messages = CreateMsgsForSingleJob(); ((MockHiveService)service).UpdateJobs(jobList, job); HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.Core(); core.Start(); Assert.AreEqual(1, ms.ResultJobs.Count); Assert.AreEqual(testJob.Id, ms.ResultJobs[0].Id); core.Shutdown(); }); } private List> CreateMsgsForShutdownSlaveWhileJobRunning() { List> allMsgs = new List>(); //get slave to fetch job List msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob)); 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(), CoresNeeded = 1, MemoryNeeded = 0 }; testJob.State = JobState.Waiting; List jobList = new List(); jobList.Add(testJob); MockJob job = new MockJob(10000, false); ServiceLocator.Instance.CallHiveService(service => { MockHiveService ms = (MockHiveService)service; ((MockHiveService)service).Messages = CreateMsgsForShutdownSlaveWhileJobRunning(); ((MockHiveService)service).UpdateJobs(jobList, job); HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.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.CalculateJob)); allMsgs.Add(msg); msg = new List(); allMsgs.Add(msg); msg = new List(); msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob)); 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(), CoresNeeded = 1, MemoryNeeded = 0 }; testJob1.State = JobState.Waiting; Job testJob2 = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 }; testJob2.State = JobState.Waiting; List jobList = new List(); jobList.Add(testJob1); jobList.Add(testJob2); MockJob job = new MockJob(2000, false); ServiceLocator.Instance.CallHiveService(service => { MockHiveService ms = (MockHiveService)service; ((MockHiveService)service).Messages = CreateMsgsForTwoJobs(); ((MockHiveService)service).UpdateJobs(jobList, job); HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.Core(); core.Start(); Assert.AreEqual(2, ms.ResultJobs.Count); core.Shutdown(); }); } } }